• Advertisement

Archived

This topic is now archived and is closed to further replies.

Not These Damm [b] String[/b]s Again?

This topic is 6443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well, sorry for the imprecise topic name, but I just thought it sounds better this way I’m just wondering how strings are stored in memory? Is one string stored together in one block, or can it be spread out? And why the %$%$% can’t a string be passed as an const or var untyped parameter? examine the code below:
    
Program StringsSuck;

Procedure SomeF(var buffer; count : cardinal);
Var
I : cardinal;
Begin
For I:=0 to count-1 do
 //manipulate the bytes 

End;

Var
Str : String;

Begin
Str:=’Something or other’; 
SomeF(str); // here I get either an access violation or some unexpected string;

 End.

//this also doesn’t work (has unpredictable results)
…
Var
Str : String;
S : TSomeStream;
Begin
Str:=’Something or other’;
s:=TSomeStream.Create;
s.write(str,10); 
S.Read(str,10);
//str=’’

End;
    
Does anyone know a way around this? Thanks for your help, Pascalix;

Share this post


Link to post
Share on other sites
Advertisement
Hi Pascalix,

well the following is working in Delphi 5.0
        
procedure TForm1.Button1Click(Sender: TObject);
var Str : String;
MemoryStream : TMemoryStream;
begin
Str := 'Something or other';
MemoryStream := TMemoryStream.Create;
MemoryStream.Write(str, 10);
str:='';
MemoryStream.Seek(0, soFromBeginning);//Maybe forgot this ?

MemoryStream.Read(str, 10);
Button1.Caption := str;
end;
[/source]

You can of course also use a TStringStream which is made for String operations.

The other one is also working for me <img src="wink.gif" width=15 height=15 align=middle>

[source]
procedure TForm1.Button2Click(Sender: TObject);
Procedure SomeF(var buffer; count : cardinal);
var i : cardinal;
begin
//manipulate the bytes

for i := 0 to count-1 do String(Buffer)<i> := Chr(Ord(String(Buffer)[i]) + 1);
for i := 0 to count-1 do String(Buffer)[i] := Chr(Ord(String(Buffer)[i]) - 1)
end;
var Str : String;
begin
Str:='Something or other';
SomeF(str, 18);
Button2.Caption := str;
end;


Which version of Delphi do you use ?

DAMN what is this board doing ?

Edited by - Ampaze on June 26, 2000 9:16:47 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
s.write(str, 10) causes 10 bytes to be written starting at the address of str. str is essentially a pointer to another memory location that has the actual string. You want to write from the location that has the string so you must write s.write(str[1], 10).

Share this post


Link to post
Share on other sites
Thanks anonymous,I’ve always suspected that str might basically be a pointer. The "problem" can be solved either like this :


s.Write(PChar(str)^,Length(str));

or like this:

s.Write(str[1],Length(str));



Ampaze: Strange..., I also have Delphi 5. This exact code works? Are you sure your not typecasting strings? Maybe you have {$H–} (String=ShortString) set?

Oh, yeah,
The board probably can’t handle more than one source /source. Lets try…
    
source 1
[/source]

[source]
source 2



Pascalix;

Edited by - Pascalix on June 29, 2000 5:11:55 PM

Share this post


Link to post
Share on other sites
Hi Pasacalix,

i just tryed the source again, and yes, it works with that exact code. It works with either {H-} or {H+} set .
Have you tryed to open a new project, drop a Button on it and copy the On Click Procedures from my other posting to Delphi ?
For me this works, whereas

s.Write(str[1],Length(str));

won't work.

If you look at the second example you see that I actually do typecasting on the buffer. But you kinda have to, if you allready know that it contains stringdata, it would be stupid not to do it.

As for another thing. If you're using quite a lot of WinApi-calls
you can also use PChar for your whole "Stringwork" right from the beginning of your project.

All for now

ARGH !!!! This board is against me ! It changes "O n C l i c k" to whenclickedon ... hmpf ...

Edited by - Ampaze on June 30, 2000 9:54:11 AM

Share this post


Link to post
Share on other sites
(sorry for the late reply, but i've been pretty busy)

Well, I still haven’t figured out why

s.Write(str,Length(str));

works on your computer.
If I run this I get an access violation or some strange string.
Try running this and see if str is still the same:
 
s.Write(str,Length(str));
str:=’’;
s.Seek(0,ssfromBegginning);
s.Read(str,Length(str));


the TstringStream uses the Pchar(str)^ typecast to write to the buffer and the SetString function to read a string.
*goes to look for the source code*

Pascalix;

Edited by - Pascalix on July 5, 2000 4:18:29 PM

Share this post


Link to post
Share on other sites
Hi
well it''s obvious that this code won''t work !
With str:=''''; you give the string a length of zero.
So any call to s.Read(str,Length(str)); won''t read anything. (or to be exact, the stream actually reads 0 bytes )

But to answer your question it does work , but str contains nothing, no access violation or anything !



[ Ampaze | turbo.gamedev.net ]

Share this post


Link to post
Share on other sites
Sorry, my mistake. With str=’’; I just wanted to clear the string, because I thought, that maybe either the

s.Write(str,Length(str));

or

s.Read(str,Length(str));


got ignored, because they might cause some error and the str would still have the same value, because it wouldn’t get changed.

Anyway, this is a mystery, but don’t devote too much time to it, ‘cause it really isn’t that urgent.


Pascalix;

Share this post


Link to post
Share on other sites
Why would you wanna pass the string as an untyped parameter when you know that you''ll be working with strings only?!?!

i dont have Pascal here and have forgotten some of the syntax, but here is what you might wanna try...

StrF(str^);

..-=ViKtOr=-..

Share this post


Link to post
Share on other sites

  • Advertisement