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

  • Thread starter Thread starter camel-man
  • Start date Start date
Click For Summary

Discussion Overview

The discussion centers around the location of pointers in C programming, specifically whether they reside on the stack or heap. It explores the implications of declaring pointers within functions and using dynamic memory allocation through malloc.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions whether a pointer declared in a function and allocated with malloc is located on both the stack and heap, suggesting it is a local variable that also involves dynamic memory.
  • Another participant asserts that the pointer itself is on the stack, while the data it points to is on the heap, indicating they are distinct entities.
  • A different participant notes that pointers can exist in various scopes, including global, static, stack, or heap, and can point to different variable types or functions.
  • One participant summarizes their understanding by stating that a pointer declared with malloc is on the stack but points to heap memory, while a pointer referencing another variable on the stack remains on the stack.
  • A later reply confirms the understanding that unless a pointer is declared globally, it will be located on the stack.

Areas of Agreement / Disagreement

Participants generally agree that pointers declared within functions are located on the stack, while the memory they point to can be on the heap. However, there is some ambiguity regarding the broader implications of pointer locations and their relationships to different memory areas.

Contextual Notes

Some assumptions about variable scope and memory allocation are present, but not all aspects are fully resolved, particularly regarding the nuances of pointer behavior in different contexts.

Who May Find This Useful

Readers interested in C programming, memory management, and the distinctions between stack and heap memory allocation may find this discussion relevant.

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.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
3K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
11
Views
4K
  • · Replies 40 ·
2
Replies
40
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
12
Views
2K