Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is memory allocated for a static const variable whose address is never used?

If I never use the address of a static const variable, is memory allocated for it when using a reasonably modern compiler?

like image 510
Emil Eriksson Avatar asked Nov 28 '11 00:11

Emil Eriksson


People also ask

Where the memory is allocated for static variables?

2) Static variables are allocated memory in data segment, not stack segment.

Where are const variables stored memory?

As per the memory layout of C program ,constant variables are stored in the Initialized data segment of the RAM. But as per some of the Microcontroller memory layout ,const variables are stored in FLASH Memory.

How are static members memory allocated?

Note: Memory for member functions and static data members is allocated per class and not per object. The class sample has no data member(except static count), but this does not mean no memory space will be allocated to the objects of Sample. In such cases, minimum memory is set aside for object.

How static variables are stored in memory?

The static variables are stored in the data segment of the memory. The data segment is a part of the virtual address space of a program. All the static variables that do not have an explicit initialization or are initialized to zero are stored in the uninitialized data segment( also known as the BSS segment).


3 Answers

It depends on the type of the variable, and on whether "constant" also means "constant expression". Example:

static const Foo = get_foo(std::cin);

static const int q = argc * 3;

static const std::string s(gets());

These variables are const, but blatantly need an actual allocation.

On the other hand, the following constant expression may never have physical storage:

static const int N = 1000;

static const std::shared_ptr<void> vp();  // constexpr constructor!

Most importantly, static constexpr member variables don't need a definition if you're careful:

struct Bar
{
  int size() const { return N; }
  static const int N = 8;
};
// does NOT need "const int Bar::N;"
like image 176
Kerrek SB Avatar answered Oct 16 '22 07:10

Kerrek SB


There is chance that it isn't, but that doesn't matter. You can't rely on implementation details, only on the standard.

like image 34
Tamás Szelei Avatar answered Oct 16 '22 08:10

Tamás Szelei


In practice, space for static storage can be allocated as part of the initial binary loading, or by the runtime during startup; but will always happen before user code is encountered.

In addition to the constraints that Kerrek SB mentions, the storage for a const expr value could be eliminated if the value itself is never used at runtime.

This wouldn't necessarily mean that the value needs to not be evaluated - if a static const expr were only used as a branch condition, that condition may be evaluated statically and other code paths may not be generated or may be excluded by the optimiser.

Pretty much any storage with static duration may be eliminated if the implementation can guarantee behaviour as though the storage were present - i.e. a comparison expression that can be evaluated at compile time - like a different const expr, a pointer comparison where the rhs is known to be an alias to a different variable, or perhaps an incompatible type. It may also be eliminated if the value is only read into variables that are never read themselves; or where the value may be reduced to a const expr.

struct Foo{};
static Foo bar; // static instance

Foo* func() {
    if ( ! (&bar) ) { // always non-NULL
        // this block may be eliminated
        Foo* myCopy(new Foo(bar));
        return myCopy;
    }
    // so 'bar' is never referred to, and we know it has no side-
    // effects, so the static variable can be eliminated
    return new Foo();
}

3.7.1 Static storage duration

2. If an object of static storage duration has initialization or a destructor with side effects, it shall not be eliminated even if it appears to be unused, except that a class object or its copy may be eliminated as specified in 12.8.

like image 34
rvalue Avatar answered Oct 16 '22 06:10

rvalue