I am getting a segmentation fault when I call an array to mark the bit position of the corresponding bit position in an unsigned int set[9] with its index in the global names array names[320][30]. When I run the program, I use ./a.out < data.dat and load a list of 320 words in a list. None are more than 30 characters.
Also, typedef unsinged int Set[10];
Here is the code where I call the addName2Set function
//add name to unsigned int set
void addName2Set(Set set, char *key){
int index;
//binary search to get index for key
index = binarySearch(names, key, 0, 319);
//call add2set to add index to set
add2Set(set, index);
}
Here is add2Set
//add value passed to set passed
void add2Set(Set set, int index){
int element, position;
//find which element, set[element] of set index is in
element = findArrayElement(index);
//convert index to bit position 0-31 in set[element]
position = findElementPos(element, index);
//in set[element], set bit position 'position' to 1
set[element] = set[element] | (1 << position);
}
Here is the findArrayElement and findElementPos function
//for unsigned int set[i], return i
int findArrayElement(int index){
//index range [j,i]
int i;
int j=0;
//element in set array
int element;
//loop through [j,i], return element if range true
for(i=31; i<320; i+=32){
if(i <= i && index >= j){
return element;
}
j+=32;
element++;
}
}
//find bit position 0-31 corresponding to index
int findElementPos(int element, int index){
int j;
int position;
j = element*32;
position = index - j + 1;
//return bit position
return position;
}
And finally here is where I call the addName2Member function
//declare key pointer
char *key = (char*)malloc(30);
//set search word to pointer key
strcpy(key, "clean");
//addName2Set
addName2Set(set1,key);
Anyone see why there would be a segmentation fault when I run the program? "Clean" is the first word in the data.dat list.
Here are the results from valgrind
==1645== Invalid read of size 4
==1645== at 0x8048860: add2Set (set.c:61)
==1645== by 0x8048A5D: addName2Set (set.c:145)
==1645== by 0x80485D2: main (driver.c:29)
==1645== Address 0x9DBED860 is not stack'd, malloc'd or (recently) free'd
==1645==
==1645== Process terminating with default action of signal 11 (SIGSEGV)
==1645== GPF (Pointer out of bounds?)
==1645== at 0x8048860: add2Set (set.c:61)
==1645== by 0x8048A5D: addName2Set (set.c:145)
==1645== by 0x80485D2: main (driver.c:29)
findArrayElementandbinarySearchor if they are not your functions, which library/header are you including to use them. The seg.fault is mostl ikely due to element pointing past the allowed size for Set (10, in this case), but its value comes from the above mentioned functions. Also, if you can add what the declaration for the variableset1looks like, that might help as well.findArrayElement. This might be your problem right there. If not, please correct your code in the question. Please also provide defintion forbinarySearchandnames(used inaddName2Set()return -1to the end offindArrayElementand there is no longer a segmentation fault. Thanks