0

Doubt in Array's element memory allocation && Local variable memory allocation.

By seeing the first code Please say whether the arrays are using heap or stack memory for storing values? In C language ------------------------------------------------------------- By seeing the second code Please tell me,Why address of local variable 'a' is greater than address of local variable 'b' Is it due to local variables being present in stack memory? https://code.sololearn.com/cHkQXaAhf64a/?ref=app https://code.sololearn.com/c6iTv4pDAkPe/?ref=app

21st May 2021, 8:47 AM
Yogeshwaran P
Yogeshwaran P - avatar
5 odpowiedzi
+ 1
Ans1) array being stored in continuously increasing memory location is a property of data structure itself and have nothing to do with it being in stack or heap. And till the time you don't explicitly perform dynamic memory allocation ( by using malloc() or similar functions ), your program will almost everytime put items in stack memory. Ans 2) that depends on your language implementation and architecture. In this case your memory stack is growing down ( towards lower memory address ) and as local variables are pushed on top of stack, "b" is being assigned a lower memory address to that of "a".
21st May 2021, 11:10 AM
Arsenic
Arsenic - avatar
+ 3
Yogeshwaran as I said, program will always store data on stack till the time you don't explicitly tell it to store something on heap ( using malloc() ).
21st May 2021, 11:17 AM
Arsenic
Arsenic - avatar
+ 1
Thank you Arsenic 😃👍
21st May 2021, 11:19 AM
Yogeshwaran P
Yogeshwaran P - avatar
0
Akash Kumar Can you please explain or show, how array is using stack memory via code?
21st May 2021, 9:23 AM
Yogeshwaran P
Yogeshwaran P - avatar
0
Thank you Arsenic .can you please tell me ,how can I find whether I storing data in stack or heap memory? Is there any technique to identify it via code? based on address of memory
21st May 2021, 11:14 AM
Yogeshwaran P
Yogeshwaran P - avatar