c++ - What happens in memory when calling a function with literal values? -
suppose have arbitrary function:
void somefunc(int, double, char);
and call somefunc(8, 2.4, 'a');
, happens? how 8, 2.4, , 'a' memory, moved memory, , passed function? type of optimizations compiler have situations these? if mix , match parameters, such somefunc(myintvar, 2.4, somechar);
?
what happens if function declared inline
?
it makes no difference values literal or not (unless function inlined , compiler can optimize stuff out).
usually, parameters put registers or function parameter stack. regardless of whether explicit values or variables.
without optimizations, parameter gets pushed onto parameter stack. in first case, value of x
taken first , put register eax
, pushed parameter stack. foo
prints x
.
foo(x); 00361a75 mov eax,dword ptr [x] 00361a78 push eax 00361a79 call get_4 (3612b7h) 00361a7e add esp,4 foo(3); 00361a81 push 3 00361a83 call get_4 (3612b7h) 00361a88 add esp,4
with optimizations function visible compiler (in sample) , call skipped altogether:
foo(x); 01011000 mov ecx,dword ptr [__imp_std::cout (101203ch)] 01011006 push 3 01011008 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (1012038h)] foo(3); 0101100e mov ecx,dword ptr [__imp_std::cout (101203ch)] 01011014 push 3 01011016 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (1012038h)]
foo
defined as:
void foo(int x) { std::cout << x; }
Comments
Post a Comment