Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

what is difference between MALLOC & NEW, FREE & DELE

Status
Not open for further replies.

sacrpio

Member level 3
Member level 3
Joined
May 24, 2004
Messages
56
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
500
hello,
Please tell me what are the differences between malloc & new, delete & free

bye..
 

Re: what is difference between MALLOC & NEW, FREE &

Malloc => Use a current variable and asign a diffrent size memory allocation for it.
New => Create a completely new memory allocation for that varible.
Delete => Delete the current memory allocation and variable index. (This destroys the variable completly. Can't use it ever again.)
Free => Free the memory assigned to the variable but leave the variable index.

Malloc and Free work together and New and Delete works together.

:sm35:
 

Re: what is difference between MALLOC & NEW, FREE &

That ain't correct!

malloc does memory allocation and returns a pointer to the allocated memory.
malloc searches the available free memory pool for a place which can hold the requested number of bytes, allocates it (remove it from the free part of the memory and keep some kind of index). (so Frikki's explanation is wrong!!!)

free frees the memory at the specified adres. It is possible that the pointer still points to the memory and that the correct data is still in that place!

Code:
int * intArray;
// Allocate 10 int variables in a continuous memory section
intArray = malloc(10*sizeof(int)); 

// Free the memory section again (so you don't have memory leaks)
free(intArray);

These are allocation and deallocation the memory, not much intelligence behind it.
In uC you can write it yourself, in an operating system they are probably provided by the memorymanger of the OS.

malloc and free can be used in C and in C++ (although I would try to avoid it as much as possible in C++)

new and delete are C++ keywords and cannot be used in C.
new is similar to the malloc but it does more. You could see new as a wrapper function around malloc. It allocates the memory needed for the object and calls the appropriate constructor (so the object is initialized as needed)

delete is similar to free but it does more! The destructor is called so you can clean up everything.

Antharax
 

If you program in C++ new and delete. If C then malloc and free (at least)
 

If you write and compile a small program in C++ using the microsoft or borland compilers, which declares a simple array, (int *I = new int[20];) and then decompile it and look at the listings, with slight variations between compilers, you will find that new calls malloc to allocate the memory from the heap, and delete calls free to release the memory. (and why not, they are time tested, solid routines?)
What new and delete do, is GARANTEE, to return or destroy, a pointer of the correct type. As C++ tries to be a strongly typed language.
In C, you have to typecast, with sizeof, the returned pointer type when using malloc, as malloc returns a void pointer.
 

Re: what is difference between MALLOC & NEW, FREE &

Yes, that means new is equivalent to malloc with some extra's (initialization of members, overloading, ...) ;-)
The book "Object Oriented Programming With ANSI C" is interesting to read here. (https://www.planetpdf.com/codecuts/pdfs/ooc.pdf)

But it doesn't mean C++ is translated to that C code before it is compiled ;-).
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top