Advertisement

NEED HELP

Started by February 15, 2000 07:57 AM
3 comments, last by Pseudo_Code 25 years ago
Look, I don''t understand the concept of memory allocation. I have looked in every C++ book I can find, but all I find is "malloc reallocates memory from the heap, and free frees it. Moving on to constructors..." I don''t even know why I would ever need to allocate memory off of the heap. But apparently, it is needed. So if someone would PLEASE help me out and tell me what type of situation I would need it in, and explain how it helps, I would really, really appreciate it alot! Thank you for reading my post and I hope you respond helpfully.
malloc/free (or in C++, new/delete) is used when the size of data is unknown at compile time (i mean at the time u r writing the code).

Just imagine when u r playing a game. The number of units u can built is unknown when the programmers are coding. So it is best to dynamically allocate the memory using malloc/free or new/delete rather than a fix array. If u know link-list, then u should know memory allocation.

Anyway, memory allocation deals with pointer... so u need to know pointer very well first.
"after many years of singularity, i'm still searching on the event horizon"
Advertisement
Could you post up some code for that? I''m not sure I completely understand you.
A more basic message would be
While your program is compiling, all the declared variables like in the code below,
int ProgStart(void)
{
int x;
char string[25];
....
have memory set aside for them by the compiler in an area called the stack. That leaves a whole bunch of memory out there that you can use. It''s called a heap basically because it''s not very pre-organized memory like the stack.
When you call malloc(), you grab a chunk of memory from the available free memory(RAM)(it''s set aside for you ) and a pointer to that is sent back to you. You have to change that a pointer to the type of data you need with a cast.
CEMap[x][y]= (struct mapinfo *) malloc(sizeof(struct mapinfo));
In the above, malloc has taken XX number of bytes from the heap(sizeof() finds how many bytes) that can be used as a mapinfo structure. Just at this moment the data is all random junk and you need to initialize the mapinfo chunk of memory.
CEMap[x][y]->TileNumber = 0;
CEMap[x][y]->x = x;
CEMap[x][y]->y = y;
CEMap[x][y]->Unit = NULL;

struct mapinfo
{
int TileNumber;
int x, y;
void *pUnitPointer;
} *CEMAP[256][256]; // a 256x256 array of POINTERS _only_ that have no memory allocated to them

In this case you might not know the size of the map you are going to use but on the stack you''ve already got a 256x256 array of POINTERS available. So you can allocate a 30x40 array of structures when you load in your map from the hard-drive.

And when you''re done with that map you can free the memory back into the heap available for another map you''re loading in.
Let me see if I can clarify.

char name[64];

This will allocate 64 bytes for a character array called name. These bytes are set aside for the array at compile time. If you know how big your array needs to be, it is easier to do it this way.

int arraysize;
char *name;
...
name = (char*)malloc(arraysize * sizeof(char));
...
free(name);

In this case, name is declared as a pointer to a char. Pointers can be treated the same as arrays. The malloc() call will allocate a specified number of bytes (in this case arraysize bytes) from the heap and will return a pointer to it. This is handy if you don''t know how big your array needs to be at compile time. When you are finished using this chunk of memory, don''t forget to call free() to release it back to the heap.

If your array will vary in size or if you don''t know how big it needs to be, then you should allocate it dynamically. Otherwise it is usually easier to declare it statically.
Mike Weldon, a.k.a. FalloutBoymweldon@san.rr.com

This topic is closed to new replies.

Advertisement