1

Maybe this has been asked several times but I couldn't find a single question that were focused on static vars storage inside templated functions. I would like to know where statics within templated functions are stored and how what is the compiler doing with them exactly? I'm going to provide some g++ memory layout just to show why I don't get them. My first code I cheched was rather simple:

#include <iostream>
using namespace std;

void my_func() {
    static int x;
}

int main() {
    my_func();

    return 0;
}

When I check the memory layout of this program with g++ 4.8.1 I end up with the following sections:

.text: 1845
.data:  620
.bss:    12

Nothing unexpected so far. The uninitialized static variable is stored within the .bss segment. The same goes if I initialize the x variable to 0, while if I initialize it with any non-zero value the; still nothing unexpected:

.text: 1845
.data:  624
.bss:     8

x in this case is stored within the data segment instead of bss. So far so good, so I turned towards my questionable part and changed my_func according to the following:

template <typename T> void my_func() {
    static T x;
}

int main() {
    my_func<int>();
    return 0;
}

Now this was interesting to me but the memory layout became:

.text: 1845
.data:  620
.bss:     4

Where did my static go? Whether I initialize it to any value static declared within templated functions doesn't seem to apprear nor in .DS neither in .BSS... Even if I instantiate another instance of that template function with different type for example my_func<float>() nothing is going to change. How is the compiler doing it? Where will it put those statics and how will these statics behave exactly the same as they weren't in templates - meaning they keep their values for each instantiated template function?

5
  • Why do you care where it's stored? Anyway, 98% of this question is compiler-, platform-, and processor-specific. So what are you using? Commented Feb 7, 2015 at 19:57
  • Couldn't really say why do i care. I just want to know, whether the standard says anything about this or in general. But if you read my post you already know my compiler and the platform is fedora-21 (linux) while the cpu is core i7-4700MQ. Commented Feb 7, 2015 at 20:00
  • templates are special to allow them to be defined in a header and not violate ODR rule. Maybe there is something there. Just throwing darts in the dark.... Commented Feb 7, 2015 at 20:03
  • Standard doesn't guarantee anything about storage and doesn't have a concept of section - this is implementation detail. Standard just says that your program should behave as specified, how it's implemented is up to implementation. Commented Feb 7, 2015 at 20:07
  • The standard says nothing at all about any of this. These are implementation details and have no bearing on the semantics of your program. Commented Feb 7, 2015 at 20:13

1 Answer 1

4

Probably the variable is being optimized away because it is not used. You may try to compile to assembly (gcc -S) and then lookup the variable in the output.

This experiment seems to confirm it:

> cat static.cpp 
#include <iostream>

template<class T>
int func()
{
  static int x = 0x666;
  return 3;
}

int main()
{
  std::cout << func<int>();
  return 0;
}
> g++ -S static.cpp && c++filt < static.s | grep ::x 
> sed -i 's/return 3/return x/' static.cpp
> g++ -S static.cpp && c++filt < static.s | grep ::x 
        movl    int func<int>()::x(%rip), %eax
        .weak   int func<int>()::x
        .section        .data._ZZ4funcIiEivE1x,"awG",@progbits,int func<int>()::x,comdat
        .type   int func<int>()::x, @gnu_unique_object
        .size   int func<int>()::x, 4
int func<int>()::x:
Sign up to request clarification or add additional context in comments.

1 Comment

yeah, you were right, the compiler optimized the variable away. Indeed the variable is in the .bss segment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.