Problems initializing a dynamically allocated 2D array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • masood.iqbal@lycos.com

    Problems initializing a dynamically allocated 2D array

    I am having lots of trouble getting a simple program that initializs a
    dynamically allocated 2D array to work. My 2D array is not getting
    initialized properly, and additionally I am getting a "Null pointer
    assignment" error. Kindly help.

    Also, eventually I intend to move this logic to a separate function.
    For that, I believe, that I will need to pass a
    pointer-to-pointer-to-pointer type as an arguent. Please confirm.

    [My apologies to C purists --- I am using the new operator instead of
    malloc/calloc since I find its usage more intuitive]

    Masood



    /*************** *************** *************** *************** ************/
    #include <stdio.h>

    #define ROWS 3
    #define COLUMNS 5


    main()
    {
    int **tbl;
    size_t rows = ROWS;
    size_t cols = COLUMNS;
    int startVal = 2;

    tbl = new (int**)[cols];

    for(size_t i = 0; i < rows; i++)
    tbl[i] = new (int *)[rows];

    for(size_t i1 = 0; i1 < rows; i1++)
    for(size_t j1 = 0; j1 < cols; j1++)
    tbl[i1][j1] = startVal++;

    for(size_t i2 = 0; i2 < rows; i2++)
    for(size_t j2 = 0; j2 < cols; j2++)
    printf("Row: %d, Col: %d => %d\n",
    i2, j2, tbl[i2][j2]);
    return 0;
    }

  • Alf P. Steinbach

    #2
    Re: Problems initializing a dynamically allocated 2D array

    * masood.iqbal@ly cos.com:[color=blue]
    > [Cross-posted to C and C++ newsgroups, Not a Good Idea][/color]

    Please don't do that
    (except where it is an issue of interest to practitioners of both).

    Follow-up set to [comp.lang.c++].


    * masood.iqbal@ly cos.com:[color=blue]
    > I am having lots of trouble getting a simple program that initializs a
    > dynamically allocated 2D array to work. My 2D array is not getting
    > initialized properly, and additionally I am getting a "Null pointer
    > assignment" error. Kindly help.[/color]

    Don't use raw pointers.

    Use standard containers like e.g. std::vector.

    Then all your current problems disappear.


    [color=blue]
    > Also, eventually I intend to move this logic to a separate function.
    > For that, I believe, that I will need to pass a
    > pointer-to-pointer-to-pointer type as an arguent. Please confirm.[/color]

    Pass a reference to the container.

    [color=blue]
    > [My apologies to C purists --- I am using the new operator instead of
    > malloc/calloc since I find its usage more intuitive][/color]

    Using 'new' is a good idea when you really need to handle allocation
    yourself.

    Here you _don't_ need to handle allocation yourself.

    Use standard containers like e.g. std::vector.


    [color=blue]
    > /*************** *************** *************** *************** ************/
    > #include <stdio.h>
    >
    > #define ROWS 3
    > #define COLUMNS 5[/color]

    Preferentially use constants, like e.g.

    std::size_t const nRows = 3;
    std::size_t const nColumns = 5;

    [color=blue]
    > main()[/color]

    Must have 'int' return value type, both in C and C++.

    [color=blue]
    > {
    > int **tbl;[/color]

    Indentation.

    [color=blue]
    > size_t rows = ROWS;
    > size_t cols = COLUMNS;[/color]

    Those should be 'const' (already mentioned).

    [color=blue]
    > int startVal = 2;
    >
    > tbl = new (int**)[cols];[/color]

    tbl = new (int*)[cols];

    [color=blue]
    > for(size_t i = 0; i < rows; i++)
    > tbl[i] = new (int *)[rows];[/color]

    tbl[i] = new int[rows];

    [color=blue]
    > for(size_t i1 = 0; i1 < rows; i1++)[/color]

    It's a good idea to use meaningful names, e.g. 'iRow'.

    Preferentially use '++i', not 'i++'.

    See
    <url: http://home.no.net/dubjai/win32cpptut/html/w32cpptut_01_02 _11.html>.
    <url:
    http://www.parashift.c om/c++-faq-lite/operator-overloading.htm l#faq-13.11>
    <url:
    http://www.parashift.c om/c++-faq-lite/operator-overloading.htm l#faq-13.12>

    [color=blue]
    > for(size_t j1 = 0; j1 < cols; j1++)
    > tbl[i1][j1] = startVal++;[/color]

    If you had used meaningful names this would have been

    tbl[iRow][iColumn] = ++startVal;

    which hopefully you can see is incorrect (which index goes where?).

    [color=blue]
    > for(size_t i2 = 0; i2 < rows; i2++)
    > for(size_t j2 = 0; j2 < cols; j2++)
    > printf("Row: %d, Col: %d => %d\n",
    > i2, j2, tbl[i2][j2]);
    > return 0;
    > }[/color]


    Use standard containers like e.g. std::vector.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • ajk

      #3
      Re: Problems initializing a dynamically allocated 2D array

      On 30 Jan 2005 06:51:05 -0800, masood.iqbal@ly cos.com wrote:
      [color=blue]
      >main()
      >{
      >int **tbl;
      >size_t rows = ROWS;
      >size_t cols = COLUMNS;
      >int startVal = 2;
      >
      >tbl = new (int**)[cols]; // ok here allocate tbl[0..4]
      >
      >for(size_t i = 0; i < rows; i++)
      >tbl[i] = new (int *)[rows]; // tbl[0..4] = int array [0..2] ok
      >
      >for(size_t i1 = 0; i1 < rows; i1++)
      >for(size_t j1 = 0; j1 < cols; j1++) // now you are turning it around
      >tbl[i1][j1] = startVal++; // tbl[0..2][0..4] .. not good
      >
      >for(size_t i2 = 0; i2 < rows; i2++)
      >for(size_t j2 = 0; j2 < cols; j2++)
      >printf("Row: %d, Col: %d => %d\n",
      >i2, j2, tbl[i2][j2]); // tbl[0..2][0..4] also not good.
      > return 0;
      >}[/color]

      hth/ak

      --
      "Those are my principles. If you don't like them I have others."
      Groucho Marx.

      Comment

      • Gianni Mariani

        #4
        Re: Problems initializing a dynamically allocated 2D array

        masood.iqbal@ly cos.com wrote:[color=blue]
        > I am having lots of trouble getting a simple program that initializs a
        > dynamically allocated 2D array to work. My 2D array is not getting
        > initialized properly, and additionally I am getting a "Null pointer
        > assignment" error. Kindly help.[/color]

        Use a matrix class ... this is just an example (although it works) -
        there are some extensive matrix libraries you could use and ones that
        are very efficient if the dimensions are known.

        #include <vector>

        template <typename w_elem_type>
        class matrix
        {
        public:
        typedef int t_Size;

        t_Size m_columns;
        t_Size m_rows;

        std::vector<w_e lem_type> m_data;

        matrix( t_Size i_columns = 0, t_Size i_rows = 0 )
        : m_columns( i_columns ),
        m_rows( i_rows ),
        m_data( i_columns * i_rows )
        {
        }

        w_elem_type * operator[]( t_Size i_index )
        {
        return & ( m_data[ i_index * m_rows ] );
        }

        template <typename w_Type, int w_columns, int w_rows>
        matrix( const w_Type (&i_array)[w_columns][w_rows] )
        : m_columns( w_columns ),
        m_rows( w_rows ),
        m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) )
        {
        }

        };

        #include <iostream>

        double array[3][4] = {
        { 1.0, 2.0, 3.3, 4.4 },
        { 1.0, 2.0, 3.3, 4.4 },
        { 1.0, 2.0, 3.3, 4.5 },

        };

        int main()
        {
        matrix<float> mat1( 3, 4 );
        matrix<float> mat2;
        matrix<float> mat3( array );

        mat2 = mat3;

        std::cout << mat2[2][3] << "\n";
        }
        [color=blue]
        >
        > Also, eventually I intend to move this logic to a separate function.
        > For that, I believe, that I will need to pass a
        > pointer-to-pointer-to-pointer type as an arguent. Please confirm.[/color]

        If you use the martix class above, you can pass it by reference, const
        reference or value.

        void foo( matrix<int> & modify_me );
        void foo( const matrix<int> & just_read_me );
        void foo( const matrix<int> & make_a_copy_of_ me );

        Note that the matrix is parameterized on element type.
        [color=blue]
        >
        > [My apologies to C purists --- I am using the new operator instead of
        > malloc/calloc since I find its usage more intuitive][/color]

        Try not posting to the C groups if you're really interested in C++.
        [color=blue]
        >
        > /*************** *************** *************** *************** ************/
        > #include <stdio.h>
        >
        > #define ROWS 3
        > #define COLUMNS 5
        >
        >
        > main()
        > {
        > int **tbl;
        > size_t rows = ROWS;
        > size_t cols = COLUMNS;
        > int startVal = 2;
        >
        > tbl = new (int**)[cols];[/color]

        tbl = new (int*)[cols]; // this is probably what you wanted
        [color=blue]
        >
        > for(size_t i = 0; i < rows; i++)
        > tbl[i] = new (int *)[rows];[/color]

        tbl[i] = new (int)[rows]; // again
        [color=blue]
        >
        > for(size_t i1 = 0; i1 < rows; i1++)
        > for(size_t j1 = 0; j1 < cols; j1++)
        > tbl[i1][j1] = startVal++;[/color]

        tbl[j1][i1] = startVal++; // probably transposed i & j
        [color=blue]
        >
        > for(size_t i2 = 0; i2 < rows; i2++)
        > for(size_t j2 = 0; j2 < cols; j2++)
        > printf("Row: %d, Col: %d => %d\n",
        > i2, j2, tbl[i2][j2]);[/color]

        .... again transposed i and j
        [color=blue]
        > return 0;
        > }
        >[/color]

        Comment

        • CBFalconer

          #5
          Re: Problems initializing a dynamically allocated 2D array

          masood.iqbal@ly cos.com wrote:[color=blue]
          >[/color]
          .... snip ...[color=blue]
          >
          > [My apologies to C purists --- I am using the new operator instead
          > of malloc/calloc since I find its usage more intuitive][/color]

          There is no new operator in C.
          [color=blue]
          >[/color]
          .... snip ...[color=blue]
          >
          > main()[/color]

          main returns int. Say so.
          [color=blue]
          > {[/color]
          .... snip ...[color=blue]
          >
          > tbl = new (int**)[cols];[/color]

          see above[color=blue]
          >
          > for(size_t i = 0; i < rows; i++)
          > tbl[i] = new (int *)[rows];[/color]

          and see above. Try indenting your code or using a real newsreader.

          In addition, do not crosspost without setting followups to one
          group alone. F'ups set to the one group where it may be topical.

          --
          "If you want to post a followup via groups.google.c om, don't use
          the broken "Reply" link at the bottom of the article. Click on
          "show options" at the top of the article, then click on the
          "Reply" at the bottom of the article headers." - Keith Thompson


          Comment

          • Old Wolf

            #6
            Re: Problems initializing a dynamically allocated 2D array

            masood.iqbal@ly cos.com wrote:[color=blue]
            > I am having lots of trouble getting a simple program that initializs[/color]
            a[color=blue]
            > dynamically allocated 2D array to work. My 2D array is not getting
            > initialized properly, and additionally I am getting a "Null pointer
            > assignment" error. Kindly help.[/color]
            [color=blue]
            > #include <stdio.h>
            >
            > #define ROWS 3
            > #define COLUMNS 5
            >
            > main()
            > {
            > int **tbl;
            > size_t rows = ROWS;
            > size_t cols = COLUMNS;
            > int startVal = 2;
            >
            > tbl = new (int**)[cols];[/color]

            Bizarre that I had never seen someone do this until yesterday,
            but now I've seen it twice (it came up in another thread).
            What you mean is:

            .. tbl = new int*[cols];

            tbl is a pointer to pointer to int. So you must create
            pointer(s) to int for it to point at.

            The syntax you wrote is actually illegal (Comeau C++ would have
            told you that). But some compilers parse it as:

            .. tbl = (new (int**))[cols]

            ie. creating a new pointer to pointer to int, and then
            dereferencing it (causing undefined behaviour, probably
            the cause of the errors you reported).

            For more information, see the thread in comp.std.c++
            titled "Strange new/new() problem".

            [color=blue]
            > for(size_t i = 0; i < rows; i++)
            > tbl[i] = new (int *)[rows];[/color]

            Ditto: tbl[i] = new int[rows];

            tbl[i] is a pointer to int. It points to ints.
            Obviously, trying to point it to a pointer-to-int will
            get you into trouble.
            [color=blue]
            > for(size_t i1 = 0; i1 < rows; i1++)
            > for(size_t j1 = 0; j1 < cols; j1++)
            > tbl[i1][j1] = startVal++;[/color]

            That should be tbl[j1][i1]

            Remember that you created 'cols' pointers to int, and
            each one points to a block of 'rows' ints.
            [color=blue]
            > for(size_t i2 = 0; i2 < rows; i2++)
            > for(size_t j2 = 0; j2 < cols; j2++)
            > printf("Row: %d, Col: %d => %d\n",
            > i2, j2, tbl[i2][j2]);[/color]

            Ditto.
            [color=blue]
            > return 0;[/color]

            You should delete[] all of the stuff you allocated.
            [color=blue]
            > }[/color]

            You could have saved yourself a lot of trouble by using
            a vector of vectors, instead of new and delete.

            Comment

            • Dave Thompson

              #7
              Re: Problems initializing a dynamically allocated 2D array

              On Sun, 30 Jan 2005 15:15:20 GMT, alfps@start.no (Alf P. Steinbach)
              wrote:
              [color=blue]
              > * masood.iqbal@ly cos.com:[color=green]
              > > [Cross-posted to C and C++ newsgroups, Not a Good Idea][/color]
              >
              > Please don't do that
              > (except where it is an issue of interest to practitioners of both).
              >
              > Follow-up set to [comp.lang.c++].
              >[/color]
              Actually they weren't. (I've made the same mistake in the past, it's
              not as convenient with Agent as one might like.) <G>

              <snip>[color=blue][color=green]
              > > main()[/color]
              >
              > Must have 'int' return value type, both in C and C++.
              >[/color]
              In C99, and (any) C++.

              <snip>[color=blue][color=green]
              > > tbl = new (int**)[cols];[/color]
              >
              > tbl = new (int*)[cols];
              >[/color]
              No, that's also a (somewhat more obscure) mistake. It parses as
              tbl = ( new (int*) ) [cols]
              which allocates a single pointer, uninitialized, and tries to
              subscript it. Except in (some versions of?) g++, which "fixes" it for
              you. To be standard you want
              tbl = new int * [cols];

              <snip>

              - David.Thompson1 at worldnet.att.ne t

              Comment

              • apoorv

                #8
                Re: Problems initializing a dynamically allocated 2D array

                main()[color=blue]
                >{
                >int **tbl;
                >size_t rows = ROWS;
                >size_t cols = COLUMNS;
                >int startVal = 2;
                >
                >tbl = new (int**)[cols]; // ok here allocate tbl[0..4]
                >
                >for(size_t i = 0; i < cols; i++) //here is simple prob u are allocating[/color]
                for cols and accessing till row??????[color=blue]
                >tbl[i] = new (int *)[rows]; // tbl[0..4] = int array [0..2][/color]
                ok[color=blue]
                >
                >for(size_t i1 = 0; i1 < rows; i1++)
                >for(size_t j1 = 0; j1 < cols; j1++) // now you are turning it around
                >tbl[i1][j1] = startVal++; // tbl[0..2][0..4] .. not good
                >
                >for(size_t i2 = 0; i2 < rows; i2++)
                >for(size_t j2 = 0; j2 < cols; j2++)
                >printf("Row: %d, Col: %d => %d\n",
                >i2, j2, tbl[i2][j2]); // tbl[0..2][0..4] also not good.
                > return 0;
                >}[/color]



                Comment

                Working...