Reputation: 22327
I have the following class that implements static methods that must use a single global array. It is defined as such:
//Defined in LockTrack.h file
enum LOCK_ID{
LOCKID_0,
LOCKID_1,
LOCKID_2,
LOCKID_COUNT
};
static LOCK_ID __glob_lock_ids[LOCKID_COUNT];
class CLockTrack
{
public:
static void getLockedLocks(/*parameters*/)
{
//__glob_lock_ids = points to 0x015ef558 address in memory
LOCK_ID lockID = __glob_lock_ids[0];
}
static void inline setLock(LOCK_ID lockID)
{
//__glob_lock_ids = points to 0x015ef330 address in memory
__glob_lock_ids[lockID] = LOCK_ON_FLAG;
}
};
But what happens is that the '__glob_lock_ids' pointer points to different memory locations in each method. Why? And how to fix this?
Upvotes: 2
Views: 109
Reputation: 258698
Nope, that's not a global. Change it to
extern LOCK_ID __glob_lock_ids[LOCKID_COUNT];
and move
LOCK_ID __glob_lock_ids[LOCKID_COUNT];
into a single implementation file. Your version, static
, will effectively create a new variable for each translation unit that includes the header.
Upvotes: 11