Array intialization

Discussion in 'C++' started by Drak I, Jun 23, 2004.

  1. Drak I

    Drak I Guest

    Hello all,
    I have very basic question on intializing two dimenstional array. Why cant I
    initialize a two dimensional int array like the following ?

    int **intArray = new int[10][20]

    What is the best way to intialize such array if I need to pass the size as
    variables, like int [rows][columns], where rows and columns has the size of the
    array.

    Thanks in advance
    Drak

    --
    Use our news server 'news.foorum.com' from anywhere.
    More details at: http://nnrpinfo.go.foorum.com/
    Drak I, Jun 23, 2004
    #1
    1. Advertising

  2. Drak I wrote:
    >
    > Hello all,
    > I have very basic question on intializing two dimenstional array. Why cant I
    > initialize a two dimensional int array like the following ?
    >
    > int **intArray = new int[10][20]
    >
    > What is the best way to intialize such array if I need to pass the size as
    > variables, like int [rows][columns], where rows and columns has the size of the
    > array.


    This is a FAQ.

    >
    > Thanks in advance
    > Drak
    >
    > --
    > Use our news server 'news.foorum.com' from anywhere.
    > More details at: http://nnrpinfo.go.foorum.com/



    --
    Karl Heinz Buchegger, GASCAD GmbH
    Teichstrasse 2
    A-4595 Waldneukirchen
    Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
    email: Web: www.gascad.com

    Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
    Karl Heinz Buchegger, Jun 23, 2004
    #2
    1. Advertising

  3. Drak I wrote:
    >
    > Hello all,
    > I have very basic question on intializing two dimenstional array. Why cant I
    > initialize a two dimensional int array like the following ?
    >
    > int **intArray = new int[10][20]
    >
    > What is the best way to intialize such array if I need to pass the size as
    > variables, like int [rows][columns], where rows and columns has the size of the
    > array.
    >


    This is a FAQ

    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.15

    Make sure you read the following items also.


    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jun 23, 2004
    #3
  4. On 23 Jun 2004 08:18:43 GMT, Drak I <programmerr@yahoo_NOSPAM_.com>
    wrote:

    >
    >Hello all,
    >I have very basic question on intializing two dimenstional array. Why cant I
    >initialize a two dimensional int array like the following ?
    >
    >int **intArray = new int[10][20]
    >
    >What is the best way to intialize such array if I need to pass the size as
    >variables, like int [rows][columns], where rows and columns has the size of the
    >array.
    >
    >Thanks in advance
    >Drak


    Scott Meyers has a nice chapter on implementing a 2-dimensional array
    class in "More Effective C++", item 30: "Proxy classes".

    However, you can also use a vector of vectors instead of unsafe
    arrays. Note that vectors -- unlike arrays -- can also be dynamically
    sized, so this is what you need if your dimensions aren't known at
    compile time.

    // e.g.:

    #include <iostream>
    #include <ostream>
    #include <vector>

    typedef std::vector<int> IntVec;
    typedef std::vector<IntVec> IntArray2D;

    const int IDX_1 = 10;
    const int IDX_2 = 20;

    int main() {
    using std::cout;
    using std::endl;

    IntVec dummy(IDX_2); // reserving space in the ctor
    IntArray2D x(IDX_1); // avoids unnecessary reallocations

    int c=0;
    for (int i=0; i<IDX_1; ++i) {
    for (int j=0; j<IDX_2; ++j, c++)
    dummy[j] = (c);
    x = dummy;
    }
    for (int i=0; i<IDX_1; ++i) {
    cout << "x[" << i << "]:";
    for (int j=0; j<IDX_2; ++j)
    cout << " " << x[j];
    cout << endl;
    }
    return 0;
    }

    I'm not sure how to reserve enough space up front for IntArray2D,
    though; assigning directly to x[j] instead of filling up the dummy
    will compile, but then gives me an access violation at run-time. The
    use of a temporary vector<int> (i.e. "dummy") seems necessary in order
    for it to work.

    Once the 2D-vector has been initialized, it should not be a problem to
    assign directly using just the index values.

    --
    Bob Hairgrove
    Bob Hairgrove, Jun 23, 2004
    #4
  5. "Drak I" <programmerr@yahoo_NOSPAM_.com> wrote in message
    news:...
    >
    > Hello all,
    > I have very basic question on intializing two dimenstional array. Why cant

    I
    > initialize a two dimensional int array like the following ?
    >
    > int **intArray = new int[10][20]
    >


    Because the types are different. intArray is type int** but new int[10][20]
    is type int (*)[20]. Not the same type at all. Newbies often get this wrong
    because they don't understand the C++ type system very well.


    > What is the best way to intialize such array if I need to pass the size as
    > variables, like int [rows][columns], where rows and columns has the size

    of the
    > array.


    As Karl said, read the FAQ.

    john
    John Harrison, Jun 23, 2004
    #5
  6. Drak I

    Mats Weber Guest

    In article <>,
    Drak I <programmerr@yahoo_NOSPAM_.com> wrote:

    >I have very basic question on intializing two dimenstional array. Why cant I
    >initialize a two dimensional int array like the following ?
    >
    >int **intArray = new int[10][20]


    using vector< vector<int> > makes it all much simpler.
    Mats Weber, Jun 23, 2004
    #6
    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. john
    Replies:
    2
    Views:
    433
    valentin tihomirov
    Jul 5, 2005
  2. john

    Intialization

    john, Sep 22, 2005, in forum: VHDL
    Replies:
    0
    Views:
    516
  3. Replies:
    1
    Views:
    960
    Stefan Schulz
    Jan 2, 2006
  4. =?iso-8859-1?Q?Juli=E1n?= Albo

    Members of anonymous unions in intialization lists

    =?iso-8859-1?Q?Juli=E1n?= Albo, Nov 25, 2003, in forum: C++
    Replies:
    1
    Views:
    329
    Victor Bazarov
    Nov 25, 2003
  5. Eric
    Replies:
    9
    Views:
    579
    Patricia Shanahan
    Nov 22, 2006
Loading...

Share This Page