Page 1 of 2

Intermediate 4 Homework Help

Posted: April 9th, 2020, 12:48 pm
by SamCam
Hey Forum,

Feels like I'm so nearly there with this, time to reach out before I go ahead and just cheat.

Issue: Can seem to write to a file (can't verify but the file is getting padded with bytes.) but cannot read back successfully. The integers for the value field seem to load back into the array fine but I cannot get the strings to load back and print.

Thingy: https://github.com/SamuelLouisCampbell/intlo.git

Thanks god there's a virus on, getting so much learnin' dun....

Many thanks.

Re: Intermediate 4 Homework Help

Posted: April 9th, 2020, 6:13 pm
by albinopapa
Seems to work just fine for me:
Image

Re: Intermediate 4 Homework Help

Posted: April 9th, 2020, 7:37 pm
by SamCam
Sorry, that must be a link to the master branch (which includes the correct solution from Chili)

https://github.com/SamuelLouisCampbell/ ... ree/SAMDEV

This hopefully links to mine. Thanks!!

Re: Intermediate 4 Homework Help

Posted: April 9th, 2020, 8:58 pm
by albinopapa
Okay, so the issue is you are casting the entire Person struct to a char* and writing out the bytes that make up the struct. Not necessarily a problem if the struct was entirely stack allocated bytes. The char* buffer is just a pointer, so the pointer address is what is stored in the file and not the bytes stored at the address from the previous run of the program.

You must serialize and deserialize the value and string separately or make buffer a c-style array.

Re: Intermediate 4 Homework Help

Posted: April 9th, 2020, 8:59 pm
by albinopapa
The way chili handles it is each entry in the database handles it's own serialization/deserialization. In each of those functions, the value and char* string is written to file.

Re: Intermediate 4 Homework Help

Posted: April 9th, 2020, 9:02 pm
by albinopapa
Also, just to point out, Person::setString only copies the address of the source string...not the string itself. You must copy the entire string from source to buffer. The only reason it works somewhat is because NameBufferArray is still in scope. If there was another function where NameBufferArray went out of scope and you tried returning a Person object without making a deep copy of NameBufferArray first, then Person::buffer would point to data that no longer existed.

Re: Intermediate 4 Homework Help

Posted: April 10th, 2020, 7:53 am
by albinopapa
Git pull-request showing you what you could do and explaining why I chose the code I did. Also, there should be a fair amount of comments in the code explaining things.

Re: Intermediate 4 Homework Help

Posted: April 11th, 2020, 7:06 pm
by SamCam
Thanks so much, just sat down to get in a couple of hours coding, I'll be working through your pull request first!

Thanks again for your help, I think I owe you more than a couple of beers now.

Re: Intermediate 4 Homework Help

Posted: April 12th, 2020, 5:54 am
by albinopapa
No problem, just wish I could collect...I could use a Corona right about now LOL.

Re: Intermediate 4 Homework Help

Posted: April 15th, 2020, 10:09 am
by chili
ba dum tiss