what's wrong
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
struct stach
{
private:
int size;
int quatity;
int next;
unsigned char* storage;
void inflate(const int increase); // if there have't more space ,to add more space by mealloc()
public:
stach(const int Size=0); // constructor
~stach(); // destreuctor
const int add(const void *element); // add a element
void* fetch(const int index)const; // read a element
const int count()const; // count how many the elements do it have
};
stach::stach(const int Size) // constructor
{
size = Size;
quatity = next = 0;
storage = (unsigned char*)malloc(sizeof(char));
}
stach::~stach() // destrouctor
{
if (storage)
free(storage);
}
const int stach::add(const void* element) // add a element
{
if ( next >= quatity )
inflate(100);
memcpy(&(storage[next*size]),element,size);
next++;
return (next-1);
}
void* stach::fetch(const int index)const // read a element
{
if ( index >= next || index < 0 )
return 0;
return &(storage[index*size]);
}
const int stach::count()const // count the element
{
return next;
}
void stach::inflate(const int increase) // if there have't more space ,to add more space by realloc()
{
void *v = (unsigned char*)realloc(storage,(quatity+increase)*size);
assert(v);
quatity += increase;
}
main()
{
stach intStach(sizeof(int));
for (int i=0;i<10;i++)
intStach.add(&i);
for (int i=0;i<intStach.count();i++)
wprintf(L"intStach.fetch(%d) = %d",i,*(int*)intStach.fetch(i));
system("pause");
}
It say nother when I complier this,
but when I run it,it do trouble.