Conor Daly wrote:
>> On Tue, Jul 03, 2001 at 05:07:27AM -0400 or thereabouts, kevin lyda wrote:
> >
> > well, i left some thoughts out. all the str* routines assume strings are
> > nul (not NULL) terminated. a more dynamic version would malloc a char *
> > of strlen(s1) + strlen(s2) + 1;
> >
> > On Tue, Jul 03, 2001 at 08:38:50AM +0000, Conor Daly wrote:
> > > Some while back, Kevin posted the above which allocates *new* memory for a
> > > string s which contains s1 and s2 (useage: s = strdup2(s1, s2); ).
> > > Now, I have a string struct.s which, as the name suggests, is part of an
> > > already allocated struct. If I want to set *that* to contain s1 and s2, can
> > > I use the above ( struct.s = strdup2(s1, s2); ) or do I just use strncpy()
> > > and strncat()?
> >
> > so how is it defined? if it's
> >
> > struct foo {
> > char s[123];
> > };
> >
> > then no, the memory is already allocated. in fact using strdup2 would
> > be an error. otoh, if it is defined as:
> >
> > struct foo {
> > char *s;
> > };
> >
> > then the answer is "it depends." if that element of the struct is already
> > pointing at a string, you could use strncpy (if you know the length of
> > the data pointed to by s). or you could free it and use strdup2.
>> "if you know the length of the data pointed to by s"
>> Therin lies the rub. I'm trying to be general here so I don't know the
> length of the destination char *array* 's'. I can find out the length of the
> string *within* 's' using strlen() but I don't know of a function that will
> return the amount of memory *allocated* to 's'.
If you haven't explicitly allocated memory to s then it's not going to
have any memory allocated to it. If you're sure that there is some
memory allocated to s then realloc() can be used to change the size to
something else (assuming it succeeds). If it's uninitialised then
there's no issue - just allocate whatever you need to it and away you
go.
> This looks likely. so I use
>> foo.s = mystrdup2(foo.s, s2);
This will cause a segmentation violation if foo.s has not been allocated
space with malloc(). In particular, free(s1) will cause a problem. Also,
if foo.s hasn't been initialised it has an indeterminate value, and
evaluating it causes undefined behaviour (as in tmp = strdup(s1)).
>> where
<snipped>
> All this depends on being able to do malloc and free stuff with bits of
> structs. Like:
Of course you can. A pointer which is a member of a struct is still a
pointer, and subject to the same rules.
> struct foo {
> char *s;
> } foozle; /* to plageri[sz]e a term of Kevin's)
>> foozle.s = (char *) malloc( <number of chars required> * sizeof(char));
>> and
>> free(foozle.s);
In general, you shouldn't cast the return value of malloc. If you have
forgotten to include stdlib.h, the cast hides that fact. And the cast is
superfluous, since any pointer can be assigned without a cast to a void
*. The preferred form for malloc on comp.lang.c these days seems to be
a = malloc(n* sizeof *a);
But this code it fine otherwise :)
Cheers,
Dave.
--
David Neary, E-Mail dave.neary at palamon.ie
Palamon Technologies Ltd. Phone +353-1-634-5059
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!