Array ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Array ?

Hello i want to know : if the array's memory is ditributed randomly?

16th Nov 2018, 8:44 PM
Houcem
Houcem - avatar
4 Answers
+ 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,)
16th Nov 2018, 9:13 PM
John Wells
John Wells - avatar
+ 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
17th Nov 2018, 8:10 AM
Babak
Babak - avatar
+ 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)
16th Nov 2018, 8:53 PM
Babak
Babak - avatar
0
If malloc and int arr [10] reserve contignous blocks of memory so what's the difference
17th Nov 2018, 7:39 AM
Houcem
Houcem - avatar