将向量转换为数组问题 [英] converting vector to array problems

查看:264
本文介绍了将向量转换为数组问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有全局变量:

  point4 * mypoints; 
color4 * mycolors;

和一个代码块,它是函数的一部分drawPlyFiles

 矢量< point4> retpolys; 
retpolys.resize(polynum * 3);
GLint polyx,polyy,polyz;
for(int i = 0; i {
inStream> polyx;
inStream>> polyx> polyy>> polyz;
// cout<< x:<< polyx< y:< polyy< z:<< polyz < \\\
;
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;

取出代码的重要部分是我设置全局mypoints(array)等于retpolys (向量)。 Retpolys被来自for循环的数据填充。当我调试,retpolys中的所有数据是好的和适当的。 (retVerts是point4的向量)



稍后在我的主要init函数中运行:

  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)
}

代码编译正常,但我有一个运行时异常。我调试为了找到问题。我在drawPlyFiles之后检查mypoints的值,它看起来像是只包含一个指向retpolys开始的地方的指针。然而,当我调试,它不让我查看数组的其他部分,只是第一个指针(我可以看到retpolys的所有单独的值,当im在函数中)然后检查这个大小的值和i得到一个像-12894161这样的数字没有意义,因为它应该是mypoints的大小。



我认为问题在于从retpolys到mypoints的转换,我不知道如何解决它,或者即使这是错误的实际原因。有任何帮助吗?



p.s。我想让mycolors有与mypoints相同的元素数量,并且所有的em都是color4(0.0,0.0,0.0,1.0)

解决方案

以下行不正确:

  mypoints =& retpolys [0] 

在这里,你将指针指向一个向量的内容,功能。向量析构函数将释放这块内存,使你的全局变量指向一个释放的内存空间。



你应该做一个实际的内存分配并复制内容。 >

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 )

解决方案

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.

这篇关于将向量转换为数组问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆