save pointers on disk

Started by
7 comments, last by zorlack 20 years, 7 months ago
is there an easy way or is there any to save a pionter on to the hard drive.
Advertisement
Why would you want to? A pointer points to a location in memory, therefore once your program is unloaded from memory and restarted, the location the pointer was pointing to would have changed.

Now if you want to save the data that the pointer points to, yes you can using your file I/O method of choice be it streams, or older fwrite. If you need an example just ask.

~ Chris
Pointer as in *p? Tis better to save the data, *p could be a different number next time program is run. If you''re sharing the data between two or more apps and the memory is out there then I''m not sure.. the nature of multitasking, I wonder if the memory from one program will go to swapfile or something and the active program might not see what memory the pointer is pointing to..

I fseek, therefore I fam.
I'll give you a beating like Rodney King who deserved it!=====================================Any and all ideas, theories, and text c2004,c2009 BrainDead Software. All Rights Reserved.
yes i would love an example, it will help alot if i could use it to create an avl tree wilth pointers ssaved on the disk than using arrays
Eh. Well theres no real way to save the pointers. What you have to do is either save the data thats in the tree to disk, and recreate the tree when your application runs, or you have to make up some sort of a file format, that stores the relationship between nodes in the tree.

~ Chris

[edited by - evilvodkaman on September 2, 2003 12:29:48 AM]
I get the impression that the OP is not really ready to be making trees and other things with pointers, without understanding how they are stored. Personally, I would search the forum for some more information on pointers before moving on. There are an awful lot of threads about pointers.
Peon
I suggest you convert the pointer to an integer value (unsigned long) and use that as an id for the data each node contains. You will also need to do the same for node pointers.

When you save, write each object id followed by the object data. Then, loop through the tree again. THis time, for each node you write the node id followed by the ids of any nodes this one points to and the id of the object it contains. When you load, use the ids to match nodes with object data. Now your pointers have new addresses but the tree retains the same structure. You can do it all in one loop if you flag each object when it is saved.

saving a pointer seems strange. you would want to do this why? a pointer is just a reference to memory, which if loaded, would have to reload the actual data. So you should save the data, and make a pointer to it
If you got Game Programming Gems 1 lying around, read the parts about HANDLEs (You could try searching online too)

If you dont have it, you might consider buying it, i found it to be quite good

It covers some not so easy topics tho, so if you dont understand how pointers work you should first work on the basics.
(www.gametutorials.com explains most C++ features quite well iirc)

This topic is closed to new replies.

Advertisement