Setting private member pointer to the same class as NULL? - c++

I came across the following structure in a C++ library:
In myClass.h
class myClass {
public:
static myClass* Instance();
.
.
private:
static myClass* _instance;
.
.
};
and in myClass.cpp
myClass* myClass::_instance = NULL;
// followed by the all other functions..
myClass::myClass() {
.
.
}
myClass* myClass::Instance() {
if (_instance == NULL) {
.
.
}
.
.
}
So what is the use of making the _instance to be NULL pointer outside any function? And when is this line of code executed?
Thank you.
Edit:
Adding the main function. And the instance function in myClass.cpp that checks for the value of the pointer. Still don't understand when the pointer get set to NULL though.
int _tmain(int argc, T_CHAR* argv[]) {
myClass* instance = myClass::Instance();
.
.
.
return 0;
}

So what is the use of making the _instance to be NULL pointer outside any function?
Static data members usually have to be defined, in the namespace containing their class, in one source file; they are subject to the One Definition Rule, and so must have exactly one definition in any program that uses them. This is that definition.
Initialising it with NULL makes sure it's initially null, so that the Instance() function can determine whether the instance has been created yet. This isn't strictly necesssary since, like all static variables, it will be zero-initialised whether or not you explicitly provide an initialiser.
And when is this line of code executed?
During static initialisation, before any other code in the program; since it's a trivial type with a constant initialiser.

I have stumbled upon something like this once. It was something similar to singleton. The reason (as explained by the person doing it) was that he specifically wanted to initialize instance at the first getInstance() function call and he wanted to make sure that the _instance pointer will be at first initialized to NULL (and not some garbage from memory) so that the check
if (_instance == NULL)
in the function works properly.
I am not sure this is the case here, but it's possible.

myClass* myClass::_instance = NULL;
the code attempts to initialize the static member variable.

It's an initialisation, ensuring that the pointer starts life with the value NULL from the very beginning of the program.1
It gives the pointer an invalid, but recognisable and testable value before some useful pointer value is assigned to it later on during the program. This way, you can safely test the pointer to see whether it was yet given said useful value.
In this case, since you are showing a singleton, "later" means "when an instance of myClass is first requested" using some myClass::getInstancePlease() function that you have not shown.
It is more common to implement a singleton with a function-static instance, rather than a class-static pointer to some [probably dynamically-allocated] instance.
1 As an object with static storage duration, it would do so anyway; therefore, this initialisation is actually completely pointless beyond documenting programmers' intent.

Static keyword means it is being shared by all the objects that will be instantiated by the class. Hence you need to initialize it outside any function.Static states that it is a class variable shared between all the instance of the class. It is opposite to instance variables, which each instance has its own copy. To access a class variable, there are two ways. 1) a_class::static_variable 2) a_class a; a.static_variable. The initialization must go in the source file (.cpp) rather than in the header.
Because it is a static variable the compiler needs to create only one copy of it. If you don't, you get a link error. If that is in a header you will get a copy in every file that includes the header, so get multiply defined symbol errors from the linker.
Static data members are not part of objects of a given class type; they are separate objects. As a result, the declaration of a static data member is not considered a definition. The data member is declared in class scope, but definition is performed at file scope.

Related

Static map used before it is initialized

I'm trying to implement a factory pattern like this.
The problem right now is that the program terminates with a segfault in the register function because the map is not initialized yet.
// initialise the registered names map
std::map<std::string, factoryMethod> SourceFactory::registeredClasses_ = { };
bool SourceFactory::Register(std::string name, factoryMethod createMethod) {
// registeredClasses_ = { }; // This prevents the segfault but does not work for obvious reasons
auto temp = std::make_pair(name.c_str(), createMethod);
std::pair<std::map<std::string, factoryMethod>::iterator, bool> registeredPair =
SourceFactory::registeredClasses_.insert(temp);
return registeredPair.second;
}
Why is it possible for Register() to be called before the initialization of the map? I tried initializing the map in the header file but then I get the linker error
multiple definition of SourceFactory::registeredClasses_
A solution would be to set a static bool isInitialized=false and then initialize the map accordingly. But I hope this can be avoided.
It's possible when Register is called from a different translation unit before the registry has been initialised.
Unfortunately, adding a static flag would not solve anything because that would not be initialised either.
A convenient solution is to add a level of indirection:
// static
std::map<std::string, factoryMethod>& SourceFactory::registry()
{
static std::map<std::string, factoryMethod> registeredClasses;
return registeredClasses;
}
bool SourceFactory::Register(const std::string& name, factoryMethod createMethod) {
auto temp = std::make_pair(name, createMethod);
return registry().insert(temp).second;
}
This is a common problem known as the static initialisation order fiasco.
Objects with static storage duration at namespace scope are constructed in-order within a translation unit, but you don't know whether those in other translation units may undergo initialisation first.
Instead of having your container at namespace scope, make it function static (perhaps returned from a new GetRegistry() function?) so that it is constructed on first use. That could be use from main, use from initialisation of another static-duration "thing" (which is presumably where your Register call is coming from), use from the moon…
This is also why the proper way to write a singleton is to have a GetInstance() function that declares (staticly!) the instance within the function's scope.
A solution would be to set a static bool isInitialized=false and then initialize the map accordingly. But I hope this can be avoided.
Nope. Not only would you then have the same problem with the isInitialized flag, but there's nothing you can "do" with that information. You can't "initialize" something other than in an initializer, and the whole problem is that the initializer hasn't been used yet. You could assign to the map, but that would have undefined behaviour because you'd be assigning to something that doesn't exist yet… and then it would be initialized later anyway!

Static variable and memory allocation in class and its use

I am new with c++ and I don't completely understand the concept of static variables.
I have a static variable in a class.
class FCCommunication : public OEMThread
{
public:
FCCommunication();
static bool MASTER;
}
I am initializing my code and allocation memory to the FCCommunication in the source file using following two statements
FCCommunication * FCObject = 0;
FCObject = new FCCommunication();
now the question for me is that what will happen if I try to access MASTER variable before the object and memory allocation will be done like this
if(FCCommunication::MASTER)// this gets called before dynamic memory allocation.
{
//do something here.
}
Static member variables are allocated as any other non-member variable with static storage duration. Meaning that they end up in a chunk of data initialized before the program is started, most often called either .bss or .data, depending on if the initalizer is a zero value or not.
So the static member variable is not actually allocated together with the class and the value you initialize it to is set by code executing before the rest of the class is even allocated.
This means that you can actually access static members no matter if any instance of the class exists or not. You can think of them as "global variables with restricted access and scope", because that is exactly what they are.
Meaning that your code is fine.
C++ member static variables (of a class) belong to all instance of that class, and they are initialized before any instances of that class are initialized. So, you can use both FCCommunications::MASTER and FCObject->MASTER to access those static variables with no differences.
One thing you should be careful about is that you have to define those static variable separately. That say, you have to do something like this, outsides the class definition:
bool FCCommunications::MASTER = false;
static members are members initialied to zero for first time it is initialized...
IN otherr words , they belongs to a common pool.
any other object can access it.
scope is within the class
the lifetime is the lifetime of the program.

How to access the variable from another function in a class

So I have experience using programming languages and just switched over to C++. Now I have created a few working applications but I always stumble upon the same problem. I don't exactly know what everything is called in the code. You have a class which obvious to see since the has written class before it. And you also have some sort of functions attached to the class are these called instances? And is the class the object it referring to by for example class::function.
But my main question was how can I access the variables from another function within the same class file. I have included an example below explaining what I want to achieve. I already tried a lot of things and googled a lot. I tried code pasting code creating setting and getting functions, calling the class to get and set the variable but I can't get it to work. I've spend a lot of time fixing this very basic problem. Could someone explain me what is called what in this code (Class,Object,Instance). And explain me the most efficient way to receive data from another function in the same .cpp file.
Thanks
load_data.h
#pragma once
class load_data
{
public:
static int data[13];
load_data();
static void test2();
};
load_data.cpp
#include "load_data.h"
#include "abc.h"
load_data::load_data()
{
int data[3]; // Initializing the array
data[0] = abc::LoadImage("textures/1.png");
data[1] = abc::LoadImage("textures/2.png");
data[2] = abc::LoadImage("textures/3.png");
}
void load_data::test2()
{
abc::CreateSprite(1, data[0]);
abc::SetSpritePosition(1, 0, 0);
abc::SetSpriteScale(1, 3, 3);
// Now I get an error saying it has no data. Which however is set in
// load_data(). But since each function gets its own variables this one will be empty.
abc::CreateSprite(2, data[1]);
abc::SetSpritePosition(2, 64, 64);
abc::SetSpriteScale(2, 3, 3);
abc::CreateSprite(3, data[2]);
abc::SetSpritePosition(3, 128, 128);
abc::SetSpriteScale(3, 3, 3);
}
Change your load_data() constructor to the following (currently, your creating a new data[] variable that is locally scoped to your load_data() constructor, which gets initialized instead of your object's data[] (gets "eclipsed"). Your subsequent call to test2() fails when it accesses data[] because the other, local data[] was initialized instead. Also, fyi, the local data[] is destroyed when load_data() returns (because it is an auto/stack variable that falls out of scope).
load_data::load_data()
{
//int data[3]; // Initializing the array
data[0] = abc::LoadImage("textures/1.png");
data[1] = abc::LoadImage("textures/2.png");
data[2] = abc::LoadImage("textures/3.png");
}
you also have some sort of functions attached to the class are these
called instances?
An object is an instance of a class. A class defines the object type, which consists of state variables, and operations for them (known as "member functions" or "methods").
An object's methods are called through a handle to the instance. IE, an instantiated variable:
load_data ld = new load_data();
ld.test2();
And is the class the object it referring to by for example
class::function.
This notation is for explicitly qualifying a method name. It helps resolve naming conflicts and should only be used when needed (otherwise it is implicit).
But my main question was how can I access the variables from another
function within the same class file.
...
But since each function gets its own variables this one will be empty
All functions of a class share the class'es (member) variables. An given instance of the class has the only copy of those member variables (ie, their specific values/memory to that instance), so all method calls through a specific instance variable (say ld) of load_data will refer to the same data[] array (so load_data ld1, ld2 would each have their own copies). Functions can, and usually do, declare their own variables to help assist in computing the task they perform (bools, counters, etc...). These such variables, as mentioned before, are scoped to that function, meaning they're no longer allocated and get destroyed when the function returns (they are auto-variables).
And you also have some sort of functions attached to the class are
these called instances?
No. the functions inside of the class are called "class member function" or just "member functions". An instance is a copy of that object (read class) in memory. So in short:
class A {
public:
void fun (void); ///< This is a class member function
};
void main (int argc, char *argv[]) {
A a; ///< a is an instance of object A
}
And is the class the object it referring to by for example
class::function.
The class defines the object. In the above snipped, A is an object.
But my main question was how can I access the variables from another
function within the same class file.
You need to do some reading on variable scope. In your above example the data array is local to the constructor. It doesn't exist within the object, only within that function. So as soon as the constructor finishes, the variable goes out of scope and is lost. In order to keep it in the object's scope you would need to declare it within the object.
class load_object {
public:
// The same
private:
int load[3];
};
Cheers

Singleton - Impl. using static class member vs. static member variable

Bring you two different implantation of singleton design pattern:
static class member
class SingletonCSM;
using SingletonCSMSp = std::shared_ptr < SingletonCSM > ;
class SingletonCSM
{
public:
~SingletonCSM() { spInstance = nullptr; }
static SingletonCSMSp GetInstance()
{
if (!spInstance)
spInstance = std::make_shared<SingletonCSM>();
return spInstance;
}
private:
SingletonCSM() {}
// will be intilized in the cpp: SingletonCSMSp SingletonCSM::spInstance = nullptr;
static SingletonCSMSp spInstance;
};
static member variable
class SingletonFSV;
using SingletonFSVSp = std::shared_ptr < SingletonFSV > ;
class SingletonFSV
{
public:
~SingletonFSV() {}
static SingletonFSVSp GetInstance()
{
static SingletonFSVSp spInstance = std::make_shared<SingletonFSV>();
return spInstance;
}
private:
SingletonFSV() {}
};
I always use the first impl. SingletonCSM. I came across, in our code, an impl. like SingletonFSV
Questions
Can we consider both impl. as a valid impl. of the design pattern?
Are both, functionally, the same?
Motivation
Background
Class SingletonFSV was implemented as a part of a DLL project. This DLL, compiled in VS 2013, is loaded in memory by an exe file and run.
Problem
I've upgrade my VS to VS 2015, compiled the DLL project and run the exe. All of a sudden, it crashed. While debugging, I've notice that the crash happened in the DLL itself. make_shared called withing GetInstance() returned nullptr and naturally caused segmentation fault.
Solution
I've changed SingletonFSV impl. to SingletonCSM and the crash stopped. make_shared returned a valid pointer and the problem was solved.
Question
I just don't understand what was the problem and why was it solved?
When you put a static variable inside a function it is created the first time the function is called so it is guaranteed to have been instantiated to all callers of the function.
Members that are declared static could be instantiated before or after you call your function because the initialization order between translation units is undefined. So a global object or static object could try to access a static member before its been initialized by the runtime system.
So to your questions:
Can we consider both impl. as a valid impl. of the design pattern?
Are both, functionally, the same?
No. Using a static member is dangerous because a caller to SingletonCSM::GetInstance() can access a non-created object if the nullptr initialization has not taken place before the call. The static function method is the recommended method because it guarantees initialization has completed for every caller.
I just don't understand what was the problem and why was it solved?
In your case moving to the more dangerous method appears to have stopped your crash. I can not explain why that is. But you may not have removed the problem, it may be that you have undefined behavior elsewhere that has simply stopped manifesting in this case but that may resurface later with a different change.

static variable inside member function of base class

i have the following:
class base
{
public
void f();
...
}
void base::f()
{
static bool indicator=false;
.....
if(!indicator)
{
...
indicator=true;
}
}
class D:public base
{
...
}
in my main() i have:
main()
{
// first instance of D
base *d1 = new D();
d1->f();
....
// 2nd instance of D
base *d2 = new D();
d2->f();
}
i find that the first time i instantiate D and call d1->f() the static variable is set to false. but the 2nd time i call d2->f() the code does not even hit the line "static bool indicator=false;" and it is kept at true (from the first pass of d1-f()) This is exactly the behavior i want but i do not understand why this is happening. can someone please explain what is happening. thanks in advance
Static variables declared in member functions will keep their value between function calls. There will be only one copy over all instances, and all accesses to indicator from different instances will affect the same indicator. This means indicator will only be initialized once.
See here for more info: Static variables in member functions
Also this code does not toggle indicator, it always sets it to true if it's false (which I'm sure is the behaviour you want).
if(!indicator)
{
...
indicator=true;
}
This is precisely the behavior of static variables declared inside function blocks. It has been like that since the introduction of the feature in the C programming language.
The static declaration can also be applied to internal variables. Internal static variables are local to a particular function just as automatic variables are, but unlike automatics, they remain in existence rather than coming and going each time the function is activated. (K&R, page 61).
The static initializer is executed before the first invocation of the containing function. Being static, the variable retains its last state across the invocations.