Why strdup

L

lovecreatesbea...

I am trying the FREE net-snmp library things. Like the FREE libxml++
library, it lacks of a reasonable document on its API. Its very
begining example code compiles and runs. It says "blah blah .." when I
substitute the host address for a snmp equipped machine
``192.168.5.10'' within our LAN like this:

session.peername = strdup("192.168.5.10");

Is strdup still in use? Is there anything prevents the code from
becoming these:

char *p = "192.168.5.10";
char a[] = 192.168.5.10";

Thank you for your time.
 
R

rahul

I am trying the FREE net-snmp library things. Like the FREE libxml++
library, it lacks of a reasonable document on its API. Its very
begining example code compiles and runs. It says "blah blah .." when I
substitute the host address for a snmp equipped machine
``192.168.5.10'' within our LAN like this:

session.peername = strdup("192.168.5.10");

strdup is not conforming to ISO C. It is conforming to POSIX.
Is strdup still in use? Is there anything prevents the code from
becoming these:

char *p = "192.168.5.10";
strdup returns a pointer to character. So the call is the same as you
mention.
char a[] = 192.168.5.10";
Here a is an array ( p is a pointer in the previous case) and this can
be done in the initialization.
char a[10];
a = "abc"; /* will generate an error */

From what you have posted, peername appears to be char *. so the call
is char *p = "192.168.5.10"

There is one subtle difference though. String literals may be stored
in read-only memory but strdup does the allocation using malloc.
 
C

Chris Dollin

I am trying the FREE net-snmp library things. Like the FREE libxml++
library, it lacks of a reasonable document on its API. Its very
begining example code compiles and runs. It says "blah blah .." when I
substitute the host address for a snmp equipped machine
``192.168.5.10'' within our LAN like this:

session.peername = strdup("192.168.5.10");

Is strdup still in use? Is there anything prevents the code from
becoming these:

char *p = "192.168.5.10";
char a[] = 192.168.5.10";

Thank you for your time.

If the code that handles the session object expects to be able
to free or realloc the peername, then copying the string literal
to mallocated store is essential.
 
R

Richard Tobin

Is strdup still in use? Is there anything prevents the code from
becoming these:

char *p = "192.168.5.10";
char a[] = 192.168.5.10";

Quite likely the copied string is passed or returned to functions which
normally get malloc()ed strings. If these strings were not strdup()ed,
the code would have to keep track of which strings were static and
which needed to be free()ed.

-- Richard
 
L

lovecreatesbea...

I am trying the FREE net-snmp library things. Like the FREE libxml++
library, it lacks of a reasonable document on its API. Its very
begining example code compiles and runs. It says "blah blah .." when I
substitute the host address for a snmp equipped machine
``192.168.5.10'' within our LAN like this:
session.peername = strdup("192.168.5.10");

strdup is not conforming to ISO C. It is conforming to POSIX.
Is strdup still in use? Is there anything prevents the code from
becoming these:
char *p = "192.168.5.10";

strdup returns a pointer to character. So the call is the same as you
mention.
char a[] = 192.168.5.10";

Sorry for the missed quote mark,

char a[] = "192.168.5.10";
Here a is an array ( p is a pointer in the previous case) and this can
be done in the initialization.
char a[10];
a = "abc"; /* will generate an error */

Yes. I would copy or assign the data to array elements.
From what you have posted, peername appears to be char *. so the call
is char *p = "192.168.5.10"

There is one subtle difference though. String literals may be stored
in read-only memory but strdup does the allocation using malloc.

Yes, and the caller needs to remember to call an extra free() on it
somewhere. I think the need of strdup() in program indicates design
flaw.
 
L

lovecreatesbea...

session.peername = strdup("192.168.5.10");
Is strdup still in use? http://www.google.com/search?hl=en&ie=ISO-8859-1&q="Do+you+still+be...
Is there anything prevents the code from
becoming these:
char *p = "192.168.5.10";
char a[] = 192.168.5.10";

char a[] = "192.168.5.10";
I only know strdup as a code example from K&R2,
section 6.5, self-referential structures.
...

If you want to use that version of it,
1 remove the cast, because it doesn't help

Yes. I also see the book errata on its web site.
and it can prevent a C89 compiler
from diagnosing undefined behavior
in the event that <stdlib.h> is not #included.

The -ansi option gives out a warning on below the code without that
cast. Why ansi is not ansi?

$ gcc --version
gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ gcc -ansi -pedantic a.c
a.c: In function ‘main’:
a.c:7: warning: initialization makes pointer from integer without a
cast
$ cat a.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *s = strdup("192.168.5.10"); /*LINE 7*/
char *p = "192.168.5.10";
char a[] = "192.168.5.10";

printf("%s\n%s\n%s\n", s, p, a);
return 0;
}
$ gcc -ansi -pedantic a.c
a.c: In function ‘main’:
a.c:7: warning: initialization makes pointer from integer without a
cast
$
 
J

Joachim Schmitz

session.peername = strdup("192.168.5.10");
Is strdup still in use? http://www.google.com/search?hl=en&ie=ISO-8859-1&q="Do+you+still+be...
Is there anything prevents the code from
becoming these:
char *p = "192.168.5.10";
char a[] = 192.168.5.10";

char a[] = "192.168.5.10";
I only know strdup as a code example from K&R2,
section 6.5, self-referential structures.
...

If you want to use that version of it,
1 remove the cast, because it doesn't help

Yes. I also see the book errata on its web site.
and it can prevent a C89 compiler
from diagnosing undefined behavior
in the event that <stdlib.h> is not #included.

The -ansi option gives out a warning on below the code without that
cast. Why ansi is not ansi?

$ gcc --version
gcc (GCC) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

$ gcc -ansi -pedantic a.c
a.c: In function ‘main’:
a.c:7: warning: initialization makes pointer from integer without a
cast
$ cat a.c
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
char *s = strdup("192.168.5.10"); /*LINE 7*/
char *p = "192.168.5.10";
char a[] = "192.168.5.10";

printf("%s\n%s\n%s\n", s, p, a);
return 0;
}
$ gcc -ansi -pedantic a.c
a.c: In function ‘main’:
a.c:7: warning: initialization makes pointer from integer without a
cast
$
Probably because with -ansi -pedantic there's no prototype for the
non-standard strdup in scope henve it defaults to be assumed to return int.
Adding -Wall (and maybe more) should have warned about an implicit
declaration.

Bye, Jojo
 
L

lovecreatesbea...

Probably because with -ansi -pedantic there's no prototype for the
non-standard strdup in scope henve it defaults to be assumed to return int.
Adding -Wall (and maybe more) should have warned about an implicit
declaration.

Thank you. I see the change.

I find the only declaration surrounded by the preprocessing directive.
It may be this one that have been skipped with -ansi -pedantic
options.

...

#if defined __USE_SVID || defined __USE_BSD || defined
__USE_XOPEN_EXTENDED
/* Duplicate S, returning an identical malloc'd string. */
extern char *strdup (__const char *__s)
__THROW __attribute_malloc__ __nonnull ((1));
#endif

...
"/usr/include/string.h" [readonly] 428 lines --19%--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top