473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Can I use delete with malloc'ed items?

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 13596
In article <MP************************@news.westnet.com.au> , mirror@
127.0.0.1 says...
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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 19 '05 #2
> 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
Jul 19 '05 #3
Woodster wrote:
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.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Simon Nickerson | last post by:
I have a function which looks like this: void rho(matrix_t *out, matrix_t *in) { static int firsttime = 1; static matrix_t *words; /* ... other variables ... */ if (firsttime) { firsttime...
14
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
11
by: Sushil | last post by:
Hi Gurus I've tried to come up with a small logical example of my problem. The problem is platform specific (MIPS) which I understand should not be discussed here. So here goes my example: ...
33
by: apropo | last post by:
what is wrong with this code? someone told me there is a BAD practice with that strlen in the for loop, but i don't get it exactly. Could anyone explain me in plain english,please? char...
5
by: Bidule | last post by:
Hi, I'm trying to sort structs defined as follows: struct combinationRec { float score; char* name; }; The number of structs and the length of the "name" field are not known
0
by: Gabriel de Dietrich | last post by:
Hi, I'm doing my first project on embedding and then extending Python in an application. The idea is to import a set of C++ plug-ins into Python and then be able to run a script that uses these...
3
by: Khookie | last post by:
Hi everyone I'm not sure whether this is good practice or not, so I thought I'll ask. I've got a function that returns a char pointer, which is malloc'ed in the function itself, as per below...
2
by: Khookie | last post by:
Hi all I was wondering whether there was a way to know whether a pointer (in my case, I'm using a char pointer) has been allocated memory through malloc or not, thus needing to be freed at a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.