I'm not entirely sure what you're asking, but I'll try my best to answer.
The following declares a variable i
on the stack:
int i;
When I ask for an address using &i
I get the actual location on the stack.
When I allocate something dynamically using malloc
, there are actually TWO pieces of data being stored. The dynamic memory is allocated on the heap, and the pointer itself is allocated on the stack. So in this code:
int* j = malloc(sizeof(int));
This is allocating space on the heap for an integer. It's also allocating space on the stack for a pointer (j
). The variable j
's value is set to the address returned by malloc
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…