Sign in to follow this  
Ubermeowmix

Vector memory and Binary files!

Recommended Posts

If I have a vector of objects, with a vector of objects inside that. Rough example follows.

class MyClass
{
// bla
void AddStuff(StuffClass);
private:
vector<StuffClass> myStuff;
};

class StuffClass
{
// bla
private:
string m_name;
int m_totalMoney;
string m_description;
};

int main()
{
vector<MyClass> totalCharacters;

totalCharacters.push_back( MyClass char1 );
char1.AddStuff( StuffClass("bag o money, 45, "etc") );
};

does the vector within and all it's contents get deleted if char1 dies and is removed from the vector?

Can I save objects in binary files, is that their main use? So if i have this little array of mean mofo baddies, can they all be put in a .bin file and loaded after the user restarts the program?

Share this post


Link to post
Share on other sites
[quote name='Ubermeowmix' timestamp='1343682633' post='4964608']
Can I save objects in binary files, is that their main use? So if i have this little array of mean mofo baddies, can they all be put in a .bin file and loaded after the user restarts the program?
[/quote]

Just RTFM on this one, will play with that tomorrow! But can it hold whole levels worth of baddies and objects? or is there a different method that's more streamlined? Edited by Ubermeowmix

Share this post


Link to post
Share on other sites
[quote name='Ubermeowmix' timestamp='1343682633' post='4964608']
does the vector within and all it's contents get deleted if char1 dies and is removed from the vector?
[/quote]

Yes. In your other thread, I mentioned that "when an object is created on the stack, its lifetime is bound to the scope it resides in," and, "similarly, the lifetime of the members inside an object are bound to the lifetime of the object." When an object expires, so do the members of that object. Whenever an object expires, its destructor is called.

[quote]
Can I save objects in binary files, is that their main use? So if i have this little array of mean mofo baddies, can they all be put in a .bin file and loaded after the user restarts the program?
[/quote]

Binary files are just a sequence of bits; they can mean whatever you cause them to mean. (Text files are also just a sequence of bits, however they happen to contain more human-readable characters than not.)

What you're looking for is serialization, and implementations range from simple to very complex. Keep in mind that you cannot write a pointer to disk and expect the memory it pointed to remain allocated forever.

Share this post


Link to post
Share on other sites
You should try saving integers to a binary file using std::ofstream. Then, try adding strings, and see if you can read them back from the file you just saved using std::ifstream.

Once you are successful at that, you should get an idea how to write/read the primitive data types to/from files. Classes are composed of primitives, so saving a class is just a matter of saving and reading primitives in a particular format.

Share this post


Link to post
Share on other sites
As the other two have said, you can pretty much serialize anything. Image formats are sequences of bytes with mostly complex algorithms to compress the image(Well, .ppm is really easy to implement...). It's the same for H264 encoding and sound compression. In the end everything is just a stream of bytes. What matters is having the code that is able to write it and another one that can read it.

Think of it, if you really wanted to, you could iterate over the whole vector and write down a segment of bytes which would have the all the enemies between {} and all the good guys in []. That's not very useful, but in the case of image and video compression to quality ratio is the only reason boundaries are pushed. Edited by DZee

Share this post


Link to post
Share on other sites
[quote name='fastcall22' timestamp='1343684196' post='4964617']
I mentioned that "when an object is created on the stack, its lifetime is bound to the scope it resides in," and, "similarly, the lifetime of the members inside an object are bound to the lifetime of the object." When an object expires, so do the members of that object.
[/quote]

I do a lot of assuming at the moment which is causing me no end of problems, i'm trying to get literal answers to my "what if's!".

Thanks for clarifying that for me, really appreciate the help.

Share this post


Link to post
Share on other sites
[quote name='alnite' timestamp='1343689487' post='4964644']
You should try saving integers to a binary file using std::ofstream. Then, try adding strings, and see if you can read them back from the file you just saved using std::ifstream.
[/quote]

Been saving text and numbers to a .txt file already. Creating and amending etc. Just jumping ahead a bit as it's all exciting atm and I want to know what else i've got to chew on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this