Hello. I have a game with code like this.

class manualSetPosition{

public:
	Ogre::ManualObject * manualDebugger;
	void SetPosition( Ogre::ManualObject * manual, Ogre::Vector3 mousepos ){
	manual->position(mousepos);


	};

};

Thats a class I use to draw a grid. I build it like this in my project.

manualSetPosition ManualSetPosition[1226];

this works fine, but what i really want is to be able to do this.

manualSetPosition ManualSetPosition[1226][1226];

I get a stack overflow. Even if that class is totally empty it does this. I really need to be able to do this for my program to work right. So my question basically is what is the limit for a class to be built as a matrix and or is there a way to extend the memory needed to do this. thanks peopless

Recommended Answers

All 3 Replies

It's going to be system/compiler dependent.

Is your matrix going to be sparsely populated? You might consider a different data structure in that case.

I don't know how big a manualSetPosition object is, but I wonder do you really need, what, 1.5 million of them? I was about to say I can make an image with 1920 x 1280 "pixels" with no trouble. But then I tried, and sure enough, it looks like I can allocate only a little over a million bytes on the stack, perhaps there's a 1MB default limit per process in Windows?

Anyway, it's easy enough to allocate it on the heap instead:

#include <iostream>
using namespace std;


int main (int argc, char *argv[]) 
{
	class FPixel {
		float r, g, b, a;
	public:
		FPixel(): r(0.0f), g(0.0f), b(0.0f), a(0.0f) {}
		void setRed(float v) { r=v; }
		float getRed() const { return r; }
	};

	cout << "size of FPixel " << sizeof(FPixel) << endl;

	// FPixel fImage[1080][1920];  // this causes a Stack Overflow exception: allocates statically -> from the stack
	FPixel *fImage = new FPixel [1080*1920];  // this works fine: allocates dynamically -> from the heap

	cout << "Type something: ";
	int val;
	cin >> val;

	return 0;
}

You are getting a stack overflow exception because SURPRISE you are using the stack to allocate 1.5 million manualSetPosition classes. Allocating that much memory on the stack is almost always an incorrect use of stack space and usually not intended. Multiple objects can be allocated on the heap and have their lifetime managed by an object allocated on the stack. An example of this is the std::vector class that is included in the Standard Template Library.

#include<vector>
using namespace std;

// create a vector of vectors to make a 2D array 
//                                                 X size                          Y size
vector< vector<manualSetPosition> > ManualPositions(1226, vector<manualSetPosition>(1226));

manualSetPosition& GetPosition(int x, int y)
{
  /* use just like a C style 2D array */
  return ManualPositions[x][y];
}

By default Windows applications reserve 1MB of stack space for the "main" thread of a process. This size can be overridden by the linker if needed. The setting within Visual Studio is located at [Project Properties->Linker->System->Stack Reserve Size]. For threads other than the default thread can be created with modified stack sizes by calling the Win32 API function CreateThread and passing the required stack size for the second argument.

As a side observation I question the reason you need 1.5 million of these objects in the first place. It looks like a design flaw. However, it is too difficult to determine based on the small code sample you provided.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.