1.
void main(void)
{
int *ptr1;
ptr1 = (int *)malloc(..);
}
2.
void main(void)
{
static int *ptr2;
ptr2 = (int *)malloc(..);
}
I want to ask how is memory allocation done for ptr1 & ptr2?
The ptr1 pointer itself is allocated on the stack. ptr1 points to memory on the heap.
The ptr2 pointer itself is allocated on program startup (before main is invoked) and is global but just happens to be visible only in main because it is declared in its scope. ptr2 points to memory on the heap as well.
Declaring ptr2 outside of main would only make it visible in all functions below it, but its storage will be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With