大虾看看这个问题怎么解决呀?????

ywchen2000 2005-05-07 02:36:25
代码如下,编译运行的时候在newhash=new hashtable*[size]; 的地方出现段异常呀,WHY????
#include<iostream>
#include<string>
using namespace std;
const int DEFAULT_SIZE=50;
class myhash
{
private:
struct bucket
{

char* strname;
bucket* next;

};
struct hashtable
{
bool is_empty;
char* strname;
bucket* next;
};

int size;
hashtable **newhash;
public:

myhash(void);
myhash(int n);
bool put(char* key);
};
myhash::myhash(void)
{
int i;
size=DEFAULT_SIZE;
newhash=new hashtable*[size]; //????
for(i=0;i<size;i++)
{
newhash[i]->is_empty=true;
newhash[i]-> next=NULL;
}
}
myhash::myhash(int n)
{
int i;
size=n;
newhash=new hashtable* [size];
for(i=0;i<size;i++)
{
newhash[i]->is_empty=true;
newhash[i]-> next=NULL;
}
}
bool myhash::put(char* key)
{}
int main(void)
{
myhash a;
}
...全文
87 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhousqy 2005-05-07
  • 打赏
  • 举报
回复
newhash = (struct hashtable **)malloc(sizeof(struct hashtable *)*size);

在循环里先执行
newhash[i] = (struct hashtable *)malloc(sizeof(struct hashtable));

先分配指针数组的内存,再在循环里分配每个指针所指东西的内存。再放东西进去。
ywchen2000 2005-05-07
  • 打赏
  • 举报
回复
SORRY,我是想建立一个指针数组
llf_hust 2005-05-07
  • 打赏
  • 举报
回复
#include<iostream>
#include<string>
using namespace std;
const int DEFAULT_SIZE=50;
class myhash
{
private:
struct bucket
{

char* strname;
bucket* next;

};
struct hashtable
{
bool is_empty;
char* strname;
bucket* next;
};

int size;
hashtable **newhash;
public:

myhash(void);
myhash(int n);
bool put(char* key);
};
myhash::myhash(void)
{
int i;
size=DEFAULT_SIZE;
newhash=new hashtable*[size]; //????
for (i=0; i<size;i++) //二维数组应该这样分配,另外记得写析构函数回收该内存空间
newhash[i] = new hashtable[size];
for(i=0;i<size;i++)
{
newhash[i]->is_empty=true;
newhash[i]-> next=NULL;
}
}
myhash::myhash(int n)
{
int i;
size=n;
newhash=new hashtable* [size];
for (i=0; i<size;i++) //二维数组应该这样分配,另外记得写析构函数回收该内存空间
newhash[i] = new hashtable[size];
for(i=0;i<size;i++)
{
newhash[i]->is_empty=true;
newhash[i]-> next=NULL;

}
}
bool myhash::put(char* key)
{
return true;
}
int main(void)
{
myhash a;
return 0;
}

33,311

社区成员

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

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