[英]C++ - Access violation reading location while reading .tga file
我正在写.tga阅读器。 我可以完美地读取标题,然后再读取文件的数据部分,但是如果到达该位置,则会出现错误:
Unhandled exception at 0x76ffa2ce in TGALoader.exe 0xC0000005: Access violation reading location 0xffff0008
该文件的长度为65580
,在18 bytes
长的标头之后,我读取了65536
。
我当前的代码是(我删去了不重要的部分):
// Texture variables
GLint bitsPP;
GLsizei width;
GLsizei height;
GLubyte *imgData;
/////////////////////////////////////////////////
file.seekg( 0, std::ios::end );
std::cout << file.tellg() << "\n"; // 65580
file.seekg( 0, std::ios::beg );
file.read( ( char* )&tGAHeader, sizeof( tGAHeader ) );
texture->width = tGAHeader[13] * 256 + tGAHeader[12];
texture->height = tGAHeader[15] * 256 + tGAHeader[14];
texture->bitsPP = tGAHeader[16];
short bytesPP = texture->bitsPP / 8; // 4
unsigned int imgSize = texture->width * texture->height * bytesPP; // 65536
texture->imgData = new GLubyte[imgSize];
file.read( ( char* )&texture->imgData, imgSize ); // Access violation reading location
我无法想象会有什么问题,所以希望有人能帮助我。
提前致谢!
更改
file.read( ( char* )&texture->imgData, imgSize )
通过
file.read( ( char* )texture->imgData, imgSize )
texture->imgData
只是一个指针,不要在第二个间接级别使用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.