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

  • Thread starter camel-man
  • Start date
In summary: A pointer declared in a function, even if it is dynamically allocated using malloc, will still be located on the stack. Only if the pointer is declared globally or statically will it be on the heap.
  • #1
camel-man
76
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
  • #3
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.
 
  • #4
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.
 
  • #5
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?
 
  • #6
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.
 

What is a pointer and where is it located?

A pointer is a variable that stores the memory address of another variable. It is typically located in the computer's memory.

How is the pointer value determined?

The value of a pointer is determined by the memory address of the variable it is pointing to. It may also be set to NULL if it is not currently pointing to a valid memory address.

Can a pointer be changed to point to a different memory address?

Yes, a pointer can be reassigned to point to a different memory address at any time during the program's execution.

Does every variable have a pointer?

No, not all variables have pointers associated with them. Pointers are only used for certain data types such as arrays, structures, and objects.

What happens if a pointer is dereferenced to an invalid memory address?

Dereferencing a pointer to an invalid memory address can result in a segmentation fault or undefined behavior. It is important to ensure that pointers are pointing to valid memory addresses before dereferencing them.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
718
  • Programming and Computer Science
Replies
5
Views
869
  • Programming and Computer Science
2
Replies
40
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
801
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
11
Views
3K
Back
Top