views:

51

answers:

3

I have a global multidimensional array, g_iAllData[MAX_LEN][MAX_WIDTH] being used in a Form. When I write to it in a function: g_iAllData[iRow][iColumn]= iByte_Count; I can see in a Watch Window that it's contents are not being changed. If I put the array in the function, it works fine.

Is there something I'm missing? I am declaring it as global after my #include's at the top of the Form1.h file. I have multiple functions that are called by buttons being pressed and I need to write and read from the array in each function. It would be easier to keep it as global instead of passing it to each function.

UPDATE code:

ProgramName.cpp

#include "stdafx.h"
#include "Form1.h"  

Form1.h

#include <iostream>
#include <string>
...
#pragma once  

const int MAX_LEN = 4033;
const int MAX_WIDTH = 21;
int g_iAllData[MAX_LEN][MAX_WIDTH];    
...
namespace ProgramName{
// later on
ReadFile();  

void ReadFile(void)

g_iAllData[iRow][iColumn]= iByte_Count;
+2  A: 

Are you including Form1.h in other files too? If so, you need to use 'extern' in the other files.

Graham Perks
+2  A: 

Your code sample really confirms that you have a problem with your variable declaration.

As @Graham hinted, the proper way to define globals is:

  • define the variable in a cpp file
  • declare the variable as extern in a header file

I.e.

//ProgramName.cpp

#include "stdafx.h"
#include "Form1.h"  

int g_iAllData[MAX_LEN][MAX_WIDTH];    

//Form1.h

#include <iostream>
#include <string>
...
#pragma once  

const int MAX_LEN = 4033;
const int MAX_WIDTH = 21;
extern int g_iAllData[MAX_LEN][MAX_WIDTH];    

This way the linker will find the definition of the global variable in exactly one compilation unit, and in all other compilation units which #include the header, it will be able to link the extern declarations to the correct variable definition.

Barring this, strange things may happen: you may get cryptic linker error messages complaining about multiple variable definitions, or you might even get multiple distinct variables in your app instead of one global variable - the latter explains why your method doesn't seem to change the contents of your variable.

Péter Török
This is really helpful thanks, however I have changed my code to this and it still doesn't work. Should functions dealing with the user interface also be defined in the .cpp file and referenced externally?
Nick S.
@Nick, no, that's not necessary, functions are handled differently than data. Have you rebuilt your whole app after the code change? Just to be on the safe side :-)
Péter Török
yeah I have. Weird. Oh well I'll do it another way I guess. Thanks.
Nick S.
A: 

If you defined your array in header file (as you code shows), then every time you include that header in a translation unit, you effectively define a separate "copy" of your array. This is not even supposed to compile, since you are defining multiple instances of the same external object (violation of ODR).

If you managed to compile it somehow (how?), then there's no way to say which instance of g_iAllData you are modifying and which instance the debugger shows you in the watch window. Could be different instances, which is why you don't see the change.

Objects with external linkage should not be defined in header files. You better reconsider your approach.

AndreyT