Jump to content
  • Advertisement
Sign in to follow this  
flodihn

fread crashes on pointer

This topic is 2253 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I got this strange problem that perhaps someone with better knowledge of C++ than me could explain.
For some reasion the progrem crashes when trying to read data into a pointer to regionVoxel.
However, reading into a voxel allocated on the stack and then copy it over to the regionVoxel works...




// * This is how I call the read_region function
voxel regionVoxel;
...
result = read_region(region_filename.str(), block_dir.str(), &regionVoxel);

int read_region(string regionFile, string blockDir, voxel *regionVoxel)
{
voxel tmp;
if(file_exists(blockDir.c_str()))
return ABYDOS_TERRAIN_MIXED_REGION;

if(!file_exists(regionFile.c_str()))
return ABYDOS_TERRAIN_READ_ERROR;

FILE *file = openFile(regionFile.c_str());

if(file == NULL)
return ABYDOS_TERRAIN_READ_ERROR;

memset(regionVoxel, 0, sizeof(voxel));
memset(&tmp, 0, sizeof(voxel));

// * In my world, &tmp and *regionVoxel is kind of the same, why a pointer
// * to voxel can not be read, but a pointer when taken by the adress opererator can eludes me.
//fread((void *) regionVoxel->density, 1, 1, file);
//fread((void *) regionVoxel->material, 2, 1, file);
read(&tmp.density, 1, 1, file);
fread(&tmp.material, 2, 1, file);
*regionVoxel = tmp;
closeFile(file);

regionVoxel->material = ntohs(regionVoxel->material);
return ABYDOS_TERRAIN_SHARED_REGION;
}

Share this post


Link to post
Share on other sites
Advertisement
You mean
fread((void *) &regionVoxel->density, 1, 1, file);
fread((void *) &regionVoxel->material, 2, 1, file);


The things you are converting to (void *) should be the addresses of the fields you want to fill out, not their values.

Share this post


Link to post
Share on other sites
Thanks, I actually just noticed that I wanted the address of density and material inside the struct :)
So I changed to this and it worked.


fread((void*) &(regionVoxel->density), 1, 1, file);
fread((void*) &(regionVoxel)->density, 1, 1, file);

Share this post


Link to post
Share on other sites

Thanks, I actually just noticed that I wanted the address of density and material inside the struct smile.png
So I changed to this and it worked.


fread((void*) &(regionVoxel->density), 1, 1, file);
fread((void*) &(regionVoxel)->density, 1, 1, file);



To avoid similar issues in future consider using static_cast, this provides the compiler with context to confirm the cast is valid, so you don't have things like this creep in by accident


unsigned int val = 0xcdcdcdcd;
void* ptr1 = static_cast<void*>(val); // error C2440: 'static_cast' : cannot convert from 'unsigned int' to 'void *'
void* ptr2 = static_cast<void*>(&val); // ok
void* ptr3 = reinterpret_cast<void*>(val); // if you insist..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!