<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi Ross,<br>
<br>
You don't say which version of Delphi you're using, but I assume
it's pre-Unicode support, since you use PChar instead of PAnsiChar
or PWideChar.<br>
<br>
Out of curiosity I built a small test application and traced through
the source code for this snippet of code using the CPU window. What
I noticed is that the address of the two strings assigned to 'a' and
'b' are part of program memory. More specifically, Delphi appears
to store these strings immediately after the "return" opcode for the
procedure and simply puts those addresses into 'a' and 'b'. Since
this area of memory is protected while the application is running,
that's why you get the error.<br>
<br>
By using:<br>
a:=AllocMem(10);<br>
StrCopy(a, 'abcdefghi');<br>
b:='jklmnopqr';<br>
StrCopy(a, b);<br>
<br>
The variable 'b' now points to application memory while 'a' is
allocated a "write-enabled" memory address, so the access violation
doesn't occur.<br>
<br>
Regards,<br>
<br>
Bevan<br>
<br>
<br>
<br>
<div class="moz-cite-prefix">On 12/05/2013 5:30 p.m., Ross Levis
wrote:<br>
</div>
<blockquote cite="mid:002001ce4ed1$dc79bc20$956d3460$@com"
type="cite">
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
<meta name="Generator" content="Microsoft Word 12 (filtered
medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:PMingLiU;
        panose-1:2 2 3 0 0 0 0 0 0 0;}
@font-face
        {font-family:PMingLiU;
        panose-1:2 2 3 0 0 0 0 0 0 0;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
@font-face
        {font-family:"\@PMingLiU";
        panose-1:2 2 3 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
p.MsoAcetate, li.MsoAcetate, div.MsoAcetate
        {mso-style-priority:99;
        mso-style-link:"Balloon Text Char";
        margin:0cm;
        margin-bottom:.0001pt;
        font-size:8.0pt;
        font-family:"Tahoma","sans-serif";}
span.apple-style-span
        {mso-style-name:apple-style-span;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
span.BalloonTextChar
        {mso-style-name:"Balloon Text Char";
        mso-style-priority:99;
        mso-style-link:"Balloon Text";
        font-family:"Tahoma","sans-serif";}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
<div class="WordSection1">
<p class="MsoNormal"><span
style="font-family:"Calibri","sans-serif";color:#1F497D">I
did see a & b had a pointer address allocated. It
looked like a normal pointer address range. I’ll try
allocating 10 bytes to a later and see what happens.<o:p></o:p></span></p>
<p class="MsoNormal"><span
style="font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span
style="font-family:"Calibri","sans-serif";color:#1F497D">Ross.<o:p></o:p></span></p>
<p class="MsoNormal"><span
style="font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF
1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif""
lang="EN-US">From:</span></b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif""
lang="EN-US"> <a class="moz-txt-link-abbreviated" href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a>
[<a class="moz-txt-link-freetext" href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>] <b>On
Behalf Of </b>Bevan Edwards<br>
<b>Sent:</b> Sunday, 12 May 2013 4:48 PM<br>
<b>To:</b> <a class="moz-txt-link-abbreviated" href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a><br>
<b>Subject:</b> Re: [DUG] StrCopy problem<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Actually, based on that article
a:='abcdefghi' should assign 10 bytes and include the zero
byte at the end. But I wonder if the problem is due to
where this data is stored and trying to copy b to a
results in an attempt to write to protected memory?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Have you tried allocating 10 bytes of
memory to 'a' and then using StrCopy?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Have you run the debugger and checked
where the PChars 'a' and 'b' are pointing to before
StrCopy?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt"><br>
<br>
<br>
-------- Original message --------<br>
From: Ross Levis <<a moz-do-not-send="true"
href="mailto:ross@stationplaylist.com">ross@stationplaylist.com</a>>
<br>
Date: <br>
To: 'NZ Borland Developers Group - Delphi List' <<a
moz-do-not-send="true"
href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a>>
<br>
Subject: Re: [DUG] StrCopy problem <br>
<br>
<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">Doesn’t really help.<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">a := 'abcdefghi' does allocate 9
bytes of RAM. I can access a and b after it is
assigned. The problem is StrCopy crashes. I would
expect “a” to have the same string as b once this is
executed.<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">Ross.<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<div>
<div style="border:none;border-top:solid #B5C4DF
1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:#1F497D"
lang="EN-US">From:</span></b><span
style="font-size:10.0pt;font-family:"Tahoma","sans-serif";color:#1F497D"
lang="EN-US"> <a moz-do-not-send="true"
href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a>
[<a moz-do-not-send="true"
href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>]
<b>On Behalf Of </b>Keith Allpress<br>
<b>Sent:</b> Sunday, 12 May 2013 10:27 AM<br>
<b>To:</b> 'NZ Borland Developers Group - Delphi
List'<br>
<b>Subject:</b> Re: [DUG] StrCopy problem</span><span
style="color:#1F497D"><o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">Perhaps:<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"><a moz-do-not-send="true"
href="http://rvelthuis.de/articles/articles-pchars.html">http://rvelthuis.de/articles/articles-pchars.html</a><o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1
1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span
style="color:#1F497D" lang="EN-US">From:</span></b><span
style="color:#1F497D" lang="EN-US"> <a
moz-do-not-send="true"
href="mailto:delphi-bounces@listserver.123.net.nz">delphi-bounces@listserver.123.net.nz</a>
[<a moz-do-not-send="true"
href="mailto:delphi-bounces@listserver.123.net.nz">mailto:delphi-bounces@listserver.123.net.nz</a>]
<b>On Behalf Of </b>Ross Levis<br>
<b>Sent:</b> Sunday, 12 May 2013 2:39 a.m.<br>
<b>To:</b> 'NZ Borland Developers Group - Delphi
List'<br>
<b>Subject:</b> [DUG] StrCopy problem</span><span
style="color:#1F497D"><o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">var<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> a: pChar;<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> b: pChar;<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">begin<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> a := 'abcdefghi';<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> b := 'jklmnopqr';<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> StrCopy(a,b);<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">end;<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D">Question: Why does this code
crash?<o:p></o:p></span></p>
<p class="MsoNormal"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><span
style="color:#1F497D"> <o:p></o:p></span></p>
</div>
</div>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
NZ Borland Developers Group - Delphi mailing list
Post: <a class="moz-txt-link-abbreviated" href="mailto:delphi@listserver.123.net.nz">delphi@listserver.123.net.nz</a>
Admin: <a class="moz-txt-link-freetext" href="http://delphi.org.nz/mailman/listinfo/delphi">http://delphi.org.nz/mailman/listinfo/delphi</a>
Unsubscribe: send an email to <a class="moz-txt-link-abbreviated" href="mailto:delphi-request@listserver.123.net.nz">delphi-request@listserver.123.net.nz</a> with Subject: unsubscribe</pre>
</blockquote>
<br>
</body>
</html>