+ 1

Does malloc() alloction allocates different memory location on each exection ?

2nd Jul 2019, 2:01 PM
Dhanesh Sivasamy
Dhanesh Sivasamy - avatar
5 Réponses
+ 3
Yeah. When windows gives your program memory, it can give you any address it wants to (complicated topic). When malloc looks at what space is free, it also can place the pointer anywhere inside those 4KB. You should treat pointers like random numbers, you do not know their values before running the code. You don't really ever need to know the value itself anyway. All that is important is that you can move it left or right (like you are doing with `p + 2`)
2nd Jul 2019, 2:52 PM
Schindlabua
Schindlabua - avatar
+ 2
You should be very carefull when using malloc() within an iterarion. It dynamically allocates memory so you need a certain degree of control
12th Jul 2019, 5:44 AM
Da2
Da2 - avatar
+ 1
Roughly: - Yes you get a different address each time (if every variable pointed to the same address everything would be overwriting everything else) - When you malloc, the operating system (windows, linux..) gives you memory in 4KB chunks, even if you requested less. - malloc internally uses a list to keep track of which sections of those 4KB have been used, and which sections can be re-used (after you have free'd some memory) - If you need more than 4KB, the operating system gives you more 4KB chunks.
2nd Jul 2019, 2:20 PM
Schindlabua
Schindlabua - avatar
+ 1
Tq For the help Mr.Schindlabua :-)
2nd Jul 2019, 2:54 PM
Dhanesh Sivasamy
Dhanesh Sivasamy - avatar
0
I tried to execute the model code provided, and i thought of getting the address that has been occupied when malloc() is used https://code.sololearn.com/cXn0O4rvGPAt/?ref=app And i get new address line whenever i refresh the webpage and RUN the code again
2nd Jul 2019, 2:33 PM
Dhanesh Sivasamy
Dhanesh Sivasamy - avatar