PDA

View Full Version : global variable declaration



prkhr4u
16th October 2013, 04:22
I am trying to make a multithreaded QT GUI application.
I have 3 files :
main.cpp
mainwindow.cpp
grabthread.cpp
there are 2 header files:
mainwindow.h
grabthread.h

I want to declare an array of the size 1200*5000 globally so that it is accessible in all 3 files.

I have tried declaring it :
1> before main() in main.cpp (using extern also)
2> in mainwindow.cpp
3> in mainwindow.h

but I am unable to access the array in other files.
Where should i declare the array? So that i have global access

pkj
16th October 2013, 04:52
Declare in any header.. or have global.h
extern MyVar var;
define in corresponding cpp....
MyVar var (a,b,c);
#include the header....

prkhr4u
16th October 2013, 10:05
(.rodata+0x0):-1: error: multiple definition of `h'

errors like this are coming multiple times

Here is what i did:
//mainwindow.h
extern const int w=1200,h=4000;
//grabthread.cpp
#include "mainwindow.h"
int w,h;

googled it,kind of some linker error...
not able to solve it..

toufic.dbouk
16th October 2013, 10:12
Because you are doing multiple definitions.
Declare your global variable in the header file as extern and initialise it the .cpp file.
mainwindow.h
extern int y;
mainwindow.cpp
int y=1200;

This is not a Qt related question , it is pure c++
so check this link : Scope, Global Variables and Static (http://www.byteauthority.com/cpp-scope-global-variables-static/).
Good Luck.

anda_skoa
16th October 2013, 11:29
I would recommend using a single instance of a data class instead.
That class can then contain the array, its dimensions and the mutex you'll need for protecting against concurrent access.

Just create one instance of that class, e.g. in main() and pass its reference or pointer to all objects that need to work with it.

Cheers,
_

folibis
17th October 2013, 12:57
Use Singleton pattern, something like this:

singleton.h

#ifndef SINGLETON_H
#define SINGLETON_H

class singleton
{
private:
singleton();
int p_value;
public:
static singleton * instance();
int GetValue();
void SetValue(int value);
};

#endif // SINGLETON_H


singleton.cpp

#include "singleton.h"

singleton * p_instance = 0;

singleton::singleton()
{

}

singleton * singleton::instance()
{
if(!p_instance) p_instance = new singleton();
return p_instance;
}

int singleton::GetValue()
{
return p_value;
}
void singleton::SetValue(int value)
{
p_value = value;
}


so in each file you need this object just include


#include "singleton.h"
//and use it in one place
singleton::instance()->SetValue(1);
// in other place
int value = singleton::instance()->GetValue();

stampede
17th October 2013, 13:26
something like this:
Its not thread-safe and leaks memory :p Try again :)

folibis
18th October 2013, 00:56
Ehhm ...

... and leaks memory

Yes, you have to destroy the global object when program terminates. But it was just an idea, an example of singleton. You can build it with static member etc.
If you want it thread safe, use semaphores or eny else method.
But idea of singleton is good, in any case it more useful then you comment :p

Cheers

stampede
18th October 2013, 06:30
Ok, ok I know :) Anyway, here is a paper on Singleton and Double-Checked Locking, by Meyers and Alexandrescu http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf.
I think its good to be aware of the problems with singletons.
Ok sorry guys end of off-topic.

wysota
18th October 2013, 07:15
Ok, ok I know :) Anyway, here is a paper on Singleton and Double-Checked Locking, by Meyers and Alexandrescu http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf.
I think its good to be aware of the problems with singletons.
Ok sorry guys end of off-topic.

You can also protect singleton creation by using fetch-and-add semantics (e.g. with QAtomicInt) or simply by creating the singleton instance manually in the main thread so that instance() always returns an existing object and never tries to create one (just like QCoreApplication does). Another viable approach is to use a wait condition so that many threads at once can execute instance() but still just one can create the singleton instance.

anda_skoa
18th October 2013, 10:04
And all singleton problems can be neatly avoided by creating the shared object explicitly and passing it to all objects that need it :)

It can be constructed before any of the threads are, can be deleted after all of them have finished, creation can take arguments, ...

Not even talking about making unittest easier since each test can have a clean instance instead of using the same instance over and over again.

Cheers,
_