Thread: 2-D array and pointers question

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    81

    Arrow 2-D array and pointers question

    Hi,

    I got the following code able to be compiled and built. But, it shows a runtime error. Also, I am not sure the meaning of some code. Anyone could explain them and how can I fix the runtime error?

    Thanks for help.

    gogo

    #include <iostream>
    #include <string>
    #include <cctype>
    #include <iomanip>
    using namespace std;

    int main()
    {
    const int row = 4;
    const int col = 4;

    int** ppint; //2-D array ptr

    ppint = new int*[row]; // Don't understand?? new space for 4x4 elements

    for (int i=0; i<row; i++)
    ppint[i]=new int(col); //Does it create the space for the col of each row?

    for (int j=0; j<row; j++)
    for (int k=0; k<col; k++)
    ppint[j][k]=0; //initialize all elements

    ppint[0][0]=ppint[1][1]=ppint[2][2]=ppint[3][3] = 1;

    for (int m=0; m<row; m++)
    {
    for (int n=0; n<col; n++)
    cout << ppint[m][n] << ' ';
    cout << endl;
    }

    for (int a=0; a<row; a++)
    delete [] ppint[a]; //What does it mean? to delete the memory of all rows?

    delete [] ppint; //Does it delete all col's elements?

    return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    The problem is because of a miswritten segment.

    In order to explain this, I will talk about the code line by line.

    when you write int**ppint; you are creating a pointer which should point to an integer pointer. In line
    ppint = new int*[row];
    you are doing this correctly by setting ppint to point to the first element of an integer pointer array of size row. However when you say
    for (int i=0; i<row; i++)
    ppint[i]=new int(col);

    you are getting only one new integer value of col to the address location which ppint[i] point to. But what you intent to do, is to create a 4*4 array so you should get an integer array of size col to each of
    ppint[i] pointers. It should be like this:
    for (int i=0; i<row; i++)
    ppint[i]=new int[col];

    Now you have a 4*4 array.

    The rest of the code is now Ok. You are creating a unit matrix and print it. For the lines
    for (int a=0; a<row; a++)
    delete [] ppint[a];
    what you do before the mistake is try to free memory consisting of arrays of size col each. However instead of an array, you have
    only one integer.

    What you do now is to free the memory locations to which ppint[i] pointers point, for the system. (these spaces are reclaimed by the system). Now you have got ridden each of the arrays of size 4, so it is the time to get rid of that pointers in the array. So in the line
    delete [] ppint;
    you are freeing the locations where ppint points (the ppint pointer array).

    If it is difficult to understand, reply again. I will draw a figure and it will be easier to understand.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Hi,

    Thanks for your kind help. Here is my understanding and pls justify it.

    const int row = 4;
    const int col = 4;

    int** ppint;

    //create a ppint pointer like ppint[row][col]

    Now, I have the following structure planning to point to the relevant memory address of each element.

    ppint[0][0], ppint[0][1], ppint[0][2], ppint[0][3]
    ppint[1][0], ppint[1][1], ppint[1][2], ppint[1][3]
    ppint[2][0], ppint[2][1], ppint[2][2], ppint[2][3]
    ppint[3][0], ppint[3][1], ppint[3][2], ppint[3][3]


    ppint = new int*[row];

    It dynamically points to first element of an integer pointer array of size row

    ppint[0][0]
    ppint[1][0]
    ppint[2][0]
    ppint[3][0]

    for (int i=0; i<row; i++)
    ppint[i]=new int[col];

    Yes, I made a typo here. I am sorry.

    Here is allocating memory space for the col address dynamically.

    ppint[0][0], ppint[0][1], ppint[0][2], ppint[0][3]
    ppint[1][0], ppint[1][1], ppint[1][2], ppint[1][3]
    ppint[2][0], ppint[2][1], ppint[2][2], ppint[2][3]
    ppint[3][0], ppint[3][1], ppint[3][2], ppint[3][3]

    for (int j=0; j<row; j++)
    for (int k=0; k<col; k++)
    ppint[j][k]=0;

    ppint[0][0]=ppint[1][1]=ppint[2][2]=ppint[3][3] = 1;

    Assign value to the above elements equal to 1.

    for (int m=0; m<row; m++)
    {
    for (int n=0; n<col; n++)
    cout << ppint[m][n] << ' ';
    cout << endl;
    }

    Print out all elements with a space.

    for (int a=0; a<row; a++)
    delete [] ppint[a];

    Remove space for col, so it becomes

    ppint[0][0]
    ppint[1][0]
    ppint[2][0]
    ppint[3][0]

    delete [] ppint;

    Remove the remaining row pointer array.

    I still have some confuse about the ppint = new int*[row];

    Is it allocating space for [row] ? The vertical one?

    Thanks for your detail illustration.

    gogo

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    ------------------------------------------------------------------
    after int **ppint;
    ppint
    ---------------------------------------------------------------------
    after ppint = new int*[row];
    note pp[i] are pointers.
    ppint------> [pp[0]]
    [pp[1]]
    [pp[2]]
    [pp[3]]
    -----------------------------------------------------------------------
    after for( ) ppint[i] = new int[col]
    ppint--> [pp[0]]-----------------------------------------------> [pp[0][0]]
    [pp[1]]------------------------->[pp[1][0] [pp[0][1]]
    [pp[2]]-----------> [pp[1][1] [pp[0][2]]
    [pp[3]]-----> [pp[1][2] [pp[0][3]]
    [pp[1][3]

    for pp[2] and pp[3] it is like pp[0] and pp[1]
    note pp[i][j] are integers.
    ------------------------------------------------------------------------------
    after first delete in for loop:
    ppint------> [ppint[0]]
    [ppint[1]]
    [ppint[2]]
    [ppint[3]]
    -------------------------------------------------------------------------
    after second delete:
    ppint
    -------------------------------------------------------------------------
    Well, after ppint = new int*[row];
    you are creating pointers to point to the beginning of row 0, 1, 2, 3. When you write the first loop, that pointers are set
    to the beginning of the rows.
    --------------------------------------------------------------------------
    as you can see this differs from
    int ppint[4][4] because the memory locations of different rows do not follow each other.
    However in int ppint[4][4] all the memory locations of the array is contiguous.
    --------------------------------------------------------------------------
    Hope this helps.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    I'm sorry. The lines are not oriented in the larger page. It should
    be aligned like these.
    all pp[i]

    pp[0]
    pp[1]
    pp[2]
    pp[3]
    should be aligned like that.
    ------------------------------------

    all pp[i][j]
    pp[0][0]
    pp[0][1]
    pp[0][2]
    pp[0][3]
    and same for pp[1][j], pp[2][j], pp[3][j]
    pp[1][0]
    pp[1][1]
    pp[1][2]
    pp[1][3]
    etc.

    The pp[0][j] 's and pp[1][j]'s pp[2][j]'s pp[3][j]'s should not mixed with each other.
    Sorry for the mess again.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    81
    Sorry, I still do not understand:

    after for( ) ppint[i] = new int[col]
    ppint-->
    [pp[0]]-----------------------------------------------> [pp[0][0]]
    [pp[1]]------------------------->[pp[1][0] [pp[0][1]]
    [pp[2]]-----------> [pp[1][1] [pp[0][2]]
    [pp[3]]-----> [pp[1][2] [pp[0][3]]
    [pp[1][3]

    Is it?

    after for( ) ppint[i] = new int[col]
    ppint-->
    [pp[0]]-----> [pp[0][0]] [pp[0][1] [pp[0][2]] [pp[0][3]
    [pp[1]]-----> [pp[1][0]] [pp[1][1] [pp[1][2]] [pp[1][3]
    [pp[2]]-----> [pp[2][0]] [pp[2][1] [pp[2][2]] [pp[2][3]
    [pp[3]]-----> [pp[3][0]] [pp[3][1] [pp[3][2]] [pp[3][3]

    Thanks for your kind help?

    gogo

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    The second one:

    ppint
    |
    |
    [pp[0]]-----> [pp[0][0]] [pp[0][1] [pp[0][2]] [pp[0][3]
    [pp[1]]-----> [pp[1][0]] [pp[1][1] [pp[1][2]] [pp[1][3]
    [pp[2]]-----> [pp[2][0]] [pp[2][1] [pp[2][2]] [pp[2][3]
    [pp[3]]-----> [pp[3][0]] [pp[3][1] [pp[3][2]] [pp[3][3]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM