Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
struct person
{
char name[100];
int freq ;
} ;


I am creating an array of structure person and using this array as a hash table.

C++
int capacity;
struct person table[capacity] ;


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.
Posted
Updated 12-Oct-18 3:54am
v4
Comments
CPallini 12-Oct-18 3:16am    
You should use the debugger to find out the offending code. And possibly report it here.

Quote:
Segmentation fault with big array size. Why ?

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.
 
Share this answer
 
Are you trying to dynamically adjust the size of your table variable? e.g. something like

C++
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
}


If that's the case, then what you have is not going to work. When the code is compiled, the size of the table variable is fixed when it is

You need to look into dynamic allocation of memory. See malloc() and friends.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900