Constantsd in C++

Started by
4 comments, last by MaulingMonkey 17 years, 6 months ago
What the compiler do when a number is passed to a function as an argument? For example:

void foo (int arg){};

foo (10);
Does he keep a place on the stack and put 10 in it? Thanks in advance.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
It instantiates (sp) int arg with the number 10.

Or rather, it "copies" 10 in arg:

arg(10)

If you were to put 10 in a variable and pass that to the function it would do something like this:

int temp = 10;
arg(temp)

So it basicly creates a temporary variable.
But where it creates this temporary variable? In the stack?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Quote:Original post by The C modest god
Does he keep a place on the stack and put 10 in it?


The behaviour is implementation-defined. The language only guarantees that arg will be a local variable with an initial value of 10.

Quote:But where it creates this temporary variable? In the stack?
Probably. If the function being called expects to find its parameters on the stack, then that is where the argument will be placed. If the function accepts its parameters in registers, the constant will be placed in the necessary register.
Quote:Original post by ToohrVyk
Quote:Original post by The C modest god
Does he keep a place on the stack and put 10 in it?


The behaviour is implementation-defined. The language only guarantees that arg will be a local variable with an initial value of 10.


And that's about all you can reasonably assume, too, without more information. In "Real Life" (tm) this depends on:

0) Whatever your compiler spits out. Duh. But seriously, look at the disassembly* if you really need to know (you don't).
1) Wheither or not it's inlined. In TCMG's brain dead simple case, anything that can reasonably call itself a compiler won't even call it at all - recognized by the optimizer as a no-op. In the more general case, if inlined, the exact placement will depend on the code that preceeds the call to the funciton, most likely. The number of variables at work here may exceed atoms in the universe.
2) Compiler, environment, phase of the moon.
3) Calling convention, assuming Venus is properly aligned with Mars, you're driving a hybrid on a Thursday, and you're a virgin Virgo. Some (non-standard) keywords to google: __fastcall, __stdcall, __thiscall, and __cdecl (?)

* OF RELEASE MODE OR I WILL STAB YOU

This topic is closed to new replies.

Advertisement