473,414 Members | 1,599 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,414 software developers and data experts.

{malloc, new} and {free, delete}

when i create an array through malloc, i use free to free it. If i create the same array using new operator (e.g.) int a[] = new int(20); then i have to user 'delete[] a'. But i don't use '[]' in free system call. Please clear my doubt and if some one suggest me a book that explain details like this stuf? I don't want some internals details like hoem memory manager is workibng. A bok that just explain me the concept of how this is done?
Aug 6 '07 #1
1 1761
JosAH
11,448 Expert 8TB
when i create an array through malloc, i use free to free it. If i create the same array using new operator (e.g.) int a[] = new int(20); then i have to user 'delete[] a'. But i don't use '[]' in free system call. Please clear my doubt and if some one suggest me a book that explain details like this stuf? I don't want some internals details like hoem memory manager is workibng. A bok that just explain me the concept of how this is done?
The free() function doesn't care what you free; any non-null pointer will be free'd
if it has been malloc'd before; even more, free() doesn't care what the content
of the memory pointed to by that pointer contains. If it contains a last reference
(pointer) to some other memory: so be it; you've got yourself a memory leak then.

Delete does a lot more but it has to know whether or not the pointer you supply
it is a pointer to just one object or possibly more object. You tell that to the
delete operator by supplying those square brackets. They need to pair up with
your new operator, i.e. if you new'd an array, you should delete [] an array.

kind regards,

Jos
Aug 6 '07 #2

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

Similar topics

3
by: Woodster | last post by:
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....
4
by: Lost Bits | last post by:
Hello there C++ pros.. Well, I have recently redesigned an already existing piece of code that someone else designed - its just a search algorithm they developed, and has to perfomr millions of...
21
by: Jeff | last post by:
Hi, i'm trying to use new/delete in C++ instead of malloc/free. I have this : int g_TTable_size=130000; // for example struct hash_t { int depth; int flags;
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
13
by: Richard | last post by:
vector<char*> m_Text; m_Text.resize(1); char* foo = "FOO"; char* bar = "BAR"; char* foobar = (char*)malloc(strlen(foo) + strlen(bar) + 1); if (foobar) { strcpy(foobar, foo); strcat(foobar,...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
48
by: avasilev | last post by:
Hi all, my question is: if i allocate some memory with malloc() and later free it (using free()), is there a possibility that a consequent malloc() will allocate memort at the same starting...
5
by: Amit_Basnak | last post by:
Dear Friends Currently we are allocating memory in our code to a Structure with malloc() function and deallocaing using free() WF_WU_HANDLE * lclStrWorkunit =...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.