[ILUG] C question [puzzled by strdup()]
Conor Daly
conor.daly at met.ie
Thu Aug 2 10:44:46 IST 2001
On Thu, Aug 02, 2001 at 10:31:41AM +0100 or thereabouts, John P. Looney wrote:
> On Thu, Aug 02, 2001 at 12:06:47AM +0100, Conor Daly mentioned:
> > The first call to strdup() allocates memory to hold the string and sets
> > optionstring to that address. When the second strdup() call passes a
> > *new* address to optionstring, what happens the previously allocated
> > memory that optionstring *was* pointing to?
>
> No offense Conor, but I strongly recommend you stick to Perl, Shell, TCL
> or Java! Something that'll do your garbage collection for you.
Alas, we have no perl! Got some swine though...
I had a suspicion about strdup though but I just wanted to check in case
something really clever goes on on there.
> In the manpage for strdup, there is;
>
> "Memory for the new string is obtained with mal loc(3), and can
> be freed with free(3)"
>
> It should read "For crying out loud, free this as soon as you can, or
> you'll be reaching for ElectricFence within a week".
:-)
Interestingly though, all this bounds checking and stuff I'm doing is
something of a novelty here. Consider the function read_to_endofline() that
uses fgetc() to read chars until it reaches a '\n' *without* checking for
EOF! The program using it was eating '-1's from an empty stream for about
two days before anyone noticed!
Conor
--
Conor Daly
Met Eireann, Glasnevin Hill, Dublin 9, Ireland
Ph +353 1 8064276 Fax +353 1 8064275
------------------------------------
9:37am up 69 days, 20:39, 8 users, load average: 2.00, 2.00, 2.00
More information about the ILUG
mailing list