Yep.
var TitleStr: string = 'abc'; Ross. From: [email protected] [mailto:[email protected]] 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: [email protected] [mailto:[email protected]] 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: [email protected] [mailto:[email protected]] 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: [email protected] [mailto:[email protected]] 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. _____
_______________________________________________ NZ Borland Developers Group - Delphi mailing list Post: [email protected] Admin: http://delphi.org.nz/mailman/listinfo/delphi Unsubscribe: send an email to [email protected] with Subject: unsubscribe
