Information Technology Solutions, Answers and Experts
Write an Article Ask a Question

Can I use delete with malloc'ed items?

Woodster
P: n/a
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
Jul 19 '05 #1

3 Replies



Jerry Coffin
P: n/a
Jerry Coffin

re: Can I use delete with malloc'ed items?

In article <MPG.1a034610a6f41385989680@news.westnet.com.au> , mirror@
127.0.0.1 says...[color=blue]
> I knoew that free is usually paired with malloc and delete is usually
> paired with malloc, alloc or similar.[/color]

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

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.
[color=blue]
> 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).[/color]

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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2

Woodster
P: n/a
Woodster

re: Can I use delete with malloc'ed items?

> strdup isn't standard, so it's impossible to say for sure, but every[color=blue]
> 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.[/color]

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
Jul 19 '05 #3

Gianni Mariani
P: n/a
Gianni Mariani

re: Can I use delete with malloc'ed items?

Woodster wrote:[color=blue]
> 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).[/color]

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.

Jul 19 '05 #4

Post your reply

Sign in to post your reply or Sign up for a free account.



Didn't find the answer to your question? Post your C / C++ question on Bytes

You can also browse similar questions: C / C++ malloc delete

Get C / C++ Help

Get C / C++ help from a network of professionals.

Post your Question » Over 331,212 Members | 3378 Online