[英]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.