Thread: Dynamically Allocated Multidimensional Array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Dynamically Allocated Multidimensional Array

    If I dynamically allocate a multidimensional array like this:

    Code:
    int* Example = new int[numInts][5];
    do I still go about freeing it like this?

    Code:
    delete[] Example;
    Also, how would I go about passing Example to another function?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by LyTning94
    If I dynamically allocate a multidimensional array like this:
    ... you get a compile error

    But yes, if you had written:
    Code:
    int (*Example)[5] = new int[numInts][5];
    then:
    Code:
    delete[] Example;
    would be appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    You can't allocate a multidimensional array like that. See this: Multi-Dimensional Arrays - C++ Forum

    In short, to dynamically allocate a multidimensional array, you can do something like this:

    Code:
    	int **example = new int*[x];
    	for(int i = 0; i < x; ++i)
    		example[i] = new int[y];
    Then, to deallocate it, you can do something like this:

    Code:
    	  for (int i = 0; i < x; ++i)
    		delete [] example[i];
    	  delete [] example;

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Thanks. How do I pass that array to another function though?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ushakal's example? It is an int**, so you would pass it as such.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Click on the link that was provided. It answers that question, and also informs you that you should be using std::vector for this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by LyTning94 View Post
    Also, how would I go about passing Example to another function?
    If you did what laserlight showed you it's the same as any other array of arrays:

    void foo ( int Example[][5] );

    foo ( Example );

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by whiteflags View Post
    If you did what laserlight showed you it's the same as any other array of arrays:

    void foo ( int Example[][5] );

    foo ( Example );
    If I do this do I need to free the memory in the original function, the second function, or both???

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    NEVER try to free memory more than once.

    You free it when you are done with it; not before.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ushakal View Post
    You can't allocate a multidimensional array like that...
    Incorrect. It is possible, and laserlight just showed how to do it.

    Quote Originally Posted by LyTning94 View Post
    If I do this do I need to free the memory in the original function, the second function, or both???
    Is there a reason you won't use the vector-based approach?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by Elysia View Post
    Incorrect. It is possible, and laserlight just showed how to do it.
    Yes, I know, I just meant that the way the OP had written it wouldn't compile.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by Elysia View Post
    Is there a reason you won't use the vector-based approach?
    I'm still a sort of programming novice and have never used vectors before. From what I've heard though it's probably the better way to go, so I'll look into it.

    You can allocate vectors of variable size, correct? (Like in this example):

    Code:
    int size;
    
    cin>>size;
    
    vector<vector<int> > Example;
    
    Example.resize(size);
    for (i = 0; i < size; i++)
    {
           Example[i].resize(2);
    }
    And do I need to free the memory at the end with Example.erase()?
    Last edited by LyTning94; 08-06-2011 at 09:48 AM.

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    See Cprogramming.com STL Tutorial: Vector Class under the benefits heading (no you don't have to free them). Learning to use std::vector is not too difficult if you've already got the handle of arrays, and it's good that you're giving it a shot. Remember to look up full documentation for a list of functions which can conveniently modify the vectors you work with.

    EDIT: This was also interesting wrt the freeing question. http://www.velocityreviews.com/forum...eeing-mem.html

    Or just google "freeing std::vector", there are plenty of discussions on the internets.
    Last edited by Ocifer; 08-06-2011 at 10:09 AM.

  14. #14
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Thanks. How do I use push_back() with a 2D vector? Say I have the array below and want to add a 7 to the red element, do I do something like Example[0].push_back(7)?

    [1][2][3][4][5][6][][][][]
    [3][4][2][5][7][4][][][][]
    Last edited by LyTning94; 08-06-2011 at 10:20 AM.

  15. #15
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Umm...do you not click on any links? Multidimensional Arrays-C++ Click the thing in red, it has answered all these questions
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically Allocated Array Troubles
    By grogel in forum C Programming
    Replies: 7
    Last Post: 06-28-2011, 09:03 PM
  2. Dynamically Allocated Array
    By chris4 in forum C Programming
    Replies: 9
    Last Post: 05-06-2011, 10:01 AM
  3. Initializing a 2D array dynamically allocated
    By newboyc in forum C Programming
    Replies: 5
    Last Post: 02-01-2011, 01:08 PM
  4. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  5. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM