How is an integer pointer variable stored?

Asked

Viewed 69 times

4

When we declare :

int* x;

How the compiler compiles this and how the computer (it would be better to say operating system because it manages memory) performs this ?

I mean, the O.R. reserves a space in its memory to store a memory address like int and names her after x. But how does it do that? Does it have a flag in its own memory that says it only accepts integers ? If it’s an address for char what would change? And how does he know that her name is x ? It would be another flag ?

Grateful.

1 answer

2


This works by the symbol table. When Voce does

int *x;

compiler goes in this table, allocates a 4 bytes space in memory and associates the address of that location with the name x and the guy int *.

So the table looks something like:

x    0xff843af int *

If it were a char, as it is nothing more than a number as well, your program interprets the byte value as char.

  • So once the program starts running, the S.O. creates a table to know who is who ? And in the case of local variables of the same name of global variables ? (It is wrong to say that the S.O. which executes ?)

  • is not the OS that takes care of the symbol table, and the compiler. the OS manages the system memory 'in general'.

  • Every time I run a program it will call the compiler to take care of the symbol table ? I found it strange..

  • No, the compiler after generating the executable will no longer use the table, it is only used during compilation.

  • Okay, but what about after I compile ? Every time I run my program it will have to allocate memory to the variables, how does it work ? (No need to enter the question of stack and heap, only the part of the variables)

  • variables will be allocated on the pages that the OS gives for the program. as the program knows types, it knows to compute addresses and use.

  • For pointers the computer always allocates the same size, only for the address to be placed. Roughly speaking, a pointer would not need a type when declared, it only occurs for code security. Addresses are the same size, so the computer knows the size to allocate in the case of pointers.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.