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 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top