Ok, I have been reading and working my way through the Thinking in CPP book. It's acutally been pretty smooth. Or it was untill I got to the section where he makes a 'Stash' structure and explains it in
sumerian. He doesn't actualy explain it in sumerian, just that he explains it really poorly. The Stash itself is used almost exactly like a vector. Im gonna post the code here and then describe what I think certain areas of significance are doing.
//: C04:CppLib.h
// C-like library converted to C++
struct Stash {
int size; // Size of each space
int quantity; // Number of storage spaces
int next; // Next empty space
// Dynamically allocated array of bytes:
unsigned char* storage;
// Functions!
void initialize(int size);
void cleanup();
int add(const void* element);
void* fetch(int index);
int count();
void inflate(int increase);
}; ///:~
int size
Seems to mean the ammount of bytes each section holds.
int quantity
Quantity looks to be the number of entities stored in the Stash.
int next
I think 'next' holds the index number of the next free storage unit.
unsigned char* storage
This one is a real doozy. In english I would translate that as non-negative single character byte pointer, named storage. As to it's use? On a guess, I would assume it is used like a measuring stick. Each 'area' of storage size is defined by a number of these. If that's not right, and I don't think it is, I have NO CLUE.
int add(const void* element)
It returns an integer, that much I know. I am almost positive const is used to say "Hey! This variable never changes!". Now it gets hairy. A void pointer.. It's uses from the declaration defy me. I think it is basically saying that it does not know what type of pointer it will receive. Be it an integer or a double or whatever.
void* fetch(int index);
A void pointer function called fetch? Dubya tee eff... Obviously it's use is to get a value stored in it at the supplied index. I don't understand why he is declaring it as a void pointer though.
On to the definintion section.. Fun!
//: C04:CppLib.cpp {O}
// C library converted to C++
// Declare structure and functions:
#include "CppLib.h"
#include <iostream>
#include <cassert>
using namespace std;
// Quantity of elements to add
// when increasing storage:
const int increment = 100;
void Stash::initialize(int sz) {
size = sz;
quantity = 0;
storage = 0;
next = 0;
}
int Stash::add(const void* element) {
if(next >= quantity) // Enough space left?
inflate(increment);
// Copy element into storage,
// starting at next empty space:
int startBytes = next * size;
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < size; i++)
storage[startBytes + i] = e;
next++;
return(next - 1); // Index number
}
void* Stash::fetch(int index) {
// Check index boundaries:
assert(0 <= index);
if(index >= next)
return 0; // To indicate the end
// Produce pointer to desired element:
return &(storage[index * size]);
}
int Stash::count() {
return next; // Number of elements in CStash
}
void Stash::inflate(int increase) {
assert(increase > 0);
int newQuantity = quantity + increase;
int newBytes = newQuantity * size;
int oldBytes = quantity * size;
unsigned char* b = new unsigned char[newBytes];
for(int i = 0; i < oldBytes; i++)
b = storage; // Copy old to new
delete []storage; // Old storage
storage = b; // Point to new memory
quantity = newQuantity;
}
void Stash::cleanup() {
if(storage != 0) {
cout << "freeing storage" << endl;
delete []storage;
}
} ///:~
Im not going over the whole thing, just the parts I obviously have no clue about.
void Stash::add(const void* element)
unsigned char* e = (unsigned char*)element;
This gives me the fits. For one, there is some strange syntax used that he never explained when it assigned the char pointer e its value. Maybey he is type casting. To make things clear I am talking about the part after the = sign. When he puts '()' around unsigned char* is he type casting 'element'?
storage[startBytes + i] = e;
Hold the boat.. It looks like he's using storage and e both like they are arrays.. Yet, they were not declared to be arrays.. *head spins*
return(next - 1);
Where is he returing this too? When you use the stash to add something you don't expect a value back. For example:
myStash.add(myInt)
That doesn't seem to imply its getting any kind of reply.
void* Stash::fetch(int index)
return &(storage[index * size])
There are two huge issues I have with this whole thing.. Firstly his seeming use of a void pointer function or method. Secondly, his odd syntax when returning things. The last one I think I get. He is returning the memory location of the value stored in the index specified of storage... Did I get it right? If that is the case, why not just make the function a non-pointer and return just the value of the index, not its memory location.
void Stash::inflate(int increase)
delete []storage;
Why the brackets before the name? Don't they normally got at the rear?
I think that's it for now. Sorry for such a long post dripping with n00b questions, but this book is starting to give me hives.
[Edited by - -JetSirus- on July 20, 2006 4:20:58 AM]