strdup in Borland C++Builder

S

Stefan Schwärzler

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
 
L

lallous

Stefan Schwärzler said:
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,

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.
 
I

Ivan Vecerina

Stefan Schwärzler said:
Hi Ng, ....
#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.

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
 
S

Stephan Br?nnimann

lallous said:
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);
}

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
(e-mail address removed)
Open source rating and billing engine for communication networks.
 
L

lallous

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;
}
Hello Ivan,

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

Ivan Vecerina

lallous said:
char *strdup( const char *s )
{
char *dup = malloc(strlen(s) +1);
return strcpy(dup, s);
}
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
 
I

Ivan Vecerina

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

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
 

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

Similar Threads

ICQ like 0
Post-Daten aus HttpSendReuqest auslesen 1

Members online

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top