+ 3
Why cant we use normal variables instead of pointers for dynamic memory allocation?
4 odpowiedzi
+ 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
+ 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.
+ 2
Normal variable are STATICALLY allocated memory (exactly opposite of DINAMYC allocated memory)
0
We can assign only a value to a variable not a memory or an address.