unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Baloff <washdc@wash.edu>
Subject: programing error
Date: 28 Aug 2005 09:46:31 +1000	[thread overview]
Message-ID: <8764treyeg.fsf@wash.edu> (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;
   }
}

             reply	other threads:[~2005-08-27 23:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-08-27 23:46 Baloff [this message]
2005-08-28  0:41 ` programing error Barry Margolin
2005-08-28  4:53   ` Baloff
2005-08-28  1:01 ` Pascal Bourguignon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8764treyeg.fsf@wash.edu \
    --to=washdc@wash.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).