Length of Dynamically Created Multidemensional Arrays

Discussion in 'C++' started by GraemeC, May 1, 2007.

  1. GraemeC

    GraemeC Guest

    How can you determine the length of a dynamically created
    multidimensional array?

    Tried using sizeof unsuccesufully. It always returns 4, presumably
    becuase I am using ints and it is just telling me the size of the
    pointer myArray

    Many thanks

    G


    int ** myArray = 0;
    int Rows;
    int Cols;

    cout << "Number of Rows?" << endl;
    cin >> Rows;

    myArray = new int*[Rows];

    for (int i_r = 0; i_r < Rows; i_r++)
    {
    cout << "Number of cols?" << endl;
    cin >> Cols;

    myArray[i_r] = new int[Cols];
    for (int i_c=0; i_c < Cols; i_c++) {
    cout << "Number" << i_c << endl;
    cin >> myArray[i_r][i_c];
    }
    }
    }

    return 0;
    }
     
    GraemeC, May 1, 2007
    #1
    1. Advertising

  2. GraemeC

    Rolf Magnus Guest

    GraemeC wrote:

    > How can you determine the length of a dynamically created
    > multidimensional array?


    By remembering the size you specified when creating it. Alternatively, use
    std::vector instead of an array.

    > Tried using sizeof unsuccesufully. It always returns 4, presumably
    > becuase I am using ints and it is just telling me the size of the
    > pointer myArray


    It has nothing to do with int. If you use sizeof on a pointer, you get the
    size of a pointer.
     
    Rolf Magnus, May 1, 2007
    #2
    1. Advertising

  3. GraemeC

    pmouse Guest

    To put it simply: You can't.

    Therefore, you either store the length somewhere, or you put a marker
    at the boundaries of the array.

    look for STL's vector type.

    Regards,

    PQ
     
    pmouse, May 2, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,746
    Eric J. Roode
    Jul 17, 2003
  2. Hendrix
    Replies:
    1
    Views:
    1,684
    Ivan Vecerina
    Jun 29, 2003
  3. Philipp
    Replies:
    21
    Views:
    1,134
    Philipp
    Jan 20, 2009
  4. msimmons
    Replies:
    0
    Views:
    497
    msimmons
    Jul 16, 2009
  5. David Belohrad
    Replies:
    1
    Views:
    897
    HT-Lab
    Dec 16, 2011
Loading...

Share This Page