Thread: multi dimensional array at runtime

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    multi dimensional array at runtime

    Hi
    I have 2 question:
    1-)I know vector class in stl but i want to make a multi-dimensional array.While user enter some parameters its lengh will grow.
    How can i do this with "pure c".Only way linked list?Or are there another ways?

    2-)In c++
    i try :
    Code:
    #include <iostream>
     using namespace std; 
    int main() 
    { 
    int x,y;
     cout << "first number";
     cin >> x; cout << "second nunmber";
     cin >> y; 
    int * a[][] = new int[x][y];
     delete[] a;
     return 0;
     }
    It gives error.
    How can i declare multi dimensional array with new in c++

    thanks.

  2. #2
    Registered User
    Join Date
    Dec 2004
    Location
    UK
    Posts
    109
    You could make a single dimensional array with size x*y
    Cell i,j would be accessed by the index i * y + j

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    34
    Code:
    //Like This//
    //Change//
    int * a[][] = new int[x][y];
    //To//
    int a[x][y];
    Last edited by unkownname; 05-10-2006 at 05:17 PM.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    5
    Hi, "sawer".

    In pure "C", I think your only option for creating dynamic arrays is malloc. You input the matrix or vector dimension, then use malloc to allocate the appropriate amount of space, do your computations, and then deallocate the memory before exiting the program.

    In C++, you could do something similar using "new" and "free"; however, the STL's <vector> class is the best way to go, even for multidimenional arrays.

    I have posted three versions of the same small program off the following page:

    Dynamic Arrays Intro

    Versions 1 and 2 are the ones you should focus on, unless you happen to be using an older comiler. They should give you the basic concepts for building your own programs.

    Regards,


    David

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    new returns a pointer, so in order to allocate a 2D array, first allocate an array of pointers:
    Code:
    int a(2);
    int** arr = new int* [a];
    Then you can allocate each sub-array seperately:
    Code:
    int b(3);
    for(int i(0); i!=a; ++i)
       arr[i] = new int[b];
    the STL is definitely the best way, otherwise I believe 'matrix' libraries exist which do this sort of thing.
    If you insist on creating 2D arrays from scratch, give this page a read
    http://www.parashift.com/c++-faq-lit...html#faq-16.16

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by unkownname
    Code:
    //Like This//
    //Change//
    int * a[][] = new int[x][y];
    //To//
    int a[x][y];
    Dynamic means it's created at runtime and stored on the freestore. Your example of:
    Code:
    int a[x][y];
    is created at compile time.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    34
    Alright Dont see much Difference besides the way its created but oh well I tried.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Dont see much Difference besides the way its created but oh well I tried.
    The difference is that your version is illegal in C++ unless x and y are const (although it is legal in C and will work on at least one C++ compiler as an extension).

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    34
    Are you sure of that? Like really sure Because I tried it out on DevC++ and it worked.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Dev-C++ uses the one C++ compiler that allows it as an extension (gcc).

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    34
    oh didnt see the at least on one c++ compiler And I know devc++ is just a GUI for gcc c++ compiler.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, if x and y are variables,
    Code:
    int a[x][y];
    is very illegal.

  13. #13
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    Quote Originally Posted by unkownname
    Are you sure of that? Like really sure Because I tried it out on DevC++ and it worked.
    I'm surprised at that, but Daved is correct. It is non-standard C++.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    34
    Alright I get your point it just seemed to work Dont get all flamy at me. your forgetting the reason for this post.

    One more thing I know its illegal first of all But its being decalred after it has x and y so wouldnt that make it legal?
    Last edited by unkownname; 05-10-2006 at 05:53 PM.

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by unkownname
    Alright I get your point it just seemed to work Dont get all flamy at me. your forgetting the reason for this post.

    One more thing I know its illegal first of all But its being decalred after it has x and y so wouldnt that make it legal?
    The reason that the standard disallows you from doing this is a bit like a stupidity protection system - the compiler has no guarantee that the non-const variable will not be changed afterwards.

    The "stupidity protection" is for people who might otherwise try something like this:
    Code:
    int y=5;
    int x[y];
    // code to populate x goes here..
    // "oh, i want to resize my array.. i'll just do this..."
    y=7;
    x[6] = 42;
    // Bad, bad, bad..
    There may actually be other good reasons why the standard was written this way, but this is a good enough reason IMHO
    Last edited by Bench82; 05-10-2006 at 06:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem within multi dimensional array
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 12-26-2008, 08:02 AM
  2. Multi Dimensional Array Compare
    By guitarist809 in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2006, 05:03 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. defining array size at runtime
    By Sanglier in forum C Programming
    Replies: 3
    Last Post: 09-16-2002, 11:38 AM
  5. 2d arrays in C
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 04-20-2002, 11:09 AM