really big arrays?

Started by
16 comments, last by noVum 19 years, 8 months ago
Quote:Original post by Washu
Quite simply, the stack is a very limited resource. So you can't allocate large arrays on it. As was already said in the previous thread before you deleted it, use new/malloc or std::vector.


Wow you respond fast.. thanks guys. It should work fine now. (I originally had a "oh wait a second I'm a dumbass" moment, and then realized it still didn't work :-/ )
Advertisement
Quote:Original post by Fruny
Quote:Original post by iMalc
It's not the declaration that causes the problem btw, it's the code that accesses it.


Actually, it is the declaration that causes the problem.
You may well be correct, but at the time you posted that post we still had not been told that it was on the stack. Though many seem to have jumped to that conclusion. For all we knew it could have been a global, and it could have been other code causing the problem right?

I guess I did kind of automatically jump to the opposite conclusion though, my bad.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
Quote:Original post by Fruny
Stack overflow (1MB by default in VS, your array is precisely 3MB). Either make a bigger stack (not a good idea) or dynamically allocate your array: int *fractal_set = new int[1024*768]; or std::vector<int> fractal_set(1024*768);
No, you are not getting "2D indexing" - get over it (index = x+768*y)


You can do this though:
int main(void){	float** array;	array = new float*[20];	for(int i = 0; i < 20; i++)		array = new float[20];	for(int y = 0; y < 20; y++)	{		for(int x = 0; x < 20; x++)		{			array[x][y] = x + y*20;			std::cout << array[x][y] << std::endl;		}	}	for(i = 0; i < 20; i++)		delete[] array;        delete[] array;	return 0;}


and get 2d indexing :D (I don't know if I deleted everything right, though).

EDIT: Thanks guys, now I've got the delete right :)
Almost, you left out delete [] array. [smile]
Almost. Just add
delete[] array;

at the end.

Edit: Beaten again.
I'd go with std::vector and boost.multi_array. I guess I don't enjoy fiddling with pointers or new and delete.
id recommend using a nested vector.

vector< vector<int> > fractal_set;fractal_set.resize(1024);for(int i = 0; i < 1024; i++)   fract_set.resize(768);


now you can access the array as fractal_set[y][x] if you want. (you can set it up for [x][y] too). i might have screwed up the way i set it up, but this is the gist of it. its late and i dont feel like checking my code =).
FTA, my 2D futuristic action MMORPG
I would also go for the boost solution, as it will likely be included into the next C++ standard

This topic is closed to new replies.

Advertisement