When declaring a variable in C ++ / C

When I declare a variable, for example:

int x = 6; 

What is x ? the memory address is usually hexadecimal.
Also, when I call X

 x = 2; 

How does the compiler know where x ? x not an address.

That was my first question.

Secondly:
Let's say I have an object:
Person p;
p has 2 data elements:

  int type1, int type2; 

What is p , and why do I need to switch to p , then a variable?

 p.type1, p->type1. 
+7
source share
11 answers
 int x=6 ; 

Here x is an identifier, i.e. name given to memory area where value 6 is stored.
x is a simple name to identify the area of ​​memory where value 6 is stored.
when u declares a variable, this time compiler stores the variable name ur in the identifier table.

For person p , here again p is the name assigned to the memory area in which two data elements type1 and type2 are stored

To access the values ​​of type1 and type2 , you first need to find the area of ​​memory in which they are stored. To do this, you first need to access the memory area p , and then you can access type1 * type2

+5
source

In the case of int x = 6 , x is just a name that will help you write the code, and the compiler will compile it. This is basically an alias for some place in memory, so it’s easier for him to access it later through x = 2 - this tells you and the compiler that you want to write the value 2 in the same place.

Same as before, but takes up more space ( sizeof(Person) , to be precise). p->type1 is only valid if p is a pointer to Person (or if you overloaded the -> operator, but it isn’t), p.type1 is the syntax used for the object to indicate which part you want get.

+5
source

x is the value of lvalue, which means "locator value". It is called that because, from x compiler knows where it is. So, in a sense, x is an address or something that allows you to display the address.

This thing becomes meaning only when reading.

 int a = x; 

In this case, the compiler takes x , reads the value at its location, and henceforth in this initialization x denotes its value. Unfortunately, this “reading of values” is an implicit process, so the value of the text x is the value or the value of lvalue depends on where it appears.

+4
source
 int x = 6; 

What is X?

x is a variable that can be defined as a name assigned to a location in memory.

how does the compiler know where x is?

There is something called a character table that the compiler uses internally to map the variable names in the actual memory cells.

+3
source

x is the identifier of the variable. A variable differs from an address in that it has a size and cannot be null. Address x can be obtained using &x . Consider this code:

 int x = 5; int* p = &x; // the pointer p now refers to x x = 6; 

Now, although you only changed the value of x , if you get the value of *p (called the "dereferencing" of the pointer), you will also get 6. p also a variable, but it is of type "pointer to int" and not "int", which has type x .

+1
source

x is a variable. Or, more precisely, the name or label of the place where this piece of information (data) that this variable refers to is stored. You need shortcuts to distinguish between different variables. It is just for you (the programmer) to help you.

p again a variable, but it is of type Person . And Person is a data structure - it contains the data that you receive through the structure. And p is an object (for example, an instance of a structure) of this type. You can have two objects of the same type

 Person p; Person someone; 

To find out which of the data / members you want to access, use p.data someone.data

Why p->data ?

Syntax

p->data equivalent to (*p).data

This explains what a pointer is. As I said, a variable is the place where you store some information (data). Just the specified pointer is a variable that stores the memory address. In other words, this indicates a different value stored somewhere in memory

For example:

 Person p; // variable of type Person Person *pointer_to_p = &p; // a pointer that points to the variable p // it holds the address of p - that what &p does it returns the address of a varible 

*p in (*p).data is called dereferencing a pointer (access to the value that the pointer points to)

For more information, this is a good idea for google pointers, or it is even better to get a book to read .

+1
source

That this is a variable is all that can be said with confidence.

It may not have a memory address if the optimizer puts it in a register. If it has a memory address, it can be given a fixed absolute address (for globals and statics in non-moving code), an address relative to some other object (for non-static members) or an address relative to the frame stack (for automatic local variables).

It may not even be a storage location if the compiler determines that it can always predict the value and substitute this value each time it is used.

If you use the & address operator for a variable, then the compiler will need to give it a memory location.

+1
source

The word is not a Thing.

All x and p , in which case p.type1 are names or identifiers. So, I think you are really asking what they identify. The answer is that they identify something that behaves as if it satisfies certain requirements of a language specification. In fact, this identified thing will depend on your platform, compiler, maybe which optimizations you turned on, etc. Etc.

So let's say

 int x = 6; 

You say that x now (in this scope and possibly nested areas) will refer to int (whatever it is) and give it an initial integer literal value of 6 .

So what is int ? This is a thing that can contain a single (positive or negative) integer value in the range [std::numeric_limits<int>::min(),std::numeric_limits<int>::max()] . Its exact size depends on the architecture, but it is no less than char and no more than long (IIRC).

Likely candidates for what it might be:

  • enough memory to hold any int on your platform associated with the linker character (possibly moved at runtime) if it is a global declaration, so all x related code uses the same memory location
  • sufficient memory with a fixed offset from the register of the stack pointer (or stack frame), if it is a local variable of a region function: the compiler is responsible for tracking the variables that it is going to put inside each function, and remembering what offset is associated with each identifier. This is a common (non-optimized) case.
  • a register of sufficient size to store int : if you never take the address x , it can spend all its life in the register, but if you either take the address or run the compiler from registers for all your variables (or you need to save them during the function call) , x can be loaded into the register when working with it and flipped back to the stack. Again, it is up to the compiler to remember when x is currently a register (and which), and when is this memory location

As you can see, the identification of “thing” x can indeed be many things at different points during the execution of your program.

If you are not optimizing something or folding your memory neatly, are the x properties more useful?

The point of a standard (or any language specification) is to make some useful guarantees about types, variables, and behavior, and then let the compiler choose how to achieve these guarantees.

+1
source

Let's take a step back ... it is important to understand that all programming languages ​​(except assembly language) describe the abstraction of a problem in terms that are supposed to be simple for programmers. The task of the compiler is to read this abstract description and generate a lower-level description that corresponds directly to the device.

When you write:

 int x = 6; 

you tell the compiler that you want to use the integer variable that you want to call this variable x, and that you want the variable to have a value of 6.

One of the tasks of the compiler is to decide how to save this variable. C ++ describes various kinds of variables that help the compiler decide which type of storage is appropriate.

  • A local variable (declared inside a function) is usually stored in memory on the stack, but a small amount (for example, an integer) can be stored in the register.

  • A global or static variable will be stored in memory.

The compiler remembers where he decided to save this value so that he can find it again - for a local varaible, which will be the name of the register or the address relative to the top of the stack; for global or static, this is the address relative to the start of all program data.

The actual address in memory is unknown until the program has been compiled, connected, and loaded into memory (since the OS may not always load the program at the same address) - it is important that the compiler knows where the variable will be and that it can generate code for access to it.

If, as in your second question, the type of the variable is some data structure that the compiler chooses where to place it in memory in the same way. When your program accesses a member of this structure, the compiler can work out the address of the member because it knows the address of the structure, as well as the offset of the member within the structure.

So, in the case of your example person p , when the program refers to p1.type2 , the compiler takes the address p and adds the offset type2 (which will probably be 4, because the first part of your struct person occupied with type1 , which is an integer that is is 4 bytes (on most 32-bit architectures)).

You must specify both p and type2 , because you may have another person (say q ), and the compiler needs to be told who you are trying to manipulate with. p.type2 not the same variable as q.type2 , and will have a different address.

+1
source

read about stack and heap.

x will be pushed onto the stack with a unique address. But you only need to call x.

but try cout <& x;

This is a real address

0
source

when you declare your variable, your compiler maps it to the variable attribute table. And this table contains the address and size, type, etc. Therefore, when necessary, the compiler gets things from this table.

-one
source

All Articles