If I dynamically allocate a multidimensional array like this:
do I still go about freeing it like this?Code:int* Example = new int[numInts][5];
Also, how would I go about passing Example to another function?Code:delete[] Example;
If I dynamically allocate a multidimensional array like this:
do I still go about freeing it like this?Code:int* Example = new int[numInts][5];
Also, how would I go about passing Example to another function?Code:delete[] Example;
... you get a compile errorOriginally Posted by LyTning94
But yes, if you had written:
then:Code:int (*Example)[5] = new int[numInts][5];
would be appropriate.Code:delete[] Example;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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:
Then, to deallocate it, you can do something like this:Code:int **example = new int*[x]; for(int i = 0; i < x; ++i) example[i] = new int[y];
Code:for (int i = 0; i < x; ++i) delete [] example[i]; delete [] example;
Thanks. How do I pass that array to another function though?
Ushakal's example? It is an int**, so you would pass it as such.
Click on the link that was provided. It answers that question, and also informs you that you should be using std::vector for this.
NEVER try to free memory more than once.
You free it when you are done with it; not before.
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):
And do I need to free the memory at the end with Example.erase()?Code:int size; cin>>size; vector<vector<int> > Example; Example.resize(size); for (i = 0; i < size; i++) { Example[i].resize(2); }
Last edited by LyTning94; 08-06-2011 at 09:48 AM.
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.
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.
Umm...do you not click on any links? Multidimensional Arrays-C++ Click the thing in red, it has answered all these questions