Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and where is variable type information stored?

Tags:

c

types

pointers

If I have allocated memory for an int, I will (usually) be given 4 bytes. As I understand, these 4 bytes make up the entire footprint of this variable in memory.

Furthermore, if I have a pointer to this int, it will hold the address of the first of its 4 bytes.

However, how does my program know that the type of data in those 4 bytes consists of a single int? And since my pointer only holds this address, which supposedly only holds raw data, how does it know that whenever the address it holds is dereferenced, it should be interpreted as an int?

Where is this type information, and how and when does the program access it?

like image 555
Daniel Avatar asked Dec 15 '14 08:12

Daniel


People also ask

How are variable types stored?

Variables are stored in two broad categories: string (text) or numeric. For tasks in analysis such as regression, Stata requires categorical variables to be stored as numeric variables, not string variables. This is also beneficial for storage size of variables.

Where does variable get stored?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).

How does C store variables?

A variable in a C language is a storage space with some memory allocated to it. The C language has five types data types: int, float,char,double and void. Variables are declared using these data types, and alphanumeric values are provided.

What is variable data type?

A variable's type determines the values that the variable can have and the operations that can be performed on it. For example, the declaration int count declares that count is an integer ( int ).


1 Answers

Once you compile a C program, the type information is essentially lost (or put another way, it is no longer needed). This is because the interpretation of any bytes of memory in C are up to the code which reads them. You can read a four-byte int as a char[4] with no problems, for example.

Type information may be preserved in certain cases for special reasons, such as debugging. But this is stored in platform-specific formats (e.g. DWARF on Linux), and is not part of the C standard at all.

like image 69
John Zwinck Avatar answered Sep 17 '22 23:09

John Zwinck