• Advertisement

Archived

This topic is now archived and is closed to further replies.

huge arrays

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

void main() { float arr[250][250][250]; } Why I get some unhandled exceptions? I use WinXP Home,MSVC++.NET Std and 512 MB RAM.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
because you''re allocating it all on the stack (local function variables are allocated on the stack) which by default is 1MB in windows programs.

Share this post


Link to post
Share on other sites
int main()
{
float* arr = new float[250][250][250];
delete [] arr; // hm, delete [][][] arr; ?
return 0;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by flangazor
int main()
{
float* arr = new float[250][250][250];
delete [] arr; // hm, delete [][][] arr; ?
return 0;
}

Syntax error. That''s far from the right way to do it!

Share this post


Link to post
Share on other sites

float* arr = new float[250][250][250];


this doesn''t compile but casting the return of new to a float* works like so:


float* arr = (float*)new float[250][250][250];



My 2D game engine

Share this post


Link to post
Share on other sites
I would go with a vector of vectors of vectors. Something a little like:


std::vector<std::vector<std::vector<int> > > array(250, std::vector<std::vector<int> >(250, std::vector<int>(250)));


Or if you dont like ugly code, maybe use boost::multi_array

http://www.boost.org/libs/multi_array/doc/user.html

Plus it should be int main() not void main().

Hope that helps

Share this post


Link to post
Share on other sites
Or without casting:

float *arr = new float[250*250*250];
delete [] arr;

Share this post


Link to post
Share on other sites
ala google:

http://cpptips.hyperformix.com/cpptips/alloc_multi_dim

Share this post


Link to post
Share on other sites
Umm is this thread C or C++ if its C than ok VOID MAIN() is ok but in C++ Mmain must return an int (which is why gcc gives u an error for void main())



Share this post


Link to post
Share on other sites
quote:
Original post by DerAnged
Umm is this thread C or C++ if its C than ok VOID MAIN() is ok but in C++ Mmain must return an int (which is why gcc gives u an error for void main())



void main() is not really ok in C either. To quote the ISO/IEC 9899:1999 C standard

quote:
5.1.2.2.1 Program startup
The function called at program startup is named main. The implementation declares no
prototype for this function. It shall be defined with a return type of int and with no
parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be
used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }



Share this post


Link to post
Share on other sites
Ok, thanks for all replies.
In general I am using int main(). There is no problem.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by gmkwo
Ok, thanks for all replies.
In general I am using int main(). There is no problem.

Just curious: What suggestion did you end up using? Half of them were invalid or just bad.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
He probaly used good half

Share this post


Link to post
Share on other sites
this is pretty straight foward dynamic memory allocation:

int xSize, ySize, zSize; // Set these to ur dimenstions



float*** myarray;
myarray = new float**[xSize];
for(int i=0; i < xSize; i++)
{
myarray[i] = new float*[ySize];
for(int j=0; j < ySize; j++)
myarray[i][j] = new float[zSize];
}



// Then you need to release that memory when ur done with it in reverse order...


for(i=0; i < xSize; i++)
{
for(int j=0; j < ySize; j++)
delete [] myarray[i][j];

delete [] myarray[i];
}
delete myarray;

I hope that helps...

[edited by - samgzman on January 14, 2004 11:53:28 PM]

Share this post


Link to post
Share on other sites

  • Advertisement