FAQ heaps

This is a discussion on FAQ heaps within the FAQ Board forums, part of the General Programming Boards category; I do not understand what heaps are. what are used for and why they are used. After searching everywhere , ...

  1. #1
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052

    Thumbs up heaps

    I do not understand what heaps are. what are used for and why they are used. After searching everywhere, all that I could find out is that a heap has something to do with memory (eg. 0x1256...or some garbage like that).

    Thanks
    -Chris

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    The heap is reserved for dynamic memory allocation needs of the program. It is an area apart from the program code and the stack. Typical C programs use the functions malloc and free to allocate and deallocate heap memory. C++ uses the new and delete operator. The new operatroe allocates the memory then calls the constructor to initialize it. The delete operator does all the necessary (hopefully) cleanup then the memory is made availible (deallocated) for reuse on the heap.

    To allocate an array of bytes on the stack

    The array is automatically deleted and its memory reclaimed when the array variable exits its scope.
    {
    const int BUFF_SIZE = 128;

    // Allocate on the frame
    char myCharArray[BUFF_SIZE];
    int myIntArray[BUFF_SIZE];
    // Reclaimed when exiting scope
    }

    To allocate an array of bytes (or any primitive data type) on the heap

    Use the new operator with the array syntax shown in this example:
    const int BUFF_SIZE = 128;

    // Allocate on the heap
    char* myCharArray = new char[BUFF_SIZE];
    int* myIntArray = new int[BUFF_SIZE];

    //Reclaimed with delete
    delete [] myCharArray;
    delete [] myIntArray;

  3. #3
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Or if you after a more general definition of a heap data structure then look here
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. Redirect FAQ...
    By Queatrix in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 05-21-2007, 04:48 PM
  3. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21