- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi all,
A simple example uses two static variables named "aa": static int told=0;
...
start_endless_loop: //------------- endless loop
if (alt_nticks() > told) {
told = alt_nticks() + 1000;
static int aa=0;
if (aa < 5) {
printf("%d ",aa);
aa++;
for (i=0; i<10; i++) {
static int aa=1000;
aa++;
if (i==9) printf("%d\n",aa);
}
}
}
....
goto start_endless_loop; //------- endless Loop
The console shows: --- Quote Start --- 0 1010 1 1020 2 1030 3 1040 4 1050 --- Quote End --- To resolve the adresses of the two "aa"s in the symbol table I used the Nios Command Shell with the command "readelf -s" : --- Quote Start --- ... 122: 002167a0 4 OBJECT LOCAL DEFAULT 6 aa.5198 ... 132: 002166f0 4 OBJECT LOCAL DEFAULT 5 aa.5199 ... --- Quote End --- It shows that the compiler has generated different variables for each "aa" The memory shows: --- Quote Start --- 0x002167a0: 5 0x002166f0: 1050 --- Quote End --- The question is: How does the compiler/linker generate the internal variables "aa.5198" and "aa.5199" and how can I get the location of the variables in the source code from the compiler-added numbers? Kind regards
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As far as I know, you can't. The numbers are added so that the linker can have unique names to work with. They might change across compiles too. If you want to know the locations, use different names for each variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
--- Quote Start --- As far as I know, you can't. The numbers are added so that the linker can have unique names to work with. They might change across compiles too. If you want to know the locations, use different names for each variable. --- Quote End --- As far as I understood, the compiler adds the numbers, when producing the .o file. In this file I found the variables with added numbers but without any hint where the compiler found the declarations. So there might exist an compiler option to output the location of the variable declaration in sourcecode or maybe in preprocessor-generated code. In the latter case I would need an access to the code the preprocessor generates....
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The best solution is 'don't do it'.
Reusing a variable name in an inner scope is a very good way to introduce subtle bugs into your code.
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page