quick question about creating a matrix using the new operator

This is a discussion on quick question about creating a matrix using the new operator within the C++ Programming forums, part of the General Programming Boards category; Hi@all Please can anyone tell me why when I run this program, I am getting memory fault error.. Code: 1 ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    29

    Unhappy quick question about creating a matrix using the new operator

    Hi@all

    Please can anyone tell me why when I run this program, I am getting memory fault error..

    Code:
          1 #include<iostream>
          2 using namespace std;
          3 int main()
          4 {
          5
          6
          7         int** M;
          8          M= new int* [5];
          9         for(int i=0; i<5; i++)
         10
         11                 for(int j=0; j<5; j++)
         12                 M[i][j]=5;
         13
         14
         15         for(int k=0; k<5; k++)
         16                 for(int m=0; m<5; m++)
         17                         cout<<M[k][m];
         18          return 0;
         19 }
    Not to know, not to learn is shame

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Because you write in memory you didn't allocate.
    Probably this is what you want. Notice the difference?
    Code:
    #include<iostream>
    using namespace std;
    
    int main()     
    {
          int i,j;
          int** M;
          M= new int* [5];
          for(i=0; i<5; i++)
                  M[i]= new int [5];
                  
          for(i=0; i<5; i++)     
                   for(j=0; j<5; j++)
                            M[i][j]=5;
         
         
         for(int k=0; k<5; k++,cout<<endl)
                 for(int m=0; m<5; m++)
                         cout<<M[k][m]<<" ";
        
         return 0;
    }
    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    This line:

    M= new int* [5]

    creates an array filled with int pointers:

    [int*, int*, int*, int*, int*]

    There are five elements, and you can access each element like this:

    M[0], M[1], M[2], etc.

    Now, what is M[0][2]? M is a single dimensional array, so there is no second index.

    What you need to do is dynamically create 5 arrays of type int and assign each one to a pointer in M[]. After you do that, the notation M[0][1] will first access the pointer at index position 0 in M[]:

    (M[0]) [1]

    and since M[0] points to an array of ints, the second index specified will access the int at position 1:

    (M[0]) [1]
    Last edited by 7stud; 04-28-2005 at 12:50 AM.

  4. #4
    Chief Code Coloniser!
    Join Date
    Apr 2005
    Posts
    121
    And don't forget to free your memory up afterwards using delete [].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. A quick question about notepad.
    By Lawn Gnomusrex in forum Tech Board
    Replies: 8
    Last Post: 11-07-2008, 08:44 AM
  3. question about overloading operators
    By *DEAD* in forum C++ Programming
    Replies: 9
    Last Post: 05-08-2008, 11:27 AM
  4. Quick question
    By gfdsa in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2008, 06:06 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 06:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21