0

I'm learning C++ from the book Thinking in C++. There is a paragraph of codes that I don't understand. It's a member function of a struct that holds an array of chars or integers. The add function is supposed to add each element: char or int.

int Stach::add(const void* element){
int startBytes=next*size; //according to the book, next is the next space available and the size is the size of the space
unsigned char* e=(unsigned char*)element;
for(int i=0; i<size;i++)
storage[startBytes+i]=e[i];
next++;
return(next-1);// return index
}

The part I don't understand is what is space, what is the size of the space? The book didn't explain what it is. Also, I'm confused with

unsigned char* e=(unsigned char*)element;
for(int i=0; i<size;i++)
storage[startBytes+i]=e[i];

My understanding of the function is that it copies, say an int, which occupies 4 bytes, byte by byte? Am I understanding it correctly? How do I interpret

unsigned char* e=(unsigned char*)element;

Thanks a lot.

NewbieDave
  • 1,199
  • 3
  • 12
  • 23
  • `int Stach::add(const void* element)`?!? Is this book from the 80s? – Casey Jul 31 '13 at 21:51
  • idk. its called Thinking in C++ by Bruce Eckel. Why do u say that? – NewbieDave Jul 31 '13 at 21:53
  • That book was written in 2000, and apparently wasn't current at the time. You will have a much better time learning from a more recent text. – Casey Jul 31 '13 at 21:55
  • ic. do u have any recommendation? – NewbieDave Jul 31 '13 at 21:56
  • 1
    [Stackoverflow has an answer for everything.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?rq=1) – Casey Jul 31 '13 at 21:57
  • @Casey: Actually, that book is fine, and even comes recommended on the list you provided. The early chapters show a C-like implementation of a generic library. Then later on it shows modern C++ fixes all that ugliness. Eventually, the void pointers are replaced by proper template generics. Plus, it's free. – Benjamin Lindley Jul 31 '13 at 22:00
  • @BenjaminLindley It sounds like it suffers from the typical "C++ for people who know C already" slant that was typical of 90s books. People learning C++ afresh don't really need the motivation of "ooh, look how much easier it is to do (thing that is hard to do in C)". Particularly when they don't grok the description of (thing that is hard to do in C). (That said, I'm also great at reviewing movies I haven't seen and songs I've never heard.) – Casey Jul 31 '13 at 22:04
  • @Casey: Hey, I agree with you there. The book is probably better for people who already know C. But then again, it's free, and there are much worse books out there. – Benjamin Lindley Jul 31 '13 at 22:06
  • @NewbieDave Since I'm judging things I know nothing about, I will recommend you look at [Lippman's C++ Primer first](https://www.google.com/shopping/product/14232165103815775923?q=C%2B%2B+primer&bav=on.2,or.r_qf.&bvm=bv.49967636,d.aWc,pv.xjs.s.en_US.jOYpRJj4zMA.O&biw=1527&bih=840&tch=1&ech=1&psi=oIr5UYnEGaiCyQH20IGQCQ.1375308447917.3&sa=X&ei=por5Uf77E4T29gSfwICADA&ved=0CE8Q8wIwAA). I haven't read it either, but I've heard many positive things and it is certainly recent. – Casey Jul 31 '13 at 22:09

1 Answers1

0

C++ models all of program memory as an array of bytes (characters, unsigned characters). We are legally allowed to inspect the representation of any object byte-by-byte by casting a pointer to that object into an (usually unsigned) char*.

What this code is doing, is using an array of bytes as a sort-of type-independent storage space. When you call add, it copies the byte representation of the object to be added into its internal array of bytes storage. This is how one might implement a type-independent container in C, but completely inappropriate to C++, even in 2000.

Casey
  • 41,449
  • 7
  • 95
  • 125
  • Actually, that is not required. Only objects which meet the requirements for being trivially copyable are allowed to be inspected that way, at least as far as the standard is concerned. That said, I know of no implementation which actually prevents this from working. – Billy ONeal Jul 31 '13 at 22:21
  • All objects have a representation in terms of bytes of memory (1.8p1 "An *object* is a region of storage"), but only standard-layout types have representations guaranteed to be contiguous. In any case, that's WAY too much detail for someone reading their first book about C++. – Casey Jul 31 '13 at 23:14
  • @BillyONeal We're both right (§1.8p5) "An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of storage." – Casey Jul 31 '13 at 23:17
  • Fair enough :) (and in 15 characters too) – Billy ONeal Jul 31 '13 at 23:33