Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location of pointers and global variables in C

Where are pointers and global variables stored in C? Are they saved in the memory, heap or stack?

like image 688
Muhammad Hewedy Avatar asked Jun 24 '10 20:06

Muhammad Hewedy


People also ask

Where are global pointer variables stored in C?

Global variables are stored in the data section.

Where are global variables located?

Global variables are stored in the data segment of memory. Local variables are stored in a stack in memory.

Are pointers local or global?

A pointer is a variable that contains the address where something lives. They aren't directly related to each other in any way. A pointer variable can be in global or local scope and can also point to a variable that is in global, local, or no scope (as if it were coming off of the heap or addressing some DIO lines).

Where are pointers used in C?

Pointers are used for file handling. Pointers are used to allocate memory dynamically. In C++, a pointer declared to a base class could access the object of a derived class. However, a pointer to a derived class cannot access the object of a base class.


1 Answers

Global variables can be in a couple places, depending on how they're set up - for example, const globals may be in a read-only section of the executable. "Normal" globals are in a read-write section of the executable. They're not on the heap or the stack at all. Pointers are just a type of variable, so they can be wherever you want them to be (on the heap if you malloc() them, on the stack if they're local variables, or in the data section if they're global).

like image 115
Carl Norum Avatar answered Oct 13 '22 06:10

Carl Norum