GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 83 of 208    Print  
can you allocate the memory using malloc() in C and deallocate the same memory using free() in c++

  
Total Answers and Comments: 7 Last Update: January 05, 2007     Asked by: prabhakar pillai 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: VK
 
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.

Above answer was rated as good by the following members:
yzesong
May 29, 2006 14:00:39   
Jigar Mehta        

RE: can you allocate the memory using malloc() in C an...
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.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 26, 2006 17:18:46   
BKumar        

RE: can you allocate the memory using malloc() in C an...
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.
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
August 30, 2006 06:40:23   
VK        

RE: can you allocate the memory using malloc() in C an...
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.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 15, 2006 08:35:21   
jagdish        

RE: can you allocate the memory using malloc() in C an...

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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 17, 2006 12:27:00   
jay        

yes you can
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)
 
Is this answer useful? Yes | No
September 18, 2006 09:39:24   
Nihar        

RE: can you allocate the memory using malloc() in C an...
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.
 
Is this answer useful? Yes | No
January 05, 2007 06:44:04   
       

RE: can you allocate the memory using malloc() in C an...

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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2010 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape