So I started with Bird, straight into alphabetical order and I had no idea it was supposed to be second-hardest, not that it mattered because I instinctively make everything harder for myself, so I started with bitwise operators! I wanted to try manipulating bits instead of memcpy to see if it would work, serialize:
buffer[0] = (char) (getX() >> 24);
buffer[1] = (char) (getX() >> 16);
buffer[2] = (char) (getX() >> 8);
buffer[3] = (char) getX();
And deserialize:
this->x = 0;
this->x |= buffer[0] << 24;
this->x |= buffer[1] << 16;
this->x |= buffer[2] << 8;
this->x |= buffer[3];
Never got it to work in C++. The same algorithm worked perfect in Java, so maybe some of my surrounding code is broken. I stored the size of the string in a single byte because the size can never change, and then I used a for loop to iterate through both the buffer and the string at the same time. This was broken for a while because I was missing the + 5 on the loops to reach the end of the string, without which it was grabbing random bytes of data.
I tried to get memcpy to save to the class member:
memcpy(this->x, buffer, sizeof(x));
Didn't work because it passes the value, not the reference.
I did Cat and Dog next, very simple, copying from the sample class for the most part.
Bird actually took me an hour or two, mainly because of dumb mistakes I kept making. I knew I couldn't save from the buffer straight into the apple and orange pointers to a null location, did it anyway. This was my absolute worst mistake that held me up for an hour:
memcpy(buffer, &pApple, sizeof(pApple));
pointer += sizeof(pApple);
Obviously, the size of a pointer is always 4 bytes. Even after I printed the sizeof I had to think about it for a few minutes. Also, the I changed the source parameter for memcpy to this->pApple; don't know if there's any significant difference between the two methods. I'm used to structs being capitalized (like classes in Java), so I kept trying to say:
apple = new apple();
Then I finally remembered how to declare it (generally, I name my objects as lowercase version of the class in Java and mostly all object-oriented languages). Oh, and then I forgot that structs need to be pointers, so I had tried to do this:
apple app = new apple();
It's been far too long since I've worked with structs, even though they are virtually the same thing as classes.
I also kept questioning whether I was offsetting the buffer correctly while this problem with structs was going on in the background. Simply adding to the buffer pointer seemed to easy, so I tried to do an array look-up and then a reference of buffer until I remembered that it was a pointer and that I shouldn't have second-guessed myself because I was doing it right to begin with.
Oh hey, remember that memcpy to a class member from up above?
memcpy(&this->x, buffer, sizeof(x));
Figured it out while I was writing this blog post, so I went over my code again and removed the temp variables I was using to memcpy to.
No comments:
Post a Comment