Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Pointers question

This topic is 5584 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

Lately I''ve been working a little bit with binary files and I noticed that if you save a pointer to a file and load it back in then the program crashes...Now I assume this is because what you are writting to the file is not really the data contained in the pointer but the address of the pointer right? So if that''s the case what should I do to get around this problem? Is it possible to write it as binary data?

Share this post

Link to post
Share on other sites
You''re right as to why it doesn''t work.

The solution depends on what the data is. If this was c and you were saving a string for example:

To save it:

int length = strlen(mystring);
fwrite(&length, sizeof(int), 1, file);
fwrite(mystring, 1, length, file);

To load it:

int length;
fread(&length, sizeof(int), 1, file);
mystring = malloc(length+1);
fread(mystring, 1, length, file);
mystring[length] = 0; // terminate the string.

Share this post

Link to post
Share on other sites
Oh Ok, so if I was say saving something like a class with different pointers in it etc...Then what would be the best solution? IMO it would be copying the variables to a structure or something like that and then saving it or is there a better way?

[edited by - JohnyB on August 8, 2003 2:54:51 AM]

Share this post

Link to post
Share on other sites
For a class that contains pointers, or contains a class that contains pointers, it''s best to save each member individually.

For the pointers, if you are using the pointer as an array, you can save the number of objects, and then each object.

If you are using it as a normal pointer, you can save that object normally.

If it is pointing to a polymorphic object (one that has virtual functions) you would want to get it''s type id and save that, then give it its own save function to save its self. Later when loading, you''d load the id to figure out what type of object it was, create an object of that type, and tell it to load its self.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!