alloc 2d array with new

Discussion in 'C++' started by aurgathor, Mar 12, 2005.

  1. aurgathor

    aurgathor Guest

    I'm trying to allocate a 2d array in runtime
    that can subsequently be accessed like
    arr[x][y], to no avail. Is there any to
    do that in C++? If yes, how?

    TIA

    This is what I got thus far:

    #include <iostream>

    struct sq_T {
    short X;
    short Y;
    int ID;
    };

    void maker ( int x, int y ) {
    sq_T *display = new sq_T[x*y];

    // test code
    for (int j = 0; j < y; j++)
    for (int i = 0; i < x; i++)
    display[(j * x) + i].ID = i + (j*x);

    for (int j = 0; j < y; j++) {
    for (int i = 0; i < x; i++) {
    cout << display[(j * x) + i].ID << " ";
    }
    cout << endl;
    }
    }


    void main () {
    maker(5,7);
    }
    aurgathor, Mar 12, 2005
    #1
    1. Advertising

  2. "aurgathor" <> wrote...
    > I'm trying to allocate a 2d array in runtime
    > that can subsequently be accessed like
    > arr[x][y], to no avail. Is there any to
    > do that in C++? If yes, how?
    > [..]


    This is covered in the FAQ (http://www.parashift.com/c -faq-lite/)
    Victor Bazarov, Mar 12, 2005
    #2
    1. Advertising

  3. aurgathor

    Michael Mair Guest

    aurgathor wrote:
    > I'm trying to allocate a 2d array in runtime
    > that can subsequently be accessed like
    > arr[x][y], to no avail. Is there any to
    > do that in C++? If yes, how?


    Start at
    http://www.parashift.com/c -faq-lite/freestore-mgmt.html#faq-16.16
    and read also the next couple of FAQs.

    -Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Mar 13, 2005
    #3
  4. aurgathor

    aurgathor Guest

    aurgathor, Mar 13, 2005
    #4
  5. aurgathor

    Axter Guest

    aurgathor wrote:
    > I'm trying to allocate a 2d array in runtime
    > that can subsequently be accessed like
    > arr[x][y], to no avail. Is there any to
    > do that in C++? If yes, how?
    >
    > TIA
    >
    > This is what I got thus far:
    >
    > #include <iostream>
    >
    > struct sq_T {
    > short X;
    > short Y;
    > int ID;
    > };
    >
    > void maker ( int x, int y ) {
    > sq_T *display = new sq_T[x*y];
    >
    > // test code
    > for (int j = 0; j < y; j++)
    > for (int i = 0; i < x; i++)
    > display[(j * x) + i].ID = i + (j*x);
    >
    > for (int j = 0; j < y; j++) {
    > for (int i = 0; i < x; i++) {
    > cout << display[(j * x) + i].ID << " ";
    > }
    > cout << endl;
    > }
    > }
    >
    >
    > void main () {
    > maker(5,7);
    > }



    Check out the following link:
    http://www.tek-tips.com/faqs.cfm?fid=5575

    It has several more efficient methods for creating 2D array.
    Axter, Mar 13, 2005
    #5
    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. Bob
    Replies:
    1
    Views:
    602
    .NET Follower
    Feb 13, 2004
  2. Adam Hartshorne

    Heap Alloc Exception Problem

    Adam Hartshorne, Jun 2, 2005, in forum: C++
    Replies:
    6
    Views:
    565
    Peter Koch Larsen
    Jun 2, 2005
  3. B. Gandhi

    Memory Alloc/Dealloc query

    B. Gandhi, Sep 23, 2003, in forum: C Programming
    Replies:
    6
    Views:
    589
    Peter Shaggy Haywood
    Sep 25, 2003
  4. ??alloc(0) - std interpretation

    , May 17, 2004, in forum: C Programming
    Replies:
    7
    Views:
    505
  5. Tristan Wibberley
    Replies:
    3
    Views:
    768
    Default User
    Feb 14, 2008
Loading...

Share This Page