Connecting Tech Pros Worldwide Forums | Help | Site Map

strdup in Borland C++Builder

Stefan Schwärzler
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi Ng,
habe nicht besonders viel Erfahrung in C und C++, deshalb:
möchte den befehl strdup in <string.h> verwenden.

#include <string.h>
attrib(char *name, char *val) : name(strdup(name)), val(strdup(val)),
next(0) {
CDEBUG(printf("attrib::attrib(%s, %s)\n", name, val));
}

Borland bringt dann die Meldung "undefinierte Funktion" 'strdup'
Müsste doch in string.h definiert sein. Oder macht hier Borland was anderes.

Danke für Eure Hilfe,

Stefan



lallous
Guest
 
Posts: n/a
#2: Jul 22 '05

re: strdup in Borland C++Builder


"Stefan Schwärzler" <stefan.sa.schwaerzler_usenet@bmw.de> wrote in message
news:chrpdl$j8k1@usenet.bmw.de...[color=blue]
> Hi Ng,
> habe nicht besonders viel Erfahrung in C und C++, deshalb:
> möchte den befehl strdup in <string.h> verwenden.
>
> #include <string.h>
> attrib(char *name, char *val) : name(strdup(name)), val(strdup(val)),
> next(0) {
> CDEBUG(printf("attrib::attrib(%s, %s)\n", name, val));
> }
>
> Borland bringt dann die Meldung "undefinierte Funktion" 'strdup'
> Müsste doch in string.h definiert sein. Oder macht hier Borland was
> anderes.
>
> Danke für Eure Hilfe,
>[/color]

Apparently, Borland C++ doesn't define strdup(), simply write it as:

char *strdup( const char *s )
{
char *dup = malloc(strlen(s) +1);
return strcpy(dup, s);
}

p.s: Next time it is better that you post to german c/c++ newsgroup next
time.

--
Elias


Ivan Vecerina
Guest
 
Posts: n/a
#3: Jul 22 '05

re: strdup in Borland C++Builder


"Stefan Schwärzler" <stefan.sa.schwaerzler_usenet@bmw.de> wrote in message
news:chrpdl$j8k1@usenet.bmw.de...[color=blue]
> Hi Ng,[/color]
....[color=blue]
> #include <string.h>
> attrib(char *name, char *val) : name(strdup(name)), val(strdup(val)),
> next(0) {
> CDEBUG(printf("attrib::attrib(%s, %s)\n", name, val));
> }
>
> Borland bringt dann die Meldung "undefinierte Funktion" 'strdup'
> Müsste doch in string.h definiert sein. Oder macht hier Borland was
> anderes.[/color]

The function "strdup" is not part of the C or C++ standards.
It is common on UNIX however, and part of some related
standards (e.g. http://tinyurl.com/46cp5 ).
Its typical implementation will look like:
char *strdup(const char *s)
{
size_t l = 1+strlen(s);
char* p = malloc(l);
if( !! p ) memcpy( p, s, l );
return p;
}

Since you are programming C++, however, I would recommend using
std::string instead, as it makes it easier to write safe and
correct code.

Also, when writing a post in German, you obviously should use
de.comp.lang.iso-c++ (maybe this was accidental?).

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form



Stephan Br?nnimann
Guest
 
Posts: n/a
#4: Jul 22 '05

re: strdup in Borland C++Builder


"lallous" <lallous@lgwm.org> wrote in message news:<2qde7uFttarhU1@uni-berlin.de>...[color=blue]
> "Stefan Schwärzler" <stefan.sa.schwaerzler_usenet@bmw.de> wrote in message
> news:chrpdl$j8k1@usenet.bmw.de...[color=green]
> > Hi Ng,
> > habe nicht besonders viel Erfahrung in C und C++, deshalb:
> > möchte den befehl strdup in <string.h> verwenden.
> >
> > #include <string.h>
> > attrib(char *name, char *val) : name(strdup(name)), val(strdup(val)),
> > next(0) {
> > CDEBUG(printf("attrib::attrib(%s, %s)\n", name, val));
> > }
> >
> > Borland bringt dann die Meldung "undefinierte Funktion" 'strdup'
> > Müsste doch in string.h definiert sein. Oder macht hier Borland was
> > anderes.
> >
> > Danke für Eure Hilfe,
> >[/color]
>
> Apparently, Borland C++ doesn't define strdup(), simply write it as:
>
> char *strdup( const char *s )
> {
> char *dup = malloc(strlen(s) +1);
> return strcpy(dup, s);
> }[/color]

That's C, in C++ better use std::string or if you think
it's really needed provide a wrapper class to avoid
confusion of operator new[]/delete[] (C++) with malloc/free (C).

[snip]

Stephan Brönnimann
broeni@osb-systems.com
Open source rating and billing engine for communication networks.
lallous
Guest
 
Posts: n/a
#5: Jul 22 '05

re: strdup in Borland C++Builder


>[color=blue]
> The function "strdup" is not part of the C or C++ standards.
> It is common on UNIX however, and part of some related
> standards (e.g. http://tinyurl.com/46cp5 ).
> Its typical implementation will look like:
> char *strdup(const char *s)
> {
> size_t l = 1 + strlen(s);
> char* p = malloc(l);
> if( !! p ) memcpy( p, s, l );
> return p;
> }
>[/color]
Hello Ivan,

Why do you use "if (!!p)" instead of "if (p)" or "if (p != 0)"?

--
Elias
Ivan Vecerina
Guest
 
Posts: n/a
#6: Jul 22 '05

re: strdup in Borland C++Builder


"lallous" <lallous@lgwm.org> wrote in message
news:2qde7uFttarhU1@uni-berlin.de...[color=blue]
> char *strdup( const char *s )
> {
> char *dup = malloc(strlen(s) +1);
> return strcpy(dup, s);
> }[/color]
NB: it is wise to check for a NULL return value of malloc
before calling strcpy, to avoid undefined behavior.
(Even though nowadays, we tend to forget about out-of-memory
conditions on our desktop platforms... )

Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


Ivan Vecerina
Guest
 
Posts: n/a
#7: Jul 22 '05

re: strdup in Borland C++Builder


"lallous" <lallous@lgwm.org> wrote in message
news:3bf2b2cf.0409110709.799b8621@posting.google.c om...[color=blue][color=green]
> > char *strdup(const char *s)
> > {
> > size_t l = 1 + strlen(s);
> > char* p = malloc(l);
> > if( !! p ) memcpy( p, s, l );
> > return p;
> > }[/color][/color]
....[color=blue]
> Why do you use "if (!!p)" instead of "if (p)" or "if (p != 0)"?[/color]

That's really just a choice of style/notation.
"!!" is one of the ways to explicitly convert a value to a boolean.

Some like to enable compiler warnings when a non-boolean expression
is used within an if/while/...., so if(p) can be a problem.

if( p!=0 ) like if( p==0 ) are disliked by some because of the
risk of confusion/mistyping/... as if( p=0 ) .
This is why some will write if( 0!=p ) and if( 0==p ) .
Also there is the debate about the use of NULL instead of 0...

I came upon the use of "!!" a few years ago in some code I was reading.
I found it disturbing at first sight, then I felt it was a convenient
notation, easily read as a "cast-to-bool" operator.
It has become a habit of mine...


Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form


Closed Thread