strdup in c++

A

Allan Bruce

Hi there,

I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];

e.g.

char *NewCharArray;

NewCharArray = strdup("Some text");

delete []NewCharArray;


Nybody have any ideas?
Thanks
Allan
 
R

Ron Natalie

Allan Bruce said:
Hi there,

I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];

e.g.

char *NewCharArray;

NewCharArray = strdup("Some text");

delete []NewCharArray;


Nybody have any ideas?

std::string NewString;

NewString = "SomeText";

// delete not required.
 
A

Attila Feher

Allan said:
Hi there,

I have a program written in c++ and I wish to use a similar function
to strdup().
The reason I have problems is that the char array requires freeing to
avoid a memory leak, but I get problems using delete[];

e.g.

char *NewCharArray;

NewCharArray = strdup("Some text");

delete []NewCharArray;


Nybody have any ideas?

Nybody must be some Scandinavian guy :)

Use std::string instead of fiddling with those arrays.
 
B

Buster

Allan Bruce said:
I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];

You have to use free:

#include <cstdlib>
#include <cstring>
// ...
char * NewCharArray = strdup ("Hi there");
// std::strdup doesn't compile on my machine
// (macro?). Good job I never use it.
std::free (NewCharArray);

But don't do that. Use the standard string class.

Regards,
Buster
 
J

John Ericson

Buster said:
Allan Bruce said:
I have a program written in c++ and I wish to use a similar function to
strdup().
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];

You have to use free:

<snip>

<OT> strdup( ) isn't even a standard C or C++ function.
Whether you should use free or delete[] depends on what the
particular compiler vendor's strdup( ) used to allocate the
memory </OT>
 
B

Buster

John Ericson said:
<OT> strdup( ) isn't even a standard C or C++ function.
Whether you should use free or delete[] depends on what the
particular compiler vendor's strdup( ) used to allocate the
memory </OT>

I'm learning a lot today. Thanks.
 
R

Ron Natalie

Buster said:
// ...
char * NewCharArray = strdup ("Hi there");
// std::strdup doesn't compile on my machine
// (macro?). Good job I never use it.

That's because there is no std::strdup. strdup
is not a standard C or C++ function.
 
A

Andre Kostur

Hi there,

I have a program written in c++ and I wish to use a similar function
to strdup().
The reason I have problems is that the char array requires freeing to
avoid a memory leak, but I get problems using delete[];

e.g.

char *NewCharArray;

NewCharArray = strdup("Some text");

delete []NewCharArray;


Nybody have any ideas?

Two ideas:

1) As others have mentioned, use std::string in preference (generally) to
char*'s.

2) Remember to always delete[] what you new[], and to free() what you
malloc() (or calloc(), or strdup(), or other functions which are
documented to requrire free()ing of the memory obtained). Under the
covers, strdup() effectively calls malloc() (I don't know if this is
strictly mandated). So, in order to correctly dispose of the memory
obtained by strdup(), your code should be:

char * NewCharArray = strdup("Some text");

free(NewCharArray);

(Depressingly, I had to look up whether free needed the parens, or does
it get called like delete... It's been so long since I've used
malloc/free.... :) )
 
G

Gianni Mariani

John said:
similar function to

requires freeing to avoid
a memory leak, but I get problems using delete[];

You have to use free:


<snip>

<OT> strdup( ) isn't even a standard C or C++ function.
Whether you should use free or delete[] depends on what the
particular compiler vendor's strdup( ) used to allocate the
memory </OT>

SMACK the supplier over the head if they don't use malloc/free.

Use free() to release memory allocated by strdup().

If you're using C++ you'd be far better off using std::string.
 
M

Mike Wahler

Allan Bruce said:
Hi there,

I have a program written in c++ and I wish to use a similar function to
strdup().

Note that 'strdup()' is not part of standard C++ (or C).
The reason I have problems is that the char array requires freeing to avoid
a memory leak, but I get problems using delete[];

IMO the reason you're having problems is that you're using
arrays of characters (with all their traps and pitfalls) and
dynamic allocation (with its possible problems) when you should
be using the 'std::string' type to represent strings.

std::string my_strdup(const std::string& s)
{
return s;
}

void foo()
{
std::string x(my_strdup("abc"));
}

But doing this is silly, since the same effect can be
achieved with simpler (and probably more efficient)
code:

std::string s1("abc"); /* 'duplicates' "abc" in the string 's1' */

std::string s2(s1); /* 'duplicates' 's1' contents in 's2' */

s1 = s2; /* 'duplicates' 's2' contents in 's1' */

/* etc */
e.g.

char *NewCharArray;

This name is misleading. 'NewCharArray' is not an array,
it's a pointer.
NewCharArray = strdup("Some text");

Why not simply:

char NewCharArray[] = {"Some text"};
delete []NewCharArray;

Then you don't need this.
Nybody have any ideas?

Yes, stop trying to use C++ to write C code. :)

IF you need a string, the C++ library has a very
powerful and easy to use string type which is much
safer than arrays of characters and messing with
allocation and deallocation.

-Mike
 
M

Mike Wahler

Andre Kostur said:
Hi there,

I have a program written in c++ and I wish to use a similar function
to strdup().
The reason I have problems is that the char array requires freeing to
avoid a memory leak, but I get problems using delete[];

e.g.

char *NewCharArray;

NewCharArray = strdup("Some text");

delete []NewCharArray;


Nybody have any ideas?

Two ideas:

1) As others have mentioned, use std::string in preference (generally) to
char*'s.

2) Remember to always delete[] what you new[], and to free() what you
malloc() (or calloc(), or strdup(), or other functions which are
documented to requrire free()ing of the memory obtained). Under the
covers, strdup() effectively calls malloc() (I don't know if this is
strictly mandated).

Of course not, since it's not a standard function. One should
follow its documentation for correct usage. This 'correct usage'
could be different among implementations.
So, in order to correctly dispose of the memory
obtained by strdup(), your code should be:

char * NewCharArray = strdup("Some text");

free(NewCharArray);

Only if the particular implementation of 'strdup()' being
used documents to do it that way.
(Depressingly, I had to look up whether free needed the parens, or does
it get called like delete... It's been so long since I've used
malloc/free.... :) )

malloc and free are functions, so argument list must always
be enclosed in parens:

p = malloc(whatever);
free(p);

new, new[], delete, and delete[] are operators, and do
not need parens.

p = new int;
delete p;

-Mike
 
R

Ron Natalie

Gianni Mariani said:
<OT> strdup( ) isn't even a standard C or C++ function.
Whether you should use free or delete[] depends on what the
particular compiler vendor's strdup( ) used to allocate the
memory </OT>

SMACK the supplier over the head if they don't use malloc/free.

They have to supply malloc / free. There's no telling what the
function strdup() requires because there's no such function in
standard C or C++.
 
A

Andre Kostur

Note that 'strdup()' is not part of standard C++ (or C).

Huh. Hadn't realized that. Only that every implementation I've used has
had a strdup.... oh well. Answer rephrase: check the documentation on your
particular implementation of strdup(). All of the implementations I've
used, the memory returned by strdup() needs to be free()'d. YMMV
 
G

Gianni Mariani

Ron said:
<OT> strdup( ) isn't even a standard C or C++ function.
Whether you should use free or delete[] depends on what the
particular compiler vendor's strdup( ) used to allocate the
memory </OT>

SMACK the supplier over the head if they don't use malloc/free.


They have to supply malloc / free. There's no telling what the
function strdup() requires because there's no such function in
standard C or C++.

Sure there is. It's in every man page for strdup.
 
W

White Wolf

Gianni said:
Sure there is. It's in every man page for strdup.

Man pages do not define the C or the C++ language. The international
standards do. And they do not contain strdup. So there is no strdup in
standard C or C++.
 
K

Kevin Goodsell

Gianni said:
Sure there is. It's in every man page for strdup.

I assume you mean "Sure there is a way of telling what strdup()
requires", not "Sure there is such a function in standard C or C++."
From the point of view of either language, there is no way to to tell,
since the function is outside the scope of the languages (and this group).

Technically, the function itself is illegal in C because it uses a
reserved name ('str' followed by a lowercase letter - reserved for
future standard library use). I don't know if this applies in C++, where
future library functions may be in std:: rather than the global namespace.

-Kevin
 
G

Gianni Mariani

White said:
Man pages do not define the C or the C++ language. The international
standards do. And they do not contain strdup. So there is no strdup in
standard C or C++.

There is a clear definition of what strdup should do. If you change
that all code that uses it breaks. It's highly unlikely that anyone
will do anything different. It's a de-facto standard.

For the longest time C was a defacto standard. In fact even now, C was
for a longer time NOT a standard that it is a standard without any help
from a standards body.

Standards are great, we have so many of them !

So what if there is no international standard to cite. If you write
somthing that breaks everyone's code, you'll need a smack in the head too.
 
W

White Wolf

Gianni said:
There is a clear definition of what strdup should do.

There is a clear definition of what strdup does on that system you were
looking at. There is no clear definiton of what strdup does in C or C++,
except that it is illegaly named.
If you change
that all code that uses it breaks.

If I change the function foo() all the code what uses it will break. That
does not make it standard C or C++ function.
It's highly unlikely that anyone
will do anything different. It's a de-facto standard.

Probably. But if you look at the name of this newsgroup, it is not
comp.lang.de-facto.c++
For the longest time C was a defacto standard. In fact even now, C
was for a longer time NOT a standard that it is a standard without
any help from a standards body.

Really? I remember those times. And I do not want them back.
Standards are great, we have so many of them !

This is irrelevant here. There is one C++ standard.
So what if there is no international standard to cite.

Irrelevant. There is one.
If you write somthing that breaks everyone's code,
you'll need a smack in the head

Irrelevant. You have stated that strdup is standard C++ and it is not. The
rest is irrelevant.
 
P

Phlip

If you write somthing that breaks everyone's code,
At the risk of taking this fun to a higher level...

....Let's remember why we are here. C++ exists to permit scalable yet
efficient projects. For the latter cause, the compiler frequently makes you
work harder than other languages would.

To become better than just dangerous at C++, you must learn the paths thru
its mine-field; a sane-subset.

Much rhetoric on this newsgroup derives from the attitude that one must know
exactly< which statements are legal, which are truly portable, which
should< be portable, and which you can get away with.

This newsgroup's greatest service is teaching that attitude.
 
R

Ron Natalie

Gianni Mariani said:
Sure there is. It's in every man page for strdup.
What is in the man page for strdup? Despite whatever you are
looking at, strdup is not addressed by any C or C++ standard.
 

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,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top