Intermediate 4 Homework Help

The Partridge Family were neither partridges nor a family. Discuss.
SamCam
Posts: 20
Joined: January 8th, 2020, 3:24 pm

Intermediate 4 Homework Help

Post by SamCam » April 9th, 2020, 12:48 pm

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.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate 4 Homework Help

Post by albinopapa » April 9th, 2020, 6:13 pm

Seems to work just fine for me:
Image
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

SamCam
Posts: 20
Joined: January 8th, 2020, 3:24 pm

Re: Intermediate 4 Homework Help

Post by SamCam » April 9th, 2020, 7:37 pm

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!!

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate 4 Homework Help

Post by albinopapa » April 9th, 2020, 8:58 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate 4 Homework Help

Post by albinopapa » April 9th, 2020, 8:59 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate 4 Homework Help

Post by albinopapa » April 9th, 2020, 9:02 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate 4 Homework Help

Post by albinopapa » April 10th, 2020, 7:53 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

SamCam
Posts: 20
Joined: January 8th, 2020, 3:24 pm

Re: Intermediate 4 Homework Help

Post by SamCam » April 11th, 2020, 7:06 pm

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.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Intermediate 4 Homework Help

Post by albinopapa » April 12th, 2020, 5:54 am

No problem, just wish I could collect...I could use a Corona right about now LOL.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Intermediate 4 Homework Help

Post by chili » April 15th, 2020, 10:09 am

ba dum tiss
Chili

Post Reply