what's wrong

laiwing 2008-07-25 03:02:51
#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.
...全文
36 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
laiwing 2008-07-25
  • 打赏
  • 举报
回复
I know what's wrong now

I forget to put a result value after using the function mealloc()

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧