Two dimensional array problem
-
I'm trying to define a char array; char m_pData[256][4]; I don't get any error messages, but the program stops and gives me a unhandled exeption when i run it.:(( this doens't work eiterh: char *m_pData[256][4]; or char m_pData[10][2]; but if I write char m_pData[2][2]; this works, it's probably just some properties needed to be change?:confused: can anyone find a solution to this? thanx
lol, a similar problem was posted here some days ago... define char m_pData[256][4]; GLOBAL! this should solve the problem Don't try it, just do it! ;-)
-
I'm trying to define a char array; char m_pData[256][4]; I don't get any error messages, but the program stops and gives me a unhandled exeption when i run it.:(( this doens't work eiterh: char *m_pData[256][4]; or char m_pData[10][2]; but if I write char m_pData[2][2]; this works, it's probably just some properties needed to be change?:confused: can anyone find a solution to this? thanx
Can you post the code so we can take a look of what you're doing? The problem can be if you use character strings, don't forget that there is a null character that is appended (so your string has to be 1 char larger than what it contains). What do you what to do with char m_pData[256][4] ? Do you know that it means you have 256 strings of 3 characters (because of the null char) and NOT 4 strings of 256 char ?
-
I'm trying to define a char array; char m_pData[256][4]; I don't get any error messages, but the program stops and gives me a unhandled exeption when i run it.:(( this doens't work eiterh: char *m_pData[256][4]; or char m_pData[10][2]; but if I write char m_pData[2][2]; this works, it's probably just some properties needed to be change?:confused: can anyone find a solution to this? thanx
:confused: You need to tell us more information about your problem? What size of aray do you need? Why do you need it to be 2 dimensional? John
-
lol, a similar problem was posted here some days ago... define char m_pData[256][4]; GLOBAL! this should solve the problem Don't try it, just do it! ;-)
I think the only reason why that solved your problem is that if you step past the end of a global variable you are probably not going to interfere with the runtime stack. Your problem probably still exists but in a sneaky way. Try the following declare your values array as
double ValuesArray[190];
Guess what it probably will not crash your dialog this time either. Why does it not crash the dialog? It is overwriting other variables in your global data that could come back and cause your program to work in an unexpected way. John
-
lol, a similar problem was posted here some days ago... define char m_pData[256][4]; GLOBAL! this should solve the problem Don't try it, just do it! ;-)
The array is define global and public in the header file. I have not yet been able to use the array, because when I define it the application stops before I'm able to do anything. a bit of the source code:
#pragma once #include "afxwin.h" #define MAX_DATARULES 256 // CSpeechControlDlg dialog class CSpeechControlDlg : public CDialog { // Construction public: CSpeechControlDlg(CWnd* pParent = NULL); HWND m_hwndForeground; HWND m_oldfocus; char *m_pData[MAX_DATARULES][4]; ....
I need to use an array to fill it with information about speech commands which the application recognizes. It compiles, and links, but stops with an access violation exception! When I created a new project this initialization worked perfectly, this makes me very confused! -
I think the only reason why that solved your problem is that if you step past the end of a global variable you are probably not going to interfere with the runtime stack. Your problem probably still exists but in a sneaky way. Try the following declare your values array as
double ValuesArray[190];
Guess what it probably will not crash your dialog this time either. Why does it not crash the dialog? It is overwriting other variables in your global data that could come back and cause your program to work in an unexpected way. John
I just tested my theory and it worked. I created a dialog application and in my OnOk() function I did the following which caused a crash because I am stepping past the end of ValuesArray.
void CStackOverflowDlg::OnOK()
{
double ValuesArray[200];
for(int i=0;i < 300;i++) {
ValuesArray[i] = 100.0;
}
}Guess what moving ValuesArray to the global namespace and there was no crash at all.
double ValuesArray[200];
void CStackOverflowDlg::OnOK()
{
// double ValuesArray[200];
for(int i=0;i < 300;i++) {
ValuesArray[i] = 100.0;
}
}But have I fixed the problem? Absolutely NO! I still am using more items ValuesArray than I declared... John
-
The array is define global and public in the header file. I have not yet been able to use the array, because when I define it the application stops before I'm able to do anything. a bit of the source code:
#pragma once #include "afxwin.h" #define MAX_DATARULES 256 // CSpeechControlDlg dialog class CSpeechControlDlg : public CDialog { // Construction public: CSpeechControlDlg(CWnd* pParent = NULL); HWND m_hwndForeground; HWND m_oldfocus; char *m_pData[MAX_DATARULES][4]; ....
I need to use an array to fill it with information about speech commands which the application recognizes. It compiles, and links, but stops with an access violation exception! When I created a new project this initialization worked perfectly, this makes me very confused!First, with this declatration: char *m_pData[MAX_DATARULES][4]; you just declare a two dimensional array that will contains .... POINTERS TO CHAR !!! I think you have to remove the * !! Second, if you never use the array, the problem is not because of your array ! Are you sure you never try to write or try to read in it ??? If you try to access one 'cell', be carefull: in your case it's not a char but a pointer. And this pointer is NOT INITIALIZED so, it contins an invalid adress. That's probably why you have an error !
-
The array is define global and public in the header file. I have not yet been able to use the array, because when I define it the application stops before I'm able to do anything. a bit of the source code:
#pragma once #include "afxwin.h" #define MAX_DATARULES 256 // CSpeechControlDlg dialog class CSpeechControlDlg : public CDialog { // Construction public: CSpeechControlDlg(CWnd* pParent = NULL); HWND m_hwndForeground; HWND m_oldfocus; char *m_pData[MAX_DATARULES][4]; ....
I need to use an array to fill it with information about speech commands which the application recognizes. It compiles, and links, but stops with an access violation exception! When I created a new project this initialization worked perfectly, this makes me very confused!Ok. For each data rule what are you storing?? Your code is written so that for each data rule you are storing a 4 pointers to characters. Here is what I mean:
#define DATA_SIZE 100
m_pData[RULE_0][0] = new char(DATA_SIZE);
m_pData[RULE_0][1] = new char(DATA_SIZE);
m_pData[RULE_0][2] = new char(DATA_SIZE);
m_pData[RULE_0][3] = new char(DATA_SIZE);strcpy(m_pData[RULE_0][0],"This is a test");
strcpy(m_pData[RULE_0][1],"This is a test");
strcpy(m_pData[RULE_0][2],"This is a test");
strcpy(m_pData[RULE_0][3],"This is a test");I bet this is not what you want... John
-
lol, a similar problem was posted here some days ago... define char m_pData[256][4]; GLOBAL! this should solve the problem Don't try it, just do it! ;-)
I thought you were the one who had the original array question... It was spaced_out. In either case I beleive the problem was not solved and just hidden. John
-
First, with this declatration: char *m_pData[MAX_DATARULES][4]; you just declare a two dimensional array that will contains .... POINTERS TO CHAR !!! I think you have to remove the * !! Second, if you never use the array, the problem is not because of your array ! Are you sure you never try to write or try to read in it ??? If you try to access one 'cell', be carefull: in your case it's not a char but a pointer. And this pointer is NOT INITIALIZED so, it contins an invalid adress. That's probably why you have an error !
cedric moonen wrote: I think you have to remove the * !! I'm sure that you should remove the '*' :) Besides that, you are not declaring a 2 dimensional array, but an array with 3 dimensions!!! If you want to use pointers, you have to initialize the array! this should work: char m_pData[MAX_DATARULES][4]; the pointer version char *m_pData[4]; and initialize it this way: for(int i = 0; i < MAX_DATARULES;i++) m_pData[i] = (char**) malloc(sizeof(char*)); :-OI haven't been doing such things lately so the initialisation could contain some errors:-O good luck!
A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.