-
Array of Global Classes
I was wondering if someone can tell me if I can do the following, maybe even give a very basic code example for it:
Create a Class, with an array of integers as members.
Then declare a global array of that class.
And if it's not too much trouble, show an example line of changing the value of the class in each iteration of the class.
If it's asking too much, I understand. The book I am reading from has good text, but no example.
-
-
I figured it out. I figured if I made my structure a class, that it would fix my problem. But it didn't. This is my problem:
I have an array global variable:
Code:
BOARD_INFO* gBoardInfo;
And the definition of BOARD_INFO is:
Code:
class BOARD_INFO {
public:
char name[BUFF_LEN];
int* pChDev;
FEATURE_TABLE* pFT;
ct_devinfo* pDevInfo;
char nameB[BUFF_LEN];
char nameC[BUFF_LEN];
char buff[BUFF_LEN];
int dev;
int nr;
int err;
int line;
DX_IOTT* plott;
char nmC[1024];
int bd, ch;
int bdV, chV;
char* pChannel[BUFF_LEN];
long devBRD;
LINEDEV* pLineDev;
int nrChannels;
};
My code compiles fine, no errors what so ever. However, during runtime I get a error:
"The instruction at "0x00401614" referenced memory at "0x00000000". The memory could not be "written".
After putting in some debug code, to find out exactly what line the error happens, I found the violating line of code:
Code:
for (ch = 0; ch < nr; ch++)
{
wsprintf (name,"dxxxB%dC%d", bd+1, ch+1);
gBoardInfo[bd].pChannel[ch] = _strdup (name);
plott = &gBoardInfo[bd].plott[ch];
plott->io_type = 0; // <- Error Popup
plott->rfu = 0;
plott->io_fhandle = -1;
plott->io_bufp = NULL;
plott->io_offset = 0;
plott->io_length = 0;
plott->io_nextp = NULL;
plott->io_prevp = NULL;
}
The error happens right when I use plott to assign values.
The declaration of plott is:
The declaration of DX_IOTT is:
Code:
typedef struct dx_iott DX_IOTT;
/*
* DX_IOTT: I/O Transfer Table structure
*/
struct dx_iott {
unsigned short io_type; /* Transfer type */
unsigned short rfu; /* reserved */
int io_fhandle; /* File descriptor */
char *io_bufp; /* Pointer to base memory */
unsigned long io_offset; /* File/Buffer offset */
long int io_length; /* Length of data */
DX_IOTT *io_nextp; /* Pointer to next DX_IOTT if IO_LINK */
DX_IOTT *io_prevp; /* (optional) Pointer to previous DX_IOTT */
};
I am fresh out of ideas of how to resolve this. Can someone tell me what it is that I am doing wrong? I've been dabbling with this error for weeks now, trying to overcome it on my own.
-
Where is gBoardInfo initialized.
-
Code:
plott = &gBoardInfo[bd].plott[ch];
Let's take a look at what plott is exactly:
Have you allocated space for plott? From the code you posted, I have no way to tell if plott actually points to a valid position in memory.
By the way, why is BOARD_INFO a class? If all its members are public, it might as well be a struct.
-
To be honest, I dont think I have allotted the memory for the variable. How do I go about doing this? I used calloc to allocate the memory for multiple BOARD_INFO's... I thought maybe since plott is a child, that it would have been enumerated with it as well.
-
gBoardInfo is initialized outside of all functions. I am assuming that it is global when I do that. I can already assign values to properties of gBoardInfo, so I'm good to go with it. My problem solely exists with the plott inside gBoardInfo
-
AH HA! Got it. Whew, I feel like a winner right now. :D My problem was, that I was not allocating the memory space for plott. Thanks for guiding me to the right direction. Seriously, thank you. ;)
-
plott is a pointer. C++ will treat it as a pointer, now and forever. C++ will do exactly as you say -- no more, and no less. Which includes not allocating memory for pointers. C++ does not assume that it knows what you want, and you certainly must not assume that it does.
By the way, you can use the simpler memory allocation functions new and delete.