* programing error
@ 2005-08-27 23:46 Baloff
2005-08-28 0:41 ` Barry Margolin
2005-08-28 1:01 ` Pascal Bourguignon
0 siblings, 2 replies; 4+ messages in thread
From: Baloff @ 2005-08-27 23:46 UTC (permalink / raw)
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;
}
}
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: programing error
2005-08-27 23:46 programing error Baloff
@ 2005-08-28 0:41 ` Barry Margolin
2005-08-28 4:53 ` Baloff
2005-08-28 1:01 ` Pascal Bourguignon
1 sibling, 1 reply; 4+ messages in thread
From: Barry Margolin @ 2005-08-28 0:41 UTC (permalink / raw)
In article <8764treyeg.fsf@wash.edu>, Baloff <washdc@wash.edu> wrote:
> 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,
What does this have to do with GNU Emacs? Try a group like
comp.lang.c++.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: programing error
2005-08-27 23:46 programing error Baloff
2005-08-28 0:41 ` Barry Margolin
@ 2005-08-28 1:01 ` Pascal Bourguignon
1 sibling, 0 replies; 4+ messages in thread
From: Pascal Bourguignon @ 2005-08-28 1:01 UTC (permalink / raw)
Baloff <washdc@wash.edu> writes:
> 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$
You need to put doubles into the list if you want to get doubles out of it!
This is the standard GIGO principle: garbage in, garbage out.
> dblStash.add(&i);
Replace with:
double d=(double)i;
dblStash.add(&d);
--
__Pascal Bourguignon__ http://www.informatimago.com/
In deep sleep hear sound,
Cat vomit hairball somewhere.
Will find in morning.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: programing error
2005-08-28 0:41 ` Barry Margolin
@ 2005-08-28 4:53 ` Baloff
0 siblings, 0 replies; 4+ messages in thread
From: Baloff @ 2005-08-28 4:53 UTC (permalink / raw)
oops, how did this happen, sorry
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-08-28 4:53 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-08-27 23:46 programing error Baloff
2005-08-28 0:41 ` Barry Margolin
2005-08-28 4:53 ` Baloff
2005-08-28 1:01 ` Pascal Bourguignon
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).