C Notes

The following are notes on C and ideas and concepts that are particular to C:

Malloc:

Memory can be thought of in two ways. Stacks and Heaps. The stack can be thought of the memory set aside. The stack itself is always reserved in LIFO order, i.e., Last In First Out order. The block that is most recently reserved is always the next block to be freed. So a block is reserved on the top of the stack for local variables and such when a function is called. When the function returns, the block returns to being unused until the next time a function is called.

 Freeing the block from the stack is only as much as adjusting a pointer. The heap on the other hand, is a memory set aside for dynamic memory allocation. There's no enforced pattern for allocation and de-allocation. You can set and free the block at any time. This makes it much harder than the stack because the stack has LIFO order, so it can be tracked easily. Malloc is an example of allocation of dynamic memory allocation

Malloc:
Suppose you needed to allocate a certain block of memory from the memory heap during the execution of a certain program. Then you could use the malloc function. The malloc function requests a certain block of memory from the heap. When the memory is not required any further, then you use the free function. Malloc is a C library function so the fundamental syntax is: [void * malloc(size_t size)]. The size itself is the size of the memory block, in bytes.

Example: str = (char *) malloc(15);

Now where would you use Malloc and why?

Malloc is generally used for dynamic memory allocation. When you don't know the size of the allocation before the run time or during the compile time, it is better to use dynamic memory allocation because it allocates at run time.

Example: Suppose you were attempting to create a system of registered voters in the state of New Jersey. You'd have to collect their only their Voter IDs (which are just a singular number for the sake of argument), their name, and their age for the sake of simplicity. The easiest way to have this amount of data would be a hash table. But you don't know how many people are there in New Jersey. The population, in other words, is dynamic. So in this case, you'd use malloc to assign the voting population of New Jersey a memory block from the memory heap.

Important Note: "Mallocing" is not the only way to allocate memory. There exist variable length arrays.

So what's the difference?

The difference is in memory management. Malloc is dynamic memory management on the heap, whereas variable length array is on the stack. When using variable length array, you'd have to be very careful about freeing the memory. Also, the variable length array is only usable for the method in which it is in. It cannot be used outside of the method. On the other hand, the heap method does not need to be freed. It also can be used for large arrays because there is generally more memory in the heap than the stack


 

No comments:

Post a Comment