I'm working my way through the learn c the hard way book and have run into a few issues on Exercise 19. The author said that ex19 was intended for the learners to get to know the macro in c. I have no problem in understanding the concept of that, but I just don't understand everything else. I can't understand how the object prototype is created.
Especilly,what does the following sentense mean?
Since C puts the Room.proto field first, that means the el pointer is really only pointing at enough of the block of memory to see a full Object struct. It has no idea that it's even called proto.
the relevant code is this:
// this seems weird, but we can make a struct of one size,
// then point a different pointer at it to "cast" it
Object *el = calloc(1, size);
*el = proto;
- can anyone tell me how on earth malloc/calloc exactly works? As far as i know, it just allocate the required number of memory and return the first address. If so, how can the computer know the data struct of the allocated memory? like in the code, after
Room *arena = NEW(Room, "The arena, with the minotaur");
,you can do this directlyarena->bad_guy = NEW(Monster, "The evil minotaur");
how does the computer know there is abad_guy
?? - what on earth is the content of *el after the above two statements(
Object *el = calloc(1, size);
and*el = proto;
)?
Any help will be appreciated!!
the link to the exercise: http://c.learncodethehardway.org/book/ex19.html