Hello coding in C++, I have a linked list to store int, but when I store double it is not working and I tried to find out why but could not,
thanks for looking at this problem output************************************************************ @debian:~/Exercies/ThinkingInC++/Vol1/4thCh/7$ ./proj1 dblStash.fetch(0) = 0 dblStash.fetch(1) = 1 dblStash.fetch(2) = 2 dblStash.fetch(3) = 3 dblStash.fetch(4) = 4 freeing storage @debian:~/Exercies/ThinkingInC++/Vol1/4thCh/7$ ./proj1 dblStash.fetch(0) = 1.6976e-313 dblStash.fetch(1) = 1.6976e-313 dblStash.fetch(2) = 1.6976e-313 dblStash.fetch(3) = 1.6976e-313 dblStash.fetch(4) = 1.6976e-313 freeing storage @debian:~/Exercies/ThinkingInC++/Vol1/4thCh/7$ for a linked list to store int, change 'double' to 'int' in the lines marked (1) and (2) below //main.cpp************************************************** #include "stash.h" #include <iostream> #include <fstream> using namespace std; int main() { Stash dblStash; dblStash.initialize(sizeof(double)); // (1) for(int i = 0; i < 5; i++) dblStash.add(&i); for(int j = 0; j < dblStash.count(); j++) cout << "dblStash.fetch(" << j << ") = " << *(double*)dblStash.fetch(j) // (2) <<endl; dblStash.cleanup(); } //stash.h************************************************** struct Stash { int size; int quantity; //number of element in the stash int next; //index of element in the stash unsigned char* storage; void initialize(int size); int add (const void* element); void* fetch (int index); int count (); void inflate (int increase); void cleanup (); }; //stash.cpp************************************************** #include "stash.h" #include <iostream> #include <cassert> using namespace std; const int increment = 5; void Stash::initialize(int sz){ size = sz; quantity = 0; next = 0; storage = 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; //now lets copy byte-by-byte unsigned char* e = (unsigned char*)element; for(int i = 0; i < size; i++) storage[startBytes + i] = e[i]; next++; return(next -1); } void* Stash::fetch(int index){ assert(0 <= index); if(index >= next) return 0; //to indicate the end return &(storage[index * size]); } int Stash::count() { return next; } 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[i] = storage[i]; //copy old tonew delete []storage; storage = b; quantity = newQuantity; } void Stash::cleanup() { if(storage != 0) { cout << "freeing storage" << endl; delete []storage; } } _______________________________________________ Help-gnu-emacs mailing list Help-gnu-emacs@gnu.org http://lists.gnu.org/mailman/listinfo/help-gnu-emacs