CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76

    Doubt on HeapAlloc

    hi,
    I have a doubt on HeapFree Function.

    We do the heap allocation like this.
    pV = HeapAlloc(Mem, HEAP_ZERO_MEMORY , ulSize);

    When we do the heap free
    HeapFree(Mem, 0, pV);


    does this freeing memory has by any chance corrupt the memeory. What actually happens when the HeapFree function in called? is the the memory that is released by this HeapFree can be reused till the Heap is destroyed?

    Thanks in Advance,
    Varadha

  2. #2
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Doubt on HeapAlloc

    Originally posted by Varadha
    does this freeing memory has by any chance corrupt the memeory.
    What do you mean by "corrupt the memory"? Are you asking if the heap (generally) will become corrupted or if the freed memory becomes corrupted? The heap itself shouldn't get corrupted but the freed memory shouldn't be reused until it has been reallocated.

  3. #3
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443

    Re: Doubt on HeapAlloc

    Originally posted by Varadha
    is the the memory that is released by this HeapFree can be reused till the Heap is destroyed?
    No. Once you have freed a memory block, consider it "invalid". This doesn't apply only to the HeapXxx functions, but to all memory allocation mechanisms. Accessing freed memory yields in undefined behaviour and is a no-no in all circumstancies.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  4. #4
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    I am allocationg the memory in the HeapAlloc function. If i call HeapDestroy fo the heap without freeing the memory by HeapFree is this causes any problem.

  5. #5
    Join Date
    Sep 2003
    Location
    Chennai, India
    Posts
    76
    I am allocationg memory using the HeapAlloc function. If i call HeapDestroy before freeing the heap created using HeapFree function, can any error may arise due to this.

    i.e
    hHeap = HeapCreate(0, 1000000, 0);
    pV = HeapAlloc(hHeap, HEAP_ZERO_MEMORY , ulSize);
    HeapDestroy(hHeap);

    here before freeing pV, we are Destroying the Heap. Is this causes any problem.

  6. #6
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    http://msdn.microsoft.com/library/de...eapdestroy.asp

    Remarks
    Processes can call HeapDestroy without first calling the HeapFree function to free memory allocated from the heap.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured