Home c work with memory in si

work with memory in si




I do not understand.
Display the memory cell number can

# include & lt; stdio.h & gt;
INT MAIN (Void) {
  int x = 0;
  PrintF ("% x", & amp; x);

But manually set as

# include & lt; stdio.h & gt;
INT MAIN (Void) {
  int x = 0;
  & amp; x = 0xcdff00;
  PrintF ("% x", & amp; x);

Lvalue Required AS Left Operand of Assignment

either how to do it, I do not know. Correct or send true …

Answer 1, Authority 100%

The fact is that the use of the & amp address: gives us a RValue expression.
At Habré you can read what it is:

Lvalue (Locator Value) is an object that occupies
An identifiable place in memory (for example, has an address).

RVALUE is defined by exclusion, saying that any expression
It is either LVALUE or RVALUE. Thus, from the definition of LVALUE
It follows that RVALUE is an expression that is not
The object that occupies an identifiable location in memory.

That is, it is such an expression for which the place is not specified in the memory, so it is impossible to assign anything. Rvalue expression, for example, is also a constant 10 .

At the same time, the Range Operation * Returns the LVALUE-expression. If the expression is LVALUE and is not const , it can stand on the left of the assignment. Therefore, we can refer to the pointer, we can write to the memory that it indicates, a new meaning:

int a = 10;
int * p = & amp; a;
* p = 15;
PrintF ("% d \ n", a); // 15

You can’t guarantee the value for your chosen address. If only because this address may be already busy with something else.

However, you can ask the operating system to allocate you memory in the right place. To do this, in UNIX there is a system call mmap , and in Windows – Function VirtualAlloc . Note that these calls do not guarantee the allocation of memory at the specified address.

Note that if you manually set the value to the


char * p = (char *) 0xcdff00;

then his gathering

* p = 0;

will lead to uncertain behavior in your program. Therefore, it is impossible to do so.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions