简体   繁体   中英

C++ - Access violation reading location while reading .tga file

I'm writing a .tga reader. I can read the header perfectly, and after that I would like to read the data part of the file, but if I get there it gives me the error:

Unhandled exception at 0x76ffa2ce in TGALoader.exe 0xC0000005: Access violation reading location 0xffff0008

The length of the file is 65580 , and I read 65536 after the 18 bytes length header.

My current code is (I cut out the non important part):

// 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

I can't imagine what can be a problem, so I hope someone can help me.

Thanks in advance!

Change

file.read( ( char* )&texture->imgData, imgSize )

by

file.read( ( char* )texture->imgData, imgSize )

texture->imgData is just a pointer, don't use it a second indirection level

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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