To give you a background, I have developed in C (and some C++ when it's called for) for the past several years, along with x86 assembly (inline and standalone). I am new to Qt and am using QT Creator 5.1 with MinGW compiler (Windows XP/no ASLR). My problem, in summary, is that I cannot use a global variable across different namespaces, as somehow the memory changes its location, even if dynamically allocated. I have tested this problem on another platform (VC++ and Linux/Clang) without using Qt and it works just fine.



The best way I can describe this problem is whenever I am using a user-defined datatype (struct serverUpdateInformation_t) within a namespace (NS_A) function, if I use a different variable of the same type (struct serverUpdateInformation_t) within a namespace (NS_B) which calls NS_A earlier in program flow, the offsets/fields within my struct change. Even using pre-processing directives like #pragma pack... do not work in keeping data uniform within memory.

Needless to say it is frustrating. I am trying to have just one copy of my variable available between "compilation units", but after trying several tricks the best I can get is the variable to have its memory address changed between functions, even though there is no explicit reassignment of the pointer location.

As an aside, these functions are not being compiled with the Q_OBJECT macro in their respective header files. I'm just trying to do "standard" C++, if not standard C. However, other "compilation units" are using the Q_OBJECT macro, which does end up calling some of these functions via my GUI code.

Example:

Assume variable "static serverLoginResponse_t serverLoginResponse" is defined within a "GlobalVARS.h" header file among two or more namespaces with functions which access the same variable.

Qt Code:
  1. namespace NS_A
  2. {
  3. ...
  4. int NS_A::RecvUpdateInformation()
  5. {
  6. // Whenever I try to use the global variable "serverLoginResponse" directly in NS_A::FuncA, if I try to again use it in NS_A::FuncB or NS_B::FuncA, the compiler makes
  7. // another "copy" of the static global variable, thus negating the point of a global variable.
  8. // Thus, I use a pointer, which at least lets me keep the global variable in the same place in memory.
  9. Credentials::serverUpdateInformation_t* pServerUpdateInformation = &Credentials::serverUpdateInformation;
  10. ...
  11. // I allocate and setup fields below for a function in namespace NS_B to deal with...
  12. return status;
  13. }
To copy to clipboard, switch view to plain text mode 

The interesting thing is, when I try to achieve the same reference in an NS_B function, it alters the offsets of the struct fields in NS_A. Yes, I know! It will still compile fine, yet the offsets of the struct's fields change between "compilation units".

For example, if I use this following code in NS_B

Qt Code:
  1. namespace NS_B
  2. {
  3. ...
  4. int Login()
  5. {
  6. ...
  7. // If we don't uncomment the following line(s), our offsets in referencing the struct get messed up, leading to improper computation.
  8. Credentials::serverUpdateInformation_t* pServerUpdateInformation = &Credentials::serverUpdateInformation;
  9. Credentials::serverUpdateInformation serverUpdateInformation_LocalCopy = Credentials::serverUpdateInformation; // This line will cause offset problems as well, as an example.
  10. ...
  11. return status;
  12. }
  13. }
To copy to clipboard, switch view to plain text mode 



This has been frustrating beyond belief. I know this is possible in standard C without any problems. It feels like I'm fighting against the framework in order to do basic low-level C/C++ within QT.
My current workaround solution was to serialize this datatype into a file after I initially referenced it in the NS_A function. Sadly, whenever I introduce a local variable of the same datatype within a different "compilation unit", it affects the memory layout of the global variable of the same type, even though they both should not interfere with each other!

Please help!