大数组大小的分段错误。为什么? [英] Segmentation fault with big array size. Why ?
问题描述
struct person
{
char name[100];
int freq ;
} ;
我正在创建一个结构人数组并将此数组用作哈希表。
I am creating an array of structure person and using this array as a hash table.
int capacity;
struct person table[capacity] ;
我插入通过读取csv文件的字符串名称,该文件的每一行都有一个名称。
当我将表容量大约为50000或更多时,分段错误生成。
小容量为5000时,一切正常。
如果在我的文件有大约60000个不同的名字,我现在如何存储它们?
我不明白为什么会这样?可以因为我的哈希函数吗?
我使用线性探测来查找哈希值。
我很困惑。请帮忙。
我尝试了什么:
我做了Netbeans IDE中的代码。
I insert the string name by reading through a csv file which has a name on each of its line.
When I take table capacity to be around 50000 or more, a segmentation fault generates.
When I take a small capacity as 5000, everything works fine.
What if in my file there are around 60000 distinct names, How will I store them now ?
I don't understand why it is happening? Can it be because of my hash function?
I am using linear probing for finding hash value.
I am confused. Please help.
What I have tried:
I made the code in Netbeans IDE.
推荐答案
大数组大小的分段错误。为什么?
Segmentation fault with big array size. Why ?
因为内存不是无限的,所以与C编译器一起使用的内存模型有一个限制,如果你不告诉操作系统,操作系统也会提供有限的内存份额你需要更多。
欲了解更多细节,你需要告诉我们:
- 你的C编译器是什么
- 哪种内存模型是在你的项目中使用
- 亲切的应用程序生成
- 哪个操作系统
建议:考虑使用一些数据量很大时数据库。
Because memory is not infinite, the memory model used with your C compiler have a limit, and the OS also give a limited share of memory if you don't tell the OS that you need more.
For more details, you need to tell us :
- What is your C compiler
- Which memory model is used in your project
- The kind app generated
- Which OS
Advice: think about using some database when you have large amount of data.
您是否尝试动态调整表
变量的大小?例如类似
Are you trying to dynamically adjust the size of yourtable
variable? e.g. something like
int get_int(void); // read an int from the user - not a standard function!
int capacity;
struct person table[capacity];
// more code here ...
capacity = get_int();
for(int i = 0; i < 500; i++)
{
// do something with table
}
如果就是这样,那么你所拥有的就是不行。编译代码时,表变量的大小是固定的
你需要调查内存的动态分配。参见 malloc()
和朋友。
这篇关于大数组大小的分段错误。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!