Please point out why below code is not working properly. The program is meant to store 25 doubles and print them out again to the console.
Code:
// function add() of struct Stash, which adds values to storage
void Stash::add(const void* element)
{
if(next>=quantity)
inflate(increment);
int startBytes = next*size;
unsigned char* e = (unsigned char*) element;
for(int i=0; i<size; i++){
storage[startBytes+i] = e[i];
}
next++;
}
// function print(), which prints the values in storage to the console
void Stash::print()
{
for(int i=0; i<=25; i++)
cout<<(double)storage[i*size]<<endl;
}
// function inflate(), which adds more memory to storage
void Stash::inflate(int increase)
{
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[i] = storage[i];
delete []storage;
storage = b;
quantity = newQuantity;
}
// we call those functions from main() like this
void main()
{
Stash s;
s.initialize(sizeof(double));
for(double d=0; d<=25; d++)
s.add(&d);
s.print();
}
Thanks a lot for reading.