Results 1 to 4 of 4

Thread: Why free is used to deallocate memory

  1. #1
    Junior Member
    Join Date
    Apr 2007
    Answers
    1

    Why free is used to deallocate memory

    When we allocate a memory by new why free is used to deallocate it why can't we use delete?
    malloc delete
    new free

    why??


  2. #2
    Junior Member
    Join Date
    May 2007
    Answers
    4

    Re: Why free is used to deallocate memory

    I think you are under wrong impression. We should use 'free' if we use 'malloc' to allocate memory. And we should use 'delete' if we use 'new' to allocate the memory.
    Moreever, in C++ you should be using new/delete always.


  3. #3

    Re: Why free is used to deallocate memory

    Yes, Rahul it correct... We need to use free to deallocate malloc memory and delete to dealocate New memory, otherwise it will lead it memory leaks... If we use New & delete the memory will be allocated & deallocated over constructors & destructors respectively...


  4. #4

    Question Re: Why free is used to deallocate memory

    Quote Originally Posted by Rahul C. View Post
    I think you are under wrong impression. We should use 'free' if we use 'malloc' to allocate memory. And we should use 'delete' if we use 'new' to allocate the memory.
    Moreever, in C++ you should be using new/delete always.
    Hi Rahul, I am a new member of the group. I have a small doubt, what happens if we allocate memory using 'malloc' and deallocate memory using 'delete'. Is tihis possible..?


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact