-2

I am working on a program that has C and C++ elements to it. I can't say I am an expert in this realm but I am noticing that a static variable isn't acting so static. Specifically I receive events asynchronously and then push events onto a static queue while the main thread pops off events from that static queue. When I push events onto the static queue I can clearly tell the queue increases by one and data is populated correctly. When I poll from that static queue the data is gonna and the queue size is 0. To confirm they act as different instances I printed their values from the asynch and the polling perspective and they both stay consistently different even though the variable has a stinking static attached to it. The static queue is stored in a .c file while I update the queue from a .cpp file. I'm almost positive this wouldn't cause issues but who knows. I'm stuck

Here are my two variables stored in the interactivity_mapping file...

/* Pointer storage */
static sdy_pointer_event_t pointer_events[MAX_POINTERS] = {0};
static int last_pointer_event = 0;

Here are the extern functions stored in the imported functions c file

/* Imported functions */
extern void GetId(SGLint32 (*WidgetId));
extern void ManageIndividualFocus(SGLbool RequestFocus, SGLbool RequestNoFocus, SGLint32 WidgetId, SGLbool IsContainer, SGLint32 NextWidgetId, SGLbool isInit, SGLint32 (*HasFocus));
extern void aol_keyboard(SGLint32 pKeyboard, SGLbool* pPressed, SGLbool* pReleased, SGLint32* pCode, SGLint32* pModifiers);
extern void aol_locate(SGLint32 pPointer, SGLfloat* pX, SGLfloat* pY);
extern void aol_pointer(SGLint32 pPointer, SGLbool* pPressed, SGLbool* pReleased, SGLint32* pButton, SGLfloat* pX, SGLfloat* pY, SGLint32* pModifiers);
extern void wrapper_PushButton(SGLint32 EmitCondition, SGLbool MouseInside, SGLbool MousePressed, SGLbool MouseReleased, SGLbool KeyPressed, SGLint32 KeyCode, SGLbool KeyReleased, SGLbool (*Selection), SGLbool (*ButtonPressed), wrapper_PushButton_mem* mem);
extern void wrapper_PushButton_reset(wrapper_PushButton_mem* mem);

Essentially in interactivity_mapping file there are implementations of the extern imported functions c file. The function I call asynchronously is not a part of this imported functions extern c file while the polling function is and is called aol_pointer. I don't know if this would cause this type of incorrect behavior though. The program is quite large so I don't think I can give a better picture without dumping the whole thing on here.

  • 6
    Please provide a [mcve] – AndyG Jul 15 '16 at 14:54
  • Probably C++ specific, yet no code to study... We are stuck too ;-) – chqrlie Jul 15 '16 at 14:54
  • 1
    Are you synchronising access to the static? – Peter Jul 15 '16 at 14:56
  • sounds like somebody might not be using [`extern`](http://stackoverflow.com/questions/3627941/global-variable-within-multiple-files) – jaggedSpire Jul 15 '16 at 14:56
  • extern is a good point. May be worth giving it a try. – PeterMorley Jul 15 '16 at 15:01
  • Note that `static` have more than one meaning. In the context of a local variable, or a member variable, `static` means one per program. In the context of a global variable, `static` means local to the translation unit. – sp2danny Jul 15 '16 at 15:05
  • If you observed the addresses of these queue's, you would have seen they were different. Then a review of what `static` means would have been done (but you really should have known what `static` means -- it is a basic fundamental of the language) – PaulMcKenzie Jul 15 '16 at 15:28
  • When I debug the program in visual studio and break at the asynchronous call the actual value is correct when I hover over it and it displays its value but when I print it to console it gives me its initialized value. Even when I look at the addresses as you mentioned all the data is the same and contains the correct data but its giving me unitialized crap... Can you explain that? – PeterMorley Jul 15 '16 at 15:46

1 Answers1

2

The static storage class means that the lifetime of the object is the whole execution of the program. The static specification of a variable means that it has static storage class and that the symbol (variable name) is only visible in the scope of the definition, that is of the block when it is in a function, and in the translation unit (.c file) when it is in global scope.

static does not mean that the element can't be used through functions or via a pointers from places where the symbol isn't visible.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177