Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static Variable Pointer?

Is this illegal/dangerous?

int* static_nonew()
{
    static int n = 5;
    return &n;
}

The compiler doesn't seem to have a problem with it, but is the pointer location itself protected from being overwritten when someone else needs memory?

EDIT: A little bit more of an explanation of why I asked this question. Note: I'm programming in C++, I just tagged it as C because it seemed to be more of a C than C++ question.

I have a class that's supposed to return a static map. I only want this map initialized once throughout the program as there doesn't seem to be a need to do it multiple times. For this reason, I was going to have something like this:

static std::map<std::string, Transition*> transitions;
static Transition trans1(transitions, ...);
static Transition trans2(transitions, ...);
return &transitions;

The Transition classes constructor would add itself to the transitions. In that way, it would create the transitions once and then return a pointer to them. I just remember that if you create a reference to a variable allocated on the stack, it could get overwritten very easily and was "unsafe". I was just a bit confused with exactly how static variables created within a function work.

like image 433
Jonathan Sternberg Avatar asked Jul 27 '10 00:07

Jonathan Sternberg


People also ask

Can a static variable be a pointer?

Static pointers in C/ C++These variables holds their value even after they are out of their scope. Once declared and initialized they are not re-declared i.e., once the memory is allocated, then they exist till the termination of that program. A static pointer declaration as shown below.

What is a static pointer?

In the simplest of terms: A static function is a member function of a class that can be called even when an object of the class is not initialized. A static function cannot access any variable of its class except for static variables. The 'this' pointer points to the object that invokes the function.

What is a static variable in C?

What is a Static Variable? In programming, a static variable is the one allocated “statically,” which means its lifetime is throughout the program run. It is declared with the 'static' keyword and persists its value across the function calls.

What is difference between global and static variable?

A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.


1 Answers

This is just a get function to the pointer of a static variable. There is nothing illegal about it. It is not intrinsically any more dangerous than any other type of static data.

But static data:

  1. creates tight coupling
  2. hampers re-entrancy
  3. creates the opportunity for subtle bugs
  4. is often a sign of weak design
  5. should be used very judiciously

The static storage class modifier means the memory will be reserved for this variable for the life of the process.

like image 198
Amardeep AC9MF Avatar answered Sep 29 '22 11:09

Amardeep AC9MF