Article View: comp.sys.next.programmer
Article #637Re: NeXT-cc bug report
From: Dan Danz
Date: Sun, 12 Apr 1992 18:47
Date: Sun, 12 Apr 1992 18:47
41 lines
2202 bytes
2202 bytes
In article <1992Apr8.183539.1868@talus.com> ajamison@talus.com (Alex
Senkevitch) writes:
> In article <1992Apr7.084848.29747@reswi.en.open.de>
> ralf@reswi.en.open.de (Ralf E. Stranzenbach) writes:
> > the following code fragment did _not_ compile properly:
> > void some_function (char * s)
> > {
> > char * d;
> > d = strcpy (alloca (strlen (s) + 1), s);
> > printf ("%s copied as %s\n", s, d);
> > }
> try this:
> void some_function(char *s) {
> char *d;
> strcpy((d=(char*)malloc(strlen(s)+1)),s);
> printf("%s copied as %s\n", s, d);
> }
What Alex has done is create a memory leak -- every time this
version of the routine is called, it allocates something in the
heap and then leaves it there. That's the beauty of alloca --
it allocates on the stack (by extending the stack pointer), and
this storage is automatically reclaimed when the subroutine
returns.
Obviously, the way to fix Alex's version is to add a free(d)
statement. before the return. Maybe Alex knew this, but I others reading
this post might not.
I suspect that Ralf's original problem has to do with both the compiler
AND alloca trying to anipulate the stack pointer to create
temporary space on the stack. This is one of the things that makes alloca
a difficult function to implement. Try it by doing the alloca call first
and then do the strcpy into d as a separate line of code.
--
L. W. "Dan" Danz VOS Mail: Dan_Danz@vos.stratus.com
Sr Consulting Software SE NeXT Mail: dan@az.stratus.com
Customer Assistance Center Voice Mail/Pager: (602) 852-3107
Telecommunications Division Customer Service: (800) 828-8513
Stratus Computer, Inc. 4455 E. Camelback #115-A, Phoenix AZ 85018
Technical Details
Message-ID:
<2121@transfer.stratus.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!centerline!noc.near.net!nic.umass.edu!m2c!crackers!transfer!quiensabe.az.stratus.comReferences:
<1992Apr8.183539.1868@talus.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!centerline!noc.near.net!nic.umass.edu!m2c!crackers!transfer!quiensabe.az.stratus.com From: dan@quiensabe.az.stratus.com (Dan Danz) Newsgroups: comp.sys.next.programmer Subject: Re: NeXT-cc bug report Keywords: alloca Message-ID: <2121@transfer.stratus.com> Date: 12 Apr 92 18:47:49 GMT References: <1992Apr8.183539.1868@talus.com> Sender: usenet@transfer.stratus.com Reply-To: dan@az.stratus.com Lines: 41