简体   繁体   English

将向量转换为数组问题

[英]converting vector to array problems

I have global variables: 我有全局变量:

point4 * mypoints;
color4 * mycolors;

And a block of code thats part of a function drawPlyFiles 以及那段代码是函数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). 从代码中删除的重要部分是我将全局mypoints(array)设置为等于retpolys(vector)。 Retpolys gets filled with data from the for loop. Retpolys充满了来自for循环的数据。 When I debug, all the data in retpolys is fine and proper. 当我调试时,retpolys中的所有数据都是正确的。 (retVerts is a vector of point4) (retVerts是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. 我在drawPlyFiles之后检查mypoints的值,看起来它只包含一个指向重新开始位置的指针。 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. 但是,当我调试时,它不能让我查看数组的其他部分,而只能查看第一个指针(当我在函数中时可以看到retpolys的所有单独值)然后检查此大小的值和得到一个像-12894161这样的数字,这没有意义,因为它应该是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? 有什么帮助吗?

ps 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 ) ps我希望mycolors具有与mypoints相同数量的元素,并且所有em均为color4(0.0,0.0,0.0,1.0)

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. 您应该进行实际的内存分配并复制内容。

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 . 的确,我的mypoints 不是数组

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. 如果您需要一个指针(可能传递给第三方图形API),则可以在最后一个时刻完成操作。

In C++ when you declare a variable like this: 在C ++中,当您声明这样的变量时:

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. 在您的情况下,这意味着在drawPlyFiles函数结束时,释放了repolys内容。 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. 那不是让编译器根据作用域设置内存,而是使用new运算符分配内存。 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. 引用重多边形时,您需要在函数的其余部分中更改语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM