So the your code is like this:

 

var TitleStr: string = 'abc';

 

procedure test;

var  a: pChar;

begin

   a := @ TitleStr [1];

   a[1] := 'A';

end;

 

The reason it works is :

 

Though the string 'abc' is still defined in a reserved memory area when
OS allocates memory for the executable,

Delphi deals with a[1] := 'A' differently.

 

It logic is : apply a piece of memory from OS, copy  'abc' in reserved
memory area to this new memory area.

Then change the value of a[1] to 'A';

 

As var TitleStr: string = 'abc', means that TitleStr is a global
variable with initial value of 'abc', which you can change its value
later some time when running, so Delphi translates the code  : 

procedure test;

var  a: pChar;

begin

   a := @ TitleStr [1];

   a[1] := 'A';

end;

 

to the following assembly code :

 

mov eax, $0044b9bc            // $0044b9bc is the actual address im my
computer where 'abc' is defined.

                                             // It is in reserved memory
area which is read only.

Call UniqueString                    // inside the UniqueString, there
is a line : call @NewAnsistring, then make a copy of 'abc' to this
memory.

Mov byte ptr [eax + $01], 'A'    // eax now becomes $009432B0, it is the
pointer to the newly applied memory in the call UniqueString.

 

Thus no AV complains from OS.

 

 

Instead, if you define 

const TitleStr  = 'abc';

 

As it means that TitleStr is a global constant with value of 'abc',
which you can not change its value later some time when running, 

so Delphi now translates the above code  to the following assembly code
:

 

mov eax, $0044a91c            // $0044a91c is the actual address of my
computer where 'abc' is defined. It is in reserved memory area.

Mov byte ptr [eax + $01], 'A'    // eax points to the reserved memory 

 

Thus AV complains from OS jumps out.

 

 

 

 

 

 

 

 

 

 

________________________________

From: delphi-boun...@listserver.123.net.nz
[mailto:delphi-boun...@listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Monday, 13 May 2013 3:30 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] StrCopy problem - Reason : Const value notallowed
tobe changed

 

Yep.

 

 

Ross.

 

From: delphi-boun...@listserver.123.net.nz
[mailto:delphi-boun...@listserver.123.net.nz] On Behalf Of Jianming Lin
(FMI)
Sent: Monday, 13 May 2013 3:09 PM
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] StrCopy problem - Reason : Const value not allowed
tobe changed

 

Hi, Ross,

 

a := 'abc';

a[1] := 'A';

 

Certainly it will fail. 'abc' is defined in a reserved memory area when
OS allocates memory for the executable.

 

As for 

a := @ConsStr[1];

a[1] := 'A';

 

I am wondering how did you define ConsStr? 

var ConsStr : string = 'abc'; ?

 

 

________________________________

From: delphi-boun...@listserver.123.net.nz
[mailto:delphi-boun...@listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Monday, 13 May 2013 2:55 p.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: Re: [DUG] StrCopy problem - Reason : Const value not allowed
tobe changed

 

The String and pChar in my case are global vars and the code is
executing in a library dpr between begin..end.  There are no units or
objects or procedures in use.  This works fine when using global vars, I
just tried it...

 

a := @ConsStr[1];

a[1] := 'A';

 

But this fails...

 

a := 'abc';

a[1] := 'A';

 

Ross.

 

From: delphi-boun...@listserver.123.net.nz
[mailto:delphi-boun...@listserver.123.net.nz] On Behalf Of Jianming Lin
(FMI)
Sent: Monday, 13 May 2013 10:35 AM
To: NZ Borland Developers Group - Delphi List
Subject: Re: [DUG] StrCopy problem - Reason : Const value not allowed to
be changed

 

It seems that we all focus on the StrCopy. That's misleading.

 

Let's look at this code:

 

procedure test;

var  a: pChar;

begin

   a := 'abc';

   a[1] := 'A';

end;

 

 

procedure TForm1.Button1Click(Sender: TObject);

begin

  test;

end;

 

The line : a[1] will cause exactly same AV as your example.

 

As CPU window shows cpu instruction in assembly language, not many
people can understand it, I do further experiment:

 

 

 

procedure test;

var  a: pChar;

const ConsStr  = 'abc';

begin

   a := @ConsStr[1];

   a[1] := 'A';

end;

 

 

The same AV will happen. And that's the key problem where your code
actually is.

 

Now we can see that the reason of your code crash:

 

Delphi compile 'abcdefghi' as the local const value. 

Certainly constant value is not allowed to change.

If you put a line : ConsStr := 'def'; Compiler won't let you go.

If you purposely use the trick of pointer : 

   a := @ConsStr[1];

   a[1] := 'A';

to bypass compiler checking, the  operating system then has to activate
the last protection by showing the AV and stop your program to run. 

 

 

Bevan is right : the area of memory for const value is protected while
the application is running.

 

 

 

 

________________________________

From: delphi-boun...@listserver.123.net.nz
[mailto:delphi-boun...@listserver.123.net.nz] On Behalf Of Ross Levis
Sent: Sunday, 12 May 2013 2:39 a.m.
To: 'NZ Borland Developers Group - Delphi List'
Subject: [DUG] StrCopy problem

 

var

  a: pChar;

  b: pChar;

begin

  a := 'abcdefghi';

  b := 'jklmnopqr';

  StrCopy(a,b);

end;

 

Question:  Why does this code crash?

 

________________________________

Attention: 
This mail and any attachments are for the use of the intended recipient
only, and may contain information which is confidential and/or
privileged. If you have received this email in error, please advise us
by return email and immediately delete this email together with all
attachments. The contents of this email may only be used, distributed or
copied with the consent of the author. Fairview Metal Industries Ltd
takes reasonable precautions to minimise the risk of this email
containing any viruses but does not accept liability for any damage
caused by software viruses and advises the recipient to carry out their
own virus check on any attachments.

________________________________

________________________________

Attention: 
This mail and any attachments are for the use of the intended recipient
only, and may contain information which is confidential and/or
privileged. If you have received this email in error, please advise us
by return email and immediately delete this email together with all
attachments. The contents of this email may only be used, distributed or
copied with the consent of the author. Fairview Metal Industries Ltd
takes reasonable precautions to minimise the risk of this email
containing any viruses but does not accept liability for any damage
caused by software viruses and advises the recipient to carry out their
own virus check on any attachments.

________________________________


##################################################################################################
Attention: 
This mail and any attachments are for the use of the intended recipient only, 
and may contain
information which is confidential and/or privileged.  If you have received this 
email in error,
please advise us by return email and immediately delete this email together 
with all attachments.
The contents of this email may only be used, distributed or copied with the 
consent of the author. 

Fairview Metal Industries Ltd takes reasonable precautions to minimise the risk 
of this email containing
any viruses but does not accept liability for any damage caused by software 
viruses and advises 
the recipient to carry out their own virus check on any attachments.

##################################################################################################
_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: delphi@listserver.123.net.nz
Admin: http://delphi.org.nz/mailman/listinfo/delphi
Unsubscribe: send an email to delphi-requ...@listserver.123.net.nz with 
Subject: unsubscribe

Reply via email to