Is a Pointer in C Located on the Stack or Heap?

  • Thread starter Thread starter camel-man
  • Start date Start date
AI Thread Summary
In C programming, the location of a pointer depends on its declaration context. A pointer declared within a function is stored on the stack, while the memory it points to can be on the heap if dynamically allocated using malloc. This means that when a pointer is created and memory is allocated for it, the pointer itself resides on the stack, but it references a block of memory on the heap. Conversely, if a pointer is assigned to point to a variable within the same function, both the pointer and the variable are on the stack. The distinction is important: pointers can be global, static, or local, affecting their storage location. Overall, unless a pointer is declared globally, it will be on the stack.
camel-man
Messages
76
Reaction score
0
IS a pointer in C located on the stack or heap? Also if you declare a pointer in a function then malloc the memory for it does that mean it is both on the stack and heap because it is a local variable but it is also dynamic memory.
 
Technology news on Phys.org
camel-man said:
if you declare a pointer in a function then malloc the memory for it does that mean it is both on the stack and heap because it is a local variable but it is also dynamic memory.

The pointer is on the stack. The block of data that it points to is on the heap. They are two different objects, although obviously related.
 
A pointer is the same as any variable, it could be global, static, on the stack, or possibly on the heap (for example, malloc memory for an array of pointers...). A pointer can point to any variable type, and a pointer to function points to code.
 
Ok thank you I think I got it. Tell me if I am on track or not

HEAP: int *x=malloc(5); <--- x is on stack but points to heap
STACK: int *x,y; x=&y; <--- x is on stack again and points to memory on the stack

so unless the int *x; is global it will be on stack correct?
 
camel-man said:
Ok thank you I think I got it. Tell me if I am on track or not

HEAP: int *x=malloc(5); <--- x is on stack but points to heap
STACK: int *x,y; x=&y; <--- x is on stack again and points to memory on the stack

so unless the int *x; is global it will be on stack correct?

Yes, that is correct.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top