+ 3

Why cant we use normal variables instead of pointers for dynamic memory allocation?

25th Aug 2018, 7:44 AM
Rashim Narayan Tiku
Rashim Narayan Tiku - avatar
4 ответов
+ 3
Normal variables are just addresses when you do: int i = 1; i becomes your handle to a memory address that holds the value 1 from now on when you reference 'i' the program goes and grabs the value in the address of i pointers are just like variables except their value *points* us to another memory address. Dynamic memory allocation means that instead of reserving some number of bytes for an object, the object will be given just enough bytes BUT we don't know where it will be stored. it might not even take up contiguous memory. So the reason why is that: static allocation means we know where a value is stored dynamic allocation means that we don't know where a value is going to be stored so after reserving memory for an object, we're given a pointer to where the value WILL be stored
25th Aug 2018, 8:41 AM
Aidan Haddon-Wright
Aidan Haddon-Wright - avatar
+ 34
We cannot use normal variables for dynamic memory allocation because program does not know the address of dynamic memory & so we use pointers to point on that memory allocated at run time. Pointer is address variable it stores address where as normal variables store data.
25th Aug 2018, 8:42 AM
Shams Awais
Shams Awais - avatar
+ 2
Normal variable are STATICALLY allocated memory (exactly opposite of DINAMYC allocated memory)
25th Aug 2018, 8:58 AM
KrOW
KrOW - avatar
0
We can assign only a value to a variable not a memory or an address.
9th Jan 2019, 11:34 AM
Jkilop