Pass pointer array by reference - Pointer Problem

Started by
16 comments, last by robert_s 20 years ago
/*I had to perform some reconstructive surgery after the copy and paste.Next time post code in source tags.*/void main(){	Vec3D *vList;	unsigned int vertexCount;		FillInList( &vList, &vertex_count );		// Don''t forget to clean up vList when you''re done with it.	for(unsigned int i=0; i < vertexCount; ++1)	{		cout << "Vertex X =" << vList[i].x << " Y = " << vList[i].y /* etc */ << endl;		delete vList[i];	}		delete [] vList;	}void FillInList( Vec3D ** vList, unsigned int * vxCount ){ 	std::list<Vec3D> tmpVertices;		/*	Do stuff with tmpVerticies	*/		*vxCount = tmpVerticies.size(); 	(*vList) = new Vec3D*[*vxCount];		std::list<Vec3D>::iterator itr = tmpVertices.begin();		// std::copy would be usefull here, but I don''t feel like doing the grunt work.	for(int i = 0; itr != tmpVertices.end(); ++itr, ++i )	{		// We want vList to contain copies of what''s in the list. 		// Vec3D better have a copy constructor!		(*vList)[i] = new Vec3D(*itr);	}		// Redundant - tmpVerticies (and it''s contents) will be destroyed when the function exits.	//tmpVertices.clear();} 
Advertisement
Oops. Misplaced *

/*I had to perform some reconstructive surgery after the copy and paste.Next time post code in source tags.*/void main(){	Vec3D *vList;	unsigned int vertexCount;		FillInList( &vList, &vertex_count );		// Don''t forget to clean up vList when you''re done with it.	for(unsigned int i=0; i < vertexCount; ++1)	{		cout << "Vertex X =" << vList[i].x << " Y = " << vList[i].y /* etc */ << endl;		delete vList[i];	}		delete [] vList;	}void FillInList( Vec3D ** vList, unsigned int * vxCount ){ 	std::list<Vec3D> tmpVertices;		/*	Do stuff with tmpVerticies	*/		*vxCount = tmpVerticies.size(); 	(*vList) = new Vec3D[*vxCount];		std::list<Vec3D>::iterator itr = tmpVertices.begin();		// std::copy would be usefull here, but I don''t feel like doing the grunt work.	for(int i = 0; itr != tmpVertices.end(); ++itr, ++i )	{		// We want vList to contain copies of what''s in the list. 		// Vec3D better have a copy constructor!		(*vList)[i] = new Vec3D(*itr);	}		// Redundant - tmpVerticies (and it''s contents) will be destroyed when the function exits.	//tmpVertices.clear();} 
Here is a utility function for you. Should make things easier for you.

void makeVec3DArrayFromVec3DList(Vec3D*& array, int& size, const std::list<Vec3D>& cont) {    Vec3D* newArray = new Vec3D[cont.size()];    std::copy(cont.begin(), cont.end(), newArray);    array = newArray;    size = cont.size();}


This could easily be templatised.

[edited by - petewood on March 30, 2004 10:28:23 AM]
Petewood;

The array is an array of pointers. That is why I didn''t use std::copy.

Incidentally, the line where you print out the contests in my source should be vList->x not vList.x </i>
Thanks Anonymous Poster. this is exactly what I needed.
I know fo rsome doesnt make sense but this is what I wanted to achieve. Little correction

(*itr) = new V3(*itr); <<<--- this doesnt compile on my machine
I used this instead
*vList = new V3[*vertex_count];

Thank you all!
I don''t know where you got that line of code; it isn''t anywhere in the stuff I posted.
My code is horribly broken anyway.Replace (*vList)[i] = new Vec3D(*itr); with (*vList)[i] = *itr;and remove delete vList[i];
Ok. Thanks again for all your help.
Thank you all!

This topic is closed to new replies.

Advertisement