I would like to know how memory allocation on the lowest levels works. For example if program wants to create some long array or anything else, how it will ask for the memory, how does the program ensure itself that it does not take memory same other program is using.
I would be very grateful if someone would bring some light for me into this matter.
Memory can be allocated in a number of ways…the two broad ways being static and dynamically allocated.
Static allocation means that all the memory that can be used by a program is allocated at once, and it is able to use up to that amount.
Dynamic allocation means that when the program needs to allocate memory, it goes to the heap and puts a pointer at the first available chunk of memory (specified in size according to the dynamic allocation algorith in use). Then, as it needs (such as in an array) it takes more, maintaining the pointer at that original spot so it knows where the beginning of the array is. Modern computers usually do a good job of allocating resources including memory to applications, which reduces the chance of deadlock. On a higher level, garbage collection takes care of this memory when the object/array/whatever can be removed from memory.
The problem here is that when free memory is given and freed at will, different programs can grab different chunks which aren’t necessarily in order. This is what we call fragmentation (which is why you defrag your disk drive every now and then). When memory is allocated in a contiguous fashion, it can be read more efficiently.
There’s a huge amount of information on memory, so here is a minute amount of data for you to allocate in your own memory 😉
Wiki Link to OSDev on Dynamic Allocation
Dynamic allocation in C++
Memory in C (This is kind of low level yet easy to understand)
Happy reading!