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;
Your code sample really confirms that you have a problem with your variable declaration.
As @Graham hinted, the proper way to define globals is:
externin a header fileI.e.
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
externdeclarations 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.