Very Confused By Array Initialization

I’m a complete beginner with C++ (and the Physx SDK for that matter). I’m trying to modify a snippet of code to return a custom structure of data. Here are the structs:

struct Vector3f {
	float x;
	float y;
	float z;

	Vector3f() {
		x = 0.0f;
		y = 0.0f;
		z = 0.0f;
	}

	Vector3f(float xNew, float yNew, float zNew) {
		x = xNew;
		y = yNew;
		z = zNew;
	}
} Vector3;

struct MeshData {
	Vector3f* vertices;
	Vector3f* normals;
	physx::PxU16* triangles;
};

And here is the main code in question:

PxDefaultMemoryInputData id(buf.getData(), buf.getSize());
PxConvexMesh* mesh = gPhysics->createConvexMesh(id);
PxU32 nbVerts = mesh->getNbVertices();
const PxVec3* convexVerts = mesh->getVertices();
const PxU8* indexBuffer = mesh->getIndexBuffer();
const PxU32 nbPolygons = mesh->getNbPolygons();

const PxU32 numberOfVertices = mesh->getNbVertices();

Vector3f* vertices = new Vector3f[numberOfVertices];
Vector3f* normals = new Vector3f[nbPolygons];
PxU16* triangles = new PxU16[nbPolygons * 6];

PxU32 offset = 0;
for(PxU32 i = 0; i<nbPolygons; i++) {
	PxHullPolygon face;
	bool status = mesh->getPolygonData(i, face);
	PX_ASSERT(status);

	const PxU8* faceIndices = indexBuffer + face.mIndexBase;
	for(PxU32 j = 0; j<face.mNbVerts; j++) {
		vertices[offset + j] = Vector3f(convexVerts[faceIndices[j]].x, convexVerts[faceIndices[j]].y, convexVerts[faceIndices[j]].z);
		normals[offset + j] = Vector3f(face.mPlane[0], face.mPlane[1], face.mPlane[2]);
	}

	for(PxU32 j = 2; j<face.mNbVerts; j++) {
		*triangles++ = PxU16(offset);
		*triangles++ = PxU16(offset + j);
		*triangles++ = PxU16(offset + j - 1);
	}

	offset += face.mNbVerts;
}


meshData.vertices = vertices;
meshData.normals = normals;
meshData.triangles = triangles;
return meshData;

Through some mind-numbing debugging I’ve found that the vertices and normals arrays are causing memory corruption. I’ve tried quite a few different things, but they won’t even compile.

Thank you for your time!

This is what I’m doing, I can’t remember where I got this code from but it works for me, I hope it may be of some help to you.

const PxU32 nbPolys = mesh->getNbPolygons();
	const PxU8* polygons = mesh->getIndexBuffer();
	const PxVec3* verts = mesh->getVertices();
	PxU32 nbVerts = mesh->getNbVertices();
	PX_UNUSED(nbVerts);

	PxU32 numTotalTriangles = 0;
	for (PxU32 i = 0; i < nbPolys; i++)
	{
		PxHullPolygon data;
		mesh->getPolygonData(i, data);

		const PxU32 nbTris = data.mNbVerts - 2;
		const PxU8 vref0 = polygons[data.mIndexBase + 0];
		PX_ASSERT(vref0 < nbVerts);
		for (PxU32 j = 0; j < nbTris; j++)
		{
			const PxU32 vref1 = polygons[data.mIndexBase + 0 + j + 1];
			const PxU32 vref2 = polygons[data.mIndexBase + 0 + j + 2];

			//generate face normal:
			PxVec3 e0 = verts[vref1] - verts[vref0];
			PxVec3 e1 = verts[vref2] - verts[vref0];

			PX_ASSERT(vref1 < nbVerts);
			PX_ASSERT(vref2 < nbVerts);

			PxVec3 fnormal = e0.cross(e1);
			fnormal.normalize();

			if (numTotalTriangles * 3 < MAX_NUM_MESH_VEC3S)
			{
				//gVertexBuffer[numTotalTriangles * 6 + 0] = fnormal;
				gVertexBuffer[numTotalTriangles * 3 + 0] = verts[vref0];
				//gVertexBuffer[numTotalTriangles * 6 + 2] = fnormal;
				gVertexBuffer[numTotalTriangles * 3 + 1] = verts[vref1];
				//gVertexBuffer[numTotalTriangles * 6 + 4] = fnormal;
				gVertexBuffer[numTotalTriangles * 3 + 2] = verts[vref2];
				numTotalTriangles++;
			}
		}
	}

@NickM - Thanks for the help, but I forgot to come back here and mention I sorted it out.

In the end I initialized the arrays with gAllocator->allocate(…).