I have function - Stash::add - to recieve any kind of value,
and store that value as a byte array:
Code:
int Stash::add(const void* element) {
........
........
// Copy element into storage,
// starting at next empty space:
int startBytes = next * size; /* size - the size of the
data type passed to add() */
unsigned char* e = (unsigned char*)element; /* storage is a
unsigned char array */
for(int i = 0; i < size; i++)
storage[startBytes + i] = e[i];
next++;
return(next - 1);
}
And - Stash::fetch - to get a value at a specific position in
that byte array:
Code:
void* Stash::fetch(int index) {
........
........
// Produce pointer to desired element:
return &(storage[index * size]);
}
Now, I have to change this codes' underlying data structure
to vector<char>.(This means to store each byte of 'element'
in subsequent positions of the vector,simulating a char array, right?)
So, I change it like this:
Code:
int Stash::add(const void* element) {
unsigned char* e = (unsigned char*)element;
for(int i = 0; i < size; i++)
storage.push_back(e[i]); // now storage is changed to a vector
next++;
return(next - 1);
}
void* Stash::fetch(int index) {
int ind = index;
int trueInd = ind * size;
........
........
unsigned char* intstore = new unsigned char[size];
for(int x=0; x<=size; x++)
intstore[x] = storage.at(trueInd+x);
return &intstore;
}
In main(), the function calls are like this:
Code:
Stash intStash;
........
for(int i = 0; i < 100; i++)
intStash.add(&i);
for(int j = 0; j < intStash.count(); j++)
cout << *(int*)intStash.fetch(j) << endl;
Is the changes I have made are correct?
(If anybody wants the whole code to figure out what's actually happening,please be kind to give a little notification)
- Wish a beautiful march to all -