C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 10-06-2001, 03:40 AM   #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
__________________
Please direct all complaints regarding this post to the nearest brick wall Have a nice day.
face_master is offline  
Old 10-06-2001, 09:31 AM   #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;
Dang is offline  
Old 10-06-2001, 11:21 AM   #3
zen
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
zen is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Wiki FAQ dwks General Discussions 192 04-29-2008 01:17 PM
Redirect FAQ... Queatrix General Discussions 21 05-21-2007 04:48 PM
FAQ Check/Lock RoD A Brief History of Cprogramming.com 2 10-15-2002 11:21 AM


All times are GMT -6. The time now is 01:49 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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