+ 1
Array ?
Hello i want to know : if the array's memory is ditributed randomly?
4 Respuestas
+ 7
Using malloc puts the array in the heap, but it is allocated as C++ Soldier (Babak) stated (one continuous chunk of memory big enough to hold the entire array,)
+ 6
As our mentor, Mr. Wells pointed out, in the case of dynamically allocated memory ¹ (using malloc), the heap memory ² is where that contiguous block lives. On the other hand, when we talk about the static form of allocation like `int arr[10]`, we refer to the stack memory which is the host of that kind.
Here ³ is a good SO discussion about the differences between stack and heap memory.
_____
¹ https://en.wikipedia.org/wiki/Memory_management#DYNAMIC
² https://stackoverflow.com/questions/2308751/what-is-a-memory-heap
³ https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap
+ 5
"distributed randomly?"
The program specifies a `static` array like this somewhere in the codebase
int arr[10];
The compiler sees the above line and asks the operating system "Hey man! How you been?! Please set aside 10 contiguous chunks of the program stack for our little array."
The operating system looks into the memory map and searches for the most `suitable` candidate 10-block of the available memory. (so the candidate's position in the program stack -- and the program stack itself -- is chosen kinda randomly)
0
If malloc and int arr [10] reserve contignous blocks of memory so what's the difference