GeekInterview.com
Series: Subject: Topic:
Question: 184 of 267

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

Asked by: Interview Candidate | Asked on: May 25th, 2006
Showing Answers 1 - 7 of 7 Answers
Jigar Mehta

Answered On : 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.

  
Login to rate this answer.
BKumar

Answered On : 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.

  
Login to rate this answer.
VK

Answered On : 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
jagdish

Answered On : 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
jay

Answered On : 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)

  
Login to rate this answer.
Nihar

Answered On : 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.

  
Login to rate this answer.
Guest

Answered On : 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Ads

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.