Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I ran into an example in my study on C++. A trivial case it seems like, but I am interested to know the internal of it:
//Example 1.
//this is easy to understand

int i =100; //a file scope variable

void foo()
{
   int j = i; //j is a automatic variable here
}

my question is the example 2 here
//Example 2
//how to understand this var j declaration works?
int i =100; //file scope variable
int j = i;  //how this var j works internally?

the paper I read explains how var j is assigned to var i as below:

//i =100 is evaluated in runtime, not compile time
//compiler generated initialization function
_sti_i_foo_c(){ j = i ;}


MY question is: how can I know this kind of internals such this function _sti_i_foo_c() generated.

I ask this question here to seek more insights from gurus in this community.

thank you!

What I have tried:

I do not have a good idea on how this _sti_i_foo_c() function is generated:
use what kind of tool? so that I can find such details?
Posted
Updated 21-Aug-21 10:20am

It says right in the comment,
//compiler generated initialization function
What you see (and many other similar gibberish names of the functions with their parameters) are the output of the compiler.

For C++, you can use the g++ to output the assembly or other similar dump from the program binary.

How do you get assembler output from C/C++ source in gcc? - Stack Overflow[^]. You can also try this online compiler that outputs the Assembly code in the right-side: Compiler Explorer[^].

Note: Every compiler outputs the code based on its own conventions, optimization techniques. For a similar result (_sti_i_foo_c()) please consult the compiler that was used by the book author.
 
Share this answer
 
Comments
Southmountain 21-Aug-21 17:40pm    
thank you! it is what I am looking for...
Further to the answer above, try setting a breakpoint on a file scope assignment like your i = j example. In VS2017, which is what I use for C++, you will find that it is hit even before your main(), and you will see whatever function the compiler generated for the initialization on top of the stack. You can then step through all the file scope initializations one at a time.
 
Share this answer
 
v3
Comments
Southmountain 21-Aug-21 17:38pm    
thank you for sharing this trick!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900