Can I use delete with malloc'ed items?

W

Woodster

I knoew that free is usually paired with malloc and delete is usually
paired with malloc, alloc or similar.

Can I use delete with malloc? The main reason I ask is for using the
strdup function. If I have a char *, I have no real way of knowing
whether that char * had memory allocated for it using new or malloc (as
used by strdup).

Thanks in advance

Woodster
 
J

Jerry Coffin

mirror@ said:
I knoew that free is usually paired with malloc and delete is usually
paired with malloc, alloc or similar.

No -- free is paired with malloc or calloc. delete is paired with new,
and delete[] with new[].
Can I use delete with malloc?

If you do so, you'll get undefined behavior. Experience indicates that
this particular undefined behavior will NOT be innocuous either -- it
will lead to nasty misbehavior in a lot of cases.
The main reason I ask is for using the
strdup function. If I have a char *, I have no real way of knowing
whether that char * had memory allocated for it using new or malloc (as
used by strdup).

strdup isn't standard, so it's impossible to say for sure, but every
version of it I've seen has used malloc.

Having said that, I feel obliged to question using strdup in C++ at all.
Use std::string, and it handles these sorts of things automatically.
 
W

Woodster

strdup isn't standard, so it's impossible to say for sure, but every
version of it I've seen has used malloc.

Having said that, I feel obliged to question using strdup in C++ at all.
Use std::string, and it handles these sorts of things automatically.

Being completely self taught in C++ from quite some time ago I never got
around to using or learning templates, and still use my own list
functions and all that where I probably should be using templates.

The main reason why I was asking is that I have my own function that I
use a bit called strnew which performs the exact same function as strdup
and I was trying to find out if it was really necessary to have my own
function or if I could get away using strdup.

Thanks for the information.

Woodster
 
G

Gianni Mariani

Woodster said:
I knoew that free is usually paired with malloc and delete is usually
paired with malloc, alloc or similar.

Can I use delete with malloc? The main reason I ask is for using the
strdup function. If I have a char *, I have no real way of knowing
whether that char * had memory allocated for it using new or malloc (as
used by strdup).

You will need to keep track or write your own strdup.


char * strdup( const char * in )
{
int len = std::strlen( in );
char * ret_str = new char[ len + 1 ];
std::memcpy( ret_str, in, len + 1 );
return ret_str;
}

....

but then while you're at it, start using std::string instead.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,371
Latest member
Brkaa

Latest Threads

Top