 |
What is, and what should we use, memory allocation for? Two questions that
we should try to clear out today.
What is Memory allocation
Memory allocation, or malloc() as the function is called, is a way to give a variable
some memory to use. As we all know, different variables uses different much memory (for example an int takes up 2 byte memory).
If you have a program, and then add an integer (int my_var;), the program will be 2 bytes bigger. Nothing strange by that, the integer will
take up it's two bytes, and then be happy with that. Why the hell should we then use memory allocation when the Compiler allocates memory for us?
Why should we use Memory allocation
A good example what you could use memory allocation is if you're using large arrays.
Let's say you have an array holding information, but you're not sure how big the array has to be, or how many 'cells' of the array the user will use.
In this case you can either make a very big array (or matrix), let's say you want to store stock market prizes for each day.
You don't know how many days the user want to store, so you think that 1000 days have to me enought (would be
really bad overflowing the array), and we're using integers, so that would be something like this:
int stock_prize[1000];
Now that looks okay, right. But if we look at the memory we're using now. Each int takes 2 byte of memory, and now we have 1000 ints, so that will take up 2000byte memory (or 2 Kbyte).
It will take up this memory, even if the user only uses 10 days (or 10 of those 1000 blocks). If we're having
smaller arrays or matrixes, this isn't that bad, but still, we have a hudge memory leakage. What we want to do is to have something like this:
int stock_prize[n];
and then just change n while the program is running. This doesn't work exactly this way, which should be good if it did, but because of the memory handling it doesnt. Instead, we have to make
a pointer (a pointer is a 'variable' that points to the used memory), and we do that like this:
int *stock_prize;
Note that we have not defined it as an array, and we don't need to do that, but before we can use this variable, we
need to allocate memory for it, and we do that will malloc();
stock_prize=malloc(sizeof(*stock_prize));
Now, we have given stock_prize variable some memory to use, or actualy we have given stock_prize the memory of the size of an int (got it ;). What i mean is that we have given stock_prize (which is an int) the memory an int would use (2 bytes).
sizeof(*stock_prize) returns 2, because stock_prize is an integer and the size of an integer is 2 byte (using sizeof is great if you're using structures). A note here is that sizeof(?)
is pre-defined by the compiler, so even if we give stock_prize 20 byte to use, sizeof(*stock_prize) will still return 2 byte!
Ok, so we have given stock_prize 2 bytes of memory to use. That means, that we have 1 integer to use (remember 1 integer = 2 byte), this mean that
we can use stock_prize[0] for something. If we want to make a new block, so we can use stock_prize[1] and so on
then we have to reallocate memory for stock_prize (re-allocate memory is done with realloc() function).
Then it will look something like this:
stock_prize=realloc(stock_prize,2*sizeof(*stock_prize));
This will give us 2*2 bytes to use, which means 2 * integer, and now we can use stock_prize[1] for something.
You can now change that 2 in the realloc call to a variable, like 'cell_num', and then just change cell_num to 2,3 or how many
cells you want to use. You can also change the memory usage the other way around, if you have allocated 32 bytes of memory (16 blocks)
and the user deletes one block, then you can reallocate 30 bytes of memory (15 blocks).
Don't forgett to add free(stock_prize); at the end of your program, to free the memory stock_prize have used,
or else you will have a memory leakage here
Here's a small programing example how the allocation works:
// Global variable
int *stock_prize;
int num_cells; // keep track of how many blocks we have
void add_cell(void)
{
num_cells++;
stock_prize=realloc(stock_prize,num_cells*sizeof(*stock_prize));
}
// Main Function
void _main(void)
{
clrscr();
num_cells=0;
//Let's give it some memory
stock_prize=malloc(sizeof(*stock_prize)); // 1 cell, automaticly
while (num_cells<7)
{
add_cell();
stock_prize[num_cells-1] = num_cells; // because num_cells = 1 when you can use stock_prize[0]
printf("%d\n",stock_prize[num_cells-1]); // Print the info
}
ngetchx();
// Free up the memory again
free(stock_prize);
}
|
If you have any question about this tutorial, be sure to drop me a mail
|
| |