converting vector to array problems

2019-08-22 02:09发布

问题:

I have global variables:

point4 * mypoints;
color4 * mycolors;

And a block of code thats part of a function drawPlyFiles

vector<point4> retpolys;
retpolys.resize(polynum * 3);
GLint polyx, polyy, polyz;
for (int i = 0; i < polynum; i++)
{
    inStream >> polyx;
    inStream >> polyx >> polyy >> polyz;
    //cout << "x: " << polyx << " y: " << polyy << " z: " << polyz << "\n";
    retpolys[i*3] = retVerts[polyx];
    retpolys[(i*3) + 1] = retVerts[polyy];
    retpolys[(i*3) + 2] = retVerts[polyz];
    //retpolys[i] = point4( polyx, polyy,  polyz, 1.0 );
}

mypoints = &retpolys[0];
return true;

The important part to take away from the code is that I set global mypoints(array) equal to retpolys(vector). Retpolys gets filled with data from the for loop. When I debug, all the data in retpolys is fine and proper. (retVerts is a vector of point4)

later on in my main init function this gets run:

drawPlyFile("ply_files/airplane.ply");
//colorcube();
point4 temp = mypoints[1];
int thissize = sizeof(mypoints)/sizeof(mypoints[0]);
for (int i = 0; i < thissize; i++)
{
    mycolors[i] = color4( 0.0, 0.0, 0.0, 1.0 );
}

The code compiles fine but I have a runtime exception. I debug in order to find the problem. I check the value of mypoints after drawPlyFiles, and it looks like it just contains a pointer to where retpolys began. However, when I debug, it doesn't let me view other parts of the array, just that first pointer.(I can see all the separate values of retpolys when im in the function) I then check the value of this size and i get a number like -12894161, which doesn't make sense since it should be the size of mypoints.

I think the issue lies in the conversion from retpolys to mypoints but I have no idea how to fix it, or even if thats the actual reason for error. Any help?

p.s. I want mycolors to have the same number of elements as mypoints and for all of em to be color4( 0.0, 0.0, 0.0, 1.0 )

回答1:

The following line is incorrect:

mypoints = &retpolys[0];

Here, you're taking the pointer on the content of a vector that is automatically destroyed when you exit the function. The vector destructor will deallocate this piece of memory making your global variable pointing to a deallocated memory space.

You should make an actual memory allocation and copy the content.



回答2:

You're not setting any array equal to anything; you're just storing a pointer to the first element of the vector; this ceases to be valid when the vector goes out of scope.

Indeed, mypoints is not an array.

Why not just keep the vector? When you need a pointer (perhaps to pass to a 3rd-party graphics API) then you can do it at the last possible moment.



回答3:

In C++ when you declare a variable like this:

vector<point4> retpolys;

it's storage class is automatic. That means that when scope the variable is declared in ends the memory used to hold the content is automatically freed (and if it's a class the destructor is called). In your case that means that when the drawPlyFiles function ends the retpolys content is freed. You cleverly got around that by returning a pointer to the first value in the vector. That won't work though -- the content has been freed. Even if it worked sometimes it is incorrect.

What you really want to do is allocate the content for retpolys yourself. That is not let the compiler setup the memory based on scope but rather use the new operator to allocate it. For example:

vector<point4> *retpolys = new vector<point4>;

You'll need to change the syntax in the rest of the function when referring to retpolys.