473,785 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13623
In article <MP************ ************@ne ws.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
443
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 = 0
14
8484
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
1946
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: Code is doing malloc of variable sizes. The last byte of malloc.ed memory is written a magic.
33
2977
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 *reverse(char *s) { int i; char *r; if(!s) return NULL;//ERROR r=calloc(strlen(s)+1,sizeof(char));
5
2767
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
1253
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 plug-ins. Please note that what I'm importing into Python are the plug-in classes in order to be able to instanciate as many objects as needed. As usual, all the plug-ins derive from a base class. But two important things differ for each...
3
4889
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 code. It's basically a singly linked list of strings that are concatenated into a big string. My question is - since you have to know to free it yourself (usage
2
1580
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 later stage? BTW, I thought this would be a common question - so either I'm just plain ignorant (coming from a C# and Python background) or my google skills need to improve.
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10356
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.