Results 1 to 11 of 11

Thread: global variable declaration

  1. #1
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default global variable declaration

    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

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: global variable declaration

    Declare in any header.. or have global.h
    extern MyVar var;
    define in corresponding cpp....
    MyVar var (a,b,c);
    #include the header....

  3. The following user says thank you to pkj for this useful post:

    prkhr4u (16th October 2013)

  4. #3
    Join Date
    Oct 2013
    Location
    Bangalore,India
    Posts
    64
    Thanks
    21
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: global variable declaration

    (.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..

  5. #4
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: global variable declaration

    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.
    Good Luck.
    Last edited by toufic.dbouk; 16th October 2013 at 10:48.

  6. The following user says thank you to toufic.dbouk for this useful post:

    prkhr4u (16th October 2013)

  7. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: global variable declaration

    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,
    _

  8. #6
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: global variable declaration

    Use Singleton pattern, something like this:

    singleton.h
    Qt Code:
    1. #ifndef SINGLETON_H
    2. #define SINGLETON_H
    3.  
    4. class singleton
    5. {
    6. private:
    7. singleton();
    8. int p_value;
    9. public:
    10. static singleton * instance();
    11. int GetValue();
    12. void SetValue(int value);
    13. };
    14.  
    15. #endif // SINGLETON_H
    To copy to clipboard, switch view to plain text mode 

    singleton.cpp
    Qt Code:
    1. #include "singleton.h"
    2.  
    3. singleton * p_instance = 0;
    4.  
    5. singleton::singleton()
    6. {
    7.  
    8. }
    9.  
    10. singleton * singleton::instance()
    11. {
    12. if(!p_instance) p_instance = new singleton();
    13. return p_instance;
    14. }
    15.  
    16. int singleton::GetValue()
    17. {
    18. return p_value;
    19. }
    20. void singleton::SetValue(int value)
    21. {
    22. p_value = value;
    23. }
    To copy to clipboard, switch view to plain text mode 

    so in each file you need this object just include

    Qt Code:
    1. #include "singleton.h"
    2. //and use it in one place
    3. singleton::instance()->SetValue(1);
    4. // in other place
    5. int value = singleton::instance()->GetValue();
    To copy to clipboard, switch view to plain text mode 
    Last edited by folibis; 17th October 2013 at 13:05.

  9. #7
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: global variable declaration

    something like this:
    Its not thread-safe and leaks memory Try again

  10. #8
    Join Date
    Nov 2011
    Posts
    79
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: global variable declaration

    Ehhm ...
    Quote Originally Posted by stampede View Post
    ... 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

    Cheers

  11. #9
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: global variable declaration

    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_J...04_revised.pdf.
    I think its good to be aware of the problems with singletons.
    Ok sorry guys end of off-topic.

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: global variable declaration

    Quote Originally Posted by stampede View Post
    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_J...04_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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: global variable declaration

    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,
    _

Similar Threads

  1. Global declaration
    By nighil in forum Newbie
    Replies: 1
    Last Post: 24th January 2011, 16:51
  2. global variable in QT: getting ISSUES
    By Girija in forum Qt Programming
    Replies: 8
    Last Post: 19th September 2010, 16:15
  3. function prototype variable declaration
    By john_god in forum General Programming
    Replies: 4
    Last Post: 31st July 2010, 18:47
  4. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.