Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Two dimensional array problem

Two dimensional array problem

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structuresquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S superstein

    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

    A Offline
    A Offline
    Alexander M
    wrote on last edited by
    #2

    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! ;-)

    J S 3 Replies Last reply
    0
    • S superstein

      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

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #3

      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 ?

      1 Reply Last reply
      0
      • S superstein

        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

        J Offline
        J Offline
        John M Drescher
        wrote on last edited by
        #4

        :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

        1 Reply Last reply
        0
        • A Alexander M

          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! ;-)

          J Offline
          J Offline
          John M Drescher
          wrote on last edited by
          #5

          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

          J 1 Reply Last reply
          0
          • A Alexander M

            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! ;-)

            S Offline
            S Offline
            superstein
            wrote on last edited by
            #6

            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!

            C J 2 Replies Last reply
            0
            • J John M Drescher

              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

              J Offline
              J Offline
              John M Drescher
              wrote on last edited by
              #7

              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

              1 Reply Last reply
              0
              • S superstein

                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!

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #8

                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 !

                B 1 Reply Last reply
                0
                • S superstein

                  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!

                  J Offline
                  J Offline
                  John M Drescher
                  wrote on last edited by
                  #9

                  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

                  1 Reply Last reply
                  0
                  • A Alexander M

                    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! ;-)

                    J Offline
                    J Offline
                    John M Drescher
                    wrote on last edited by
                    #10

                    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

                    1 Reply Last reply
                    0
                    • C Cedric Moonen

                      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 !

                      B Offline
                      B Offline
                      Bob Stanneveld
                      wrote on last edited by
                      #11

                      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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups