简体   繁体   中英

C++ cast memory allocation

I'm wondering if the following operation results in a memory allocation:

int x = 0;
long long y = x + 1;

If it does, are there any compiler optimizations that take care of this on the fly, or does this result in a standalone cast followed by the addition?

The answer is: it depends. There is absolutely no requirement on the compiler to perform memory allocation if the the behaviour of the program does not depend on it. Specifically C++14 n3797 S1.9:

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.

This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.

In this case:

long long f() {
  int x = 0;
  long long y = x + 1;
  return y;
}

The compiler is absolutely free to rewrite this function as:

long long f() {
  return 1;
}

In doing so it may simply load immediate values into registers and never perform any memory access at all. Depending on the calling context it may even inline this function so it completely disappears from view.

If the variables are local variables inside a function, then the compiler will make sure that there is enough space for them on the stack. If they are global variables, then there will be space reserved in the executable file for the variables, and it will be "allocated" by the runtime loader of your operating system.

There is no dynamic memory allocation going on, ie there's nothing allocated on the heap. The space is reserved by the compiler, so it's already taken cared of by the compiler "on the fly".

I don't know if I fully understand your question, but I'll do my best to offer my 2 cents. In case you are not aware, initializing the way are , is not the most efficient way of doing so. For example:

int x = 0;

is calling the default int constructor, which uses the default allocator - reserves memory in the data segment - then puts the default int value in it which is 0, then uses the assignment operator - which uses the copy constructor - which in turn calls the destructor for the first int x , then finally int x = 0; . In other words int x(0); is better, though it doesn't really matter for ints. Also the compiler will optimize this for you, I was just pointing this out to help you make decisions in your programming style.

With the long long y = x + 1; similar constructor calls and copy constructors are being called, but the addition portion is trivial.

I think that your question is really whether a temporary variable is constructed holding the value of x as a long long so that it can be added to 1.

The answer is no. Both 1 and x are of type int , so the arithmetic will be performed as int arithmetic and the result will then be cast to long long and stored in y .

If you were adding x to something else of type long long then yes, x would be cast to long long before the arithmetic was done. In general, there is no requirement on how the compiler handles this sort of thing, but in practice, on almost any architecture you're likely to come across, it will happen in CPU registers, not in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM