可变堆栈腐败 [英] Variable Stack Corruption
问题描述
大家好,
我是C ++的新手,甚至是Computer Graphics的新手,我已经为Wavefront OBJ文件分配了解析器/加载器,但是在文件解析器中一直出现错误:
``运行时检查失败#2-围绕变量``c''的堆栈已损坏.''
这是我的代码:
Hi all,
I''m fairly new to C++, and even newer to Computer Graphics, and I''ve been assigned a parser/loader for Wavefront OBJ files, but I kept getting an error in the file parser:
''Run-Time Check Failure #2 - Stack around the variable ''c'' was corrupted.''
And here''s my code:
void loader(char* fileName)
{
ifstream in;
in.open(fileName, ifstream::in);
if(!in)
{
cout << "Error:File Not Open";
exit (1);
}
NormalNum = 0;
TriangleNum = 0;
VertexNum = 0;
GLfloat x, y, z;
GLubyte a, b, c;
char str[512];
while(in)
{
in.getline(str, 512);
if (str[0]==''v'' && str[1]==''n'')
{
printf("1");
sscanf_s(str,"vn %f %f %f", &x, &y, &z);
printf("1.2");
NormalArray.push_back(x);
NormalArray.push_back(y);
NormalArray.push_back(z);
NormalNum++;
}
else
if (str[0]==''v'')
{
sscanf_s(str, "v %f %f %f",&x, &y, &z);
VertexArray.push_back(x);
VertexArray.push_back(y);
VertexArray.push_back(z);
VertexNum++;
}
else
if (str[0]==''f'')
{
sscanf_s(str,"f %d %d %d", &a, &b, &c);
IndexArray.push_back(a);
IndexArray.push_back(b);
IndexArray.push_back(c);
TriangleNum++;
}
}
in.close();
}
我在代码中遍历了一些标志,我可以说代码在in.close()之后停止,我假设它在释放变量的内存时遇到了一些错误.
顺便说一句,我正在使用Visual Studio 2010,并且我是计算机科学专业的学生,所以我可以讲一些行话;)
这是在这样的结构内编码的:
I''ve run some flags troughout the code, and I can say the code is stopping after the in.close(), I''m assuming it is getting some error in freeing the memory for the variable.
BTW, I''m using Visual Studio 2010, and I''m a computer sciences student, so I can take some jargon ;)
This is coded inside a struct like this:
typedef struct _Model{
int VertexNum, NormalNum, TriangleNum;
vector<GLfloat> VertexArray;
vector<GLfloat> NormalArray;
vector<GLubyte> IndexArray;
void loader(char* fileName)
Thankz
Thankz
推荐答案
如果GLubyte
是它的样子,是byte
的别名,那么您的最后一个sscanf_s
将覆盖a b
附近的内存和c
.将sscanf_s
转换为整数大小的临时变量,然后在执行push_back
时转换为GLubyte
.
干杯,
彼得
IfGLubyte
is what it looks like, an alias forbyte
, then your lastsscanf_s
is going to overwrite memory neara b
andc
. Do thesscanf_s
into integer-sized temporary variables then cast toGLubyte
as you are doing thepush_back
s.
Cheers,
Peter
这篇关于可变堆栈腐败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!