Can you allocate the memory using malloc() in C and deallocate the same memory using free() in c++

Showing Answers 1 - 9 of 9 Answers

Jigar Mehta

  • May 29th, 2006
 

No, we should not do that. if we do that we can have un-defined or un-predictable results. Which can corrupt other memory too. Instead, we should make a simple rule not to do that.

  Was this answer useful?  Yes

BKumar

  • Jul 26th, 2006
 

The answer is - Yes, should you do it - NO, result - unpredictable behaviour, corruption of heap.The problem is that the memory gets allocated as a chunk of memory which is equivalent to the size of the class (members, functions etc), as is the case of a struct. This follows the C style of memory allocation, rather in C++ the base class is first constructed and then the derived classes till your object is created when new is used.Using free on memory created using one of the "lloc" functions will try to perform a C++ deletion rather which is destructors of the derived classes called till the base class is destroyed.This will lead to as expected -- unexpected behaviour.

  Was this answer useful?  Yes

VK

  • Aug 30th, 2006
 

Both the above answers are completely wrong. malloc() and free() are calls to a C library. There is *no* difference between calling them from C or C++. No heap corruption, or destructor calls, or unpredictable results will occur.

jagdish

  • Sep 15th, 2006
 

We allocated a memory using malloc() this is c library function. now we deallocate using free() it also c function. then no problem the memory is realse.

jay

  • Sep 17th, 2006
 

It's completely legal to use malloc and free, in fact that's the proper way to handle the heap in ANSI C. The only memory allocation conflict may occur while using both :alloc/calloc/malloc to allocate and delete to deallocate, ornew operator and free() functionfor the same chunk of memory.(Mixing C and C++ (any) memory handling functions is a deadly sin, since, among other reasons,although C++'s new and delete MAY internaly use malloc and free, allocating/deallocating with C functions explicitly do not cause class constructor and destructors to be called)

  Was this answer useful?  Yes

Nihar

  • Sep 18th, 2006
 

Yes , you can do it. But you should not do it. This is "Bad De-allocation" a security flaw. The way ?new? allocates and Delete ?de-allocates? are completely different from malloc and free. Program will show abnormal behavior it has been observed.

  Was this answer useful?  Yes

Guest

  • Jan 5th, 2007
 

This is a trick question. Should you allocate memory using malloc or new is a question that is only pertinent to C++. So if you allocate memory using malloc in C++ then you should be using free to deallocate. If you use new then you should use delete. However in C stick you have to stick to malloc and free.

If you are using a C library which allocates some memory for you and returns the pointer address - you should use the C library function to deallocate. Most libraries do do this.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions