[SOLVED]crash initializing size_t array

Get answers to all your basic programming questions. No Ogre questions, please!
drwbns
Orc Shaman
Posts: 788
Joined: Mon Jan 18, 2010 6:06 pm
Location: Costa Mesa, California
x 24

[SOLVED]crash initializing size_t array

Post by drwbns »

Hi guys, I'm wondering why I get a crash when initializing the array -

Code: Select all

		struct TerrainGroupData
		{
			TerrainGroupData(){};
			TerrainGroupData(Ogre::Vector3 ** iVertices, unsigned long ** iIndices, size_t* iFaceNum, size_t* iVertNum)

			{

				vertices = iVertices;

				indices = iIndices;

				meshIndexCount = iFaceNum;

				meshVertexCount = iVertNum;

			}
			~TerrainGroupData(){
				delete [] vertices;
				delete [] indices;
			};
			size_t *meshVertexCount;
			size_t *meshIndexCount;

			Ogre::Vector3 ** vertices;

			unsigned long ** indices;

		};

// using the struct	
			terrainGroupData.indices = new unsigned long*[1];
			size_t size = 3;

			// resize this terrain's index buffer
			terrainGroupData.indices[0] = new unsigned long[size]; // crash here
Last edited by drwbns on Fri Nov 30, 2012 12:22 am, edited 1 time in total.
User avatar
Kojack
OGRE Moderator
OGRE Moderator
Posts: 7157
Joined: Sun Jan 25, 2004 7:35 am
Location: Brisbane, Australia
x 535

Re: crash initializing size_t array

Post by Kojack »

There's no array initializing in that code. There's some pointers and pointers to pointers passed as args that are being stored in members, and there's cleanup code to delete the arrays, but at least in that code they are never being created.
If it's created outside of the code, what's the lifetime? Passing in the face and vertex count as pointers to size_t then storing the pointers instead of dereferencing them to get the current values sounds dangerous (depending on the lifetime of the memory you are pointing to).
drwbns
Orc Shaman
Posts: 788
Joined: Mon Jan 18, 2010 6:06 pm
Location: Costa Mesa, California
x 24

Re: crash initializing size_t array

Post by drwbns »

I changed it to look like -

Code: Select all

		struct TerrainGroupData
		{
			TerrainGroupData(size_t numMeshes, size_t mapSize){
				vertices = new Ogre::Vector3[mapSize*mapSize];
				indices = new unsigned long[((mapSize*mapSize)-(mapSize*2)) * 6];
			};
			TerrainGroupData(Ogre::Vector3 * iVertices, unsigned long * iIndices, size_t iFaceNum, size_t iVertNum)

			{

				vertices = iVertices;

				indices = iIndices;

				meshIndexCount = iFaceNum;

				meshVertexCount = iVertNum;

			}
			~TerrainGroupData(){
				delete [] vertices;
				delete [] indices;
			};
			size_t meshVertexCount;
			size_t meshIndexCount;

			Ogre::Vector3 * vertices;

			unsigned long * indices;

		};


and I'm trying to use it like this now, but I get a compiler error, why?

Code: Select all

TerrainGroupData* terrainGroupData;
terrainGroupData = new TerrainGroupData(totalMeshes, 513)[totalMeshes];
User avatar
vitefalcon
Orc
Posts: 438
Joined: Tue Sep 18, 2007 5:28 pm
Location: Seattle, USA
x 13

Re: crash initializing size_t array

Post by vitefalcon »

drwbns wrote:and I'm trying to use it like this now, but I get a compiler error, why?

Code: Select all

TerrainGroupData* terrainGroupData;
terrainGroupData = new TerrainGroupData(totalMeshes, 513)[totalMeshes];
That's is not how you initialize elements in C++. That's why. To fix your issue, and assuming that TerrainGroupData doesn't have a default constructor, you'll have to modify that piece of code like this:

Code: Select all

TerrainGroupData** terrainGroupData; // Pointer-to-pointer of TerrainGroupData
terrainGroupData = new (TerrainGroupData*)[totalMeshes]; // Array of pointers to TerrainGroupData
for (size_t i = 0; i < totalMeshes; ++i)
{
    terrainGroupData[i] = new TerrainGroupData(totalMeshes, 513);
}
This is if you don't like using STL containers. WIth STL containers and Ogre::SharedPtr classes you can make it something like:

Code: Select all

typedef Ogre::SharedPtr<TerrainGroupData> TerrainGroupDataPtr;
typedef std::vector<TerrainGroupDataPtr> TerrainGroupDataArray;
TerrainGroupDataArray terrainGroupData;
terrainGroupData.resize(totalMeshes]; // This step can save you a lot of time and memory if totalMeshes is too big
for (size_t i = 0; i < totalMeshes; ++i)
{
    terrainGroupData[i].bind(new TerrainGroupData(totalMeshes, 513));
}

// Access elements in terrainGroupData like so: terrainGroupData[i]->someFunction(arg1, arg2, arg3);
With the code above, you don't need to work about memory allocations or memory-leak.
Image
drwbns
Orc Shaman
Posts: 788
Joined: Mon Jan 18, 2010 6:06 pm
Location: Costa Mesa, California
x 24

Re: crash initializing size_t array

Post by drwbns »

Awesome, thanks. I ran into another problem with Havok crashing, so I need to review if I did something wrong with the data I'm giving. Thanks for your help though.