Segmentation fault on separate chaining hashtable - c

So i implemented a hashtable with separate chaining for a struct called Objective, so that i could perform some operations on said Objectives. Currently i have this:
Hashtable.h:
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
/*Using separate chaining to store the obejctives*/
typedef struct Objective{
char name [8000];
unsigned long id, duration, deps [9000];
int hasDeps;
}*pObjective;
typedef struct nodehash{ /*Node of list*/
pObjective obj;
struct nodehash*next;
}*link;
void Init(int M);
int search(unsigned long id);
void insert(pObjective o);
void delete(unsigned long id);
link insertBegin(link h, pObjective obj);
int searchList(link h, unsigned long id);
link removeList(link h, unsigned long id);
pObjective searchObj(unsigned long id);
pObjective searchObjAux(link h, unsigned long id);
#endif
Objectives.c:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "OBJECTIVES.h"
/*Checks if all inserted dependencies already exist*/
int existDeps(unsigned long dep[9000]){
int i, count = 0;
for(i = 0; i < 9000; i++){
if(search(dep[i]) != 0)
count++;
}
return count;
}
/ *Adds objective with dependencies*/
void addObj(unsigned long id, char name [8000], unsigned long duration,
unsigned long dep[9000]){
int i;
pObjective obj = malloc(sizeof(pObjective));
obj->id = id;
obj->duration = duration;
obj->hasDeps = 1;
strcpy(name, obj->name);
for(i = 0; i < 9000; i++){
obj->deps[i] = dep[i];
}
if(search(id) != 0)
printf("id already exists\n");
else if(existDeps(dep) != 0)
printf("no such task\n");
else
insert(obj);
free(obj);
}
/*Adds objective with no dependencies*/
void addNoDeps(unsigned long id, char name [8000], unsigned long
duration){
pObjective obj = malloc(sizeof(pObjective));
obj->id = id;
obj->duration = duration;
obj->hasDeps = 1;
strcpy(name, obj->name);
if(search(id) != 0)
printf("id already exists\n");
else
insert(obj);
free(obj);
}
/*Removes objective with no dependencies*/
void removeObj(unsigned long id){
int res = search(id);
pObjective obj = searchObj(id);
if(res == 0)
printf("no such task\n");
else if(obj->hasDeps == 1)
printf("task with dependencies\n");
else
delete(id);
}
Objectives.h:
#ifndef OBJECTIVES_H
#define OBJECTIVES_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "HASHTABLE.h"
/*Functions to work with objectives*/
int existDeps(unsigned long dep[9000]);
void addObj(unsigned long id, char name [8000], unsigned long duration,
unsigned long dep[9000]);
void addNoDeps(unsigned long id, char name [8000], unsigned long
duration);
void removeObj(unsigned long id);
#endif
Hashtable.c:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "HASHTABLE.h"
#define hash(A,B) (A%B) /*Hash function*/
static link *heads;
static int M;
/*Initiates hashtable with size m*/
void Init(int m){
int i;
M = m;
heads = (link*)malloc(M*sizeof(link));
for(i = 0; i < M; i++)
heads[i] = NULL;
}
/*Searches objective with said id*/
int search(unsigned long id){
int i = hash(id, M);
return searchList(heads[i], id);
}
/*Inserts objective into hashtable*/
void insert(pObjective o){
int i = hash(o->id, M);
heads[i] = insertBegin(heads[i], o);
}
/*Deletes objective using it's id*/
void delete(unsigned long id){
int i = hash(id, M);
heads[i] = removeList(heads[i], id);
}
/*Returns objective with said id*/
pObjective searchObj(unsigned long id){
int i = hash(id, M);
return searchObjAux(heads[i], id);
}
/*Inserts objective into list*/
link insertBegin(link h, pObjective obj){
link new = (link)malloc(sizeof(struct nodehash));
new->obj = obj;
new->next = h;
return new;
}
/*Searches objective by id in a list*/
int searchList(link h, unsigned long id){
link t;
int count = 0;
for(t = h; t != NULL; t = t->next){
if(t->obj->id == id)
count++;
}
return count++;
}
/*Removes objective from list*/
link removeList(link h, unsigned long id){
link t, x, z;
for(t = h; t != NULL; t = t->next){
if(t->next->obj->id == id)
x = t;
}
z = x->next;
x->next = z->next;
free(z);
return h;
}
/*Returns objetive from said id from list*/
pObjective searchObjAux(link h, unsigned long id){
link t, x;
for(t = h; t != NULL; t = t->next){
if(t->obj->id == id)
x = t;
}
return x->obj;
}
I'm quick testing the funcions addObj (adds an objective with dependencies) and addNoDeps (adds an objective with no dependencies) on my main:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "OBJECTIVES.h"
int main(){
unsigned long array [1] = {3};
Init(11);
addNoDeps(1, "tarefa1", 20);
addObj(2, "tarefa2", 20, array);
return 0;
}
But i keep getting segmentation fault(core dumped) and i can't figure out why. Is my implementation wrong? Are the functions wrong? I can't get to the problem, can someone help me?

I can't run your code right now so I can't analyze the core dump, but I believe what is happening is that you are trying to access memory that has already been freed. At the end of addNoDeps, you free the pObjective after putting it into the list. Then, when you addObj after, you search the list and check to make sure that the links object associated with it is not null. Specifically this code:
for(t = h; t != NULL; t = t->next){
if(t->obj->id == id)
count++;
You only check to see that the t (links pointer) is not null, but since you freed the previous object, the t->obj pointer is not pointing to initialized memroy. Therefore trying to access it via t->obj->id is accessing uninitialized memory. If you remove the free(obj) at the end of your addNoDeps and addObj functions you should be fine. You may also want to add checks to make sure that t->obj is not null as well. In general segmentation faults are caused by accessing uninitialized memory, so when debugging check for accessing pointers after a free, double frees, and other things. Also learning to use GDB can help a lot in these situations.

Related

gcc segmentation fault due to a pointer variable that changed itself

Let's consider follwing code.
message_word.h
struct array_int64{
int size;
long* value;
};
struct array_int64* array_int64_create(int size);
int array_int64_get(void* value, const struct array_int64* array, int index);
int array_int64_set(struct array_int64* array, long value, int index);
void array_int64_free(struct array_int64* array);
struct hello_message{
struct array_int64* test;
};
struct hello_message* hello_message_create();
void hello_message_free(struct hello_message* msg);
message_word.c
#include <stdlib.h>
#include <string.h>
#include "word_message.h"
struct array_int64* array_int64_create(int size){
struct array_int64* new_array = (struct array_int64*)malloc(sizeof(struct array_int64));
new_array->size = size;
new_array->value = (long*)malloc(sizeof(long) * (new_array->size));
return new_array;
}
int array_int64_get(void* value, const struct array_int64* array, int index){
long* vvalue = (long*)value;
if(index >= array->size)
return -1;
*vvalue = (array->value)[index];
return 0;
}
int array_int64_set(struct array_int64* array, long value, int index){
if(index >= array->size)
return -1;
array->value[index] = value;
return 0;
}
void array_int64_free(struct array_int64* array){
if(array != NULL && array->value != NULL)
free(array->value);
if(array != NULL)
free(array);
}
struct hello_message* hello_message_create(){
struct hello_message* new_msg = (struct hello_message*)malloc(sizeof(struct hello_message));
new_msg->test = array_int64_create(5);
return new_msg;
}
void hello_message_free(struct hello_message* msg){
if(msg == NULL) return;
array_int64_free(msg->test);
free(msg);
}
main.c
#include "word_message.h"
#include <stdio.h>
//struct hello_message* msg = NULL;
int main(void)
{
struct hello_message* msg = hello_message_create();
//msg = hello_message_create();
array_int64_set(msg->test, 10, 0);
int number;
array_int64_get(&number, msg->test, 0);
printf("value is: %d\n", number);
array_int64_get(&number, msg->test, 0);
printf("value is: %d\n", number);
hello_message_free(msg);
return 0;
}
When I compile and run this code with gcc-9.3.0 or gcc-7.5.0 on Ubuntu the msg reference address changes itself when array_int64_get is executed. However, if you make msg a global variable like in the comment, it does not change.When I compile and run this code on Ubuntu 18, gcc-7.5.0 on a Raspberry Pi (ARM), everything works fine. It also works fine on windows. So is this a bug in gcc?
This is the result of running on ubuntu18(x86)+gcc-9.3.0
running on ubuntu18
This is the result of running it on a Raspberry Pi
run on raspberry pi
int array_int64_get(void* value, const struct array_int64* array, int index){
long* vvalue = (long*)value;
...
}
int main() {
int number;
array_int64_get(&number, msg->test, 0);
}
You are passing an int with a pointer and then reading it as long. That's invalid. If you want it long, be it long.
Use longs everywhere, and if your functions wants a long make it take a long.
int array_int64_get(long *vvalue, const struct array_int64* array, int index){
...
}
int main() {
int number;
array_int64_get(&number, ...); // compiler warning!
long correctnumber;
array_int64_get(&correctnumber, ...); // all fine!
}
Conceptually, your code is odd. long is not 64-bit long, it's at least 32-bits long. It can have 32-bits, 64-bits, 1000-bits. For 64-bit, use uint64_t or int64_t from #include <stdint.h>. See https://en.cppreference.com/w/c/types/integer .
free(NULL) is totally fine - no need to check for it.
Do I cast the result of malloc?

Adding an update node into a linked list

I'm having trouble figuring out how to do a update function in this current code. He does not want us to change the student ID, but wants us to change the GPA and the major associated with the current student. He wants us to
create a prompt for the user to enter the student ID number to search for. Also to create a prompt to determine which field the user wished to update then accept new input for that field and change the contents of the appropriate student. I don't want the answer. I just need to be pointed in the right direction.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stuff.h"
#include "staticll.h"
int compareID(void*, void*);
int compareGPA(void*, void*);
int compareMajor(void*, void*);
void main(void)
{
FILE *fp;
struct Node List[15];
int Begin, current, newStuff, oldStuff;
double oldData;
int(*fnPtr)();
fnPtr = compareMajor;
Begin = 0;
struct Student *stu;
for (int x = 0; x < 3; x++)
{
newStuff = FindSpace(List);
List[newStuff].nodeData = readData(stdin);
addNode(Begin, List, newStuff);
}
for (int i = Begin; i != -1; i = List[i].nodeLink)
fprintf(stdout,"%.2f\n",((Stu*)List[i].nodeData));
for (int i = Begin; i != -1; i = List[i].nodeLink)
free(List[i].nodeData);
}
int compareID(void *p1, void *p2)
{
if (((Stu*)p1)->stuID == ((Stu*)p2)->stuID)
return 0;
else
if ((((Stu*)p1)->stuID < ((Stu*)p2)->stuID))
return 1;
else
return -1;
}
int compareGPA(void *p1, void *p2)
{
if (((Stu*)p1)->stuGPA == ((Stu*)p2)->stuGPA)
return 0;
else
if ((((Stu*)p1)->stuGPA < ((Stu*)p2)->stuGPA))
return 1;
else
return -1;
}
int compareMajor(void *p1, void *p2)
{
return strcmp(((Stu*)p1)->stuMajor, ((Stu*)p2)->stuMajor);
}
This is the current code I've created for the update function needed.
#include"Stuff.h"
void updateNode(struct Node List[], int current, int *NewData)
{
int temp1;
List[current].nodeLink = List[*NewData].nodeData;
List[*NewData].nodeLink = NewData;
printf("enter the student ID number ");
}

C Memory Management -> Hash

I don't know if my problem is a memory leak, or i'm not acessing the hashtable in the correct way.
My hash.h
#define HASHSIZE 31
#define EMPTY ""
#define DELETED "-"
typedef char KeyType[9];
typedef void *Info;
typedef struct entry
{
KeyType key;
Info info;
}Entry;
typedef Entry HashTable[HASHSIZE];
My hash.c
int Hash(KeyType k){
return atoi(k)%HASHSIZE;
}
void InitializeTable(HashTable t){
for(int i=0; i < HASHSIZE; i++){
strncpy(t[i].key,EMPTY,9);
}
}
void ClearTable(HashTable t){
InitializeTable(t);
}
void InsertTable_LP(HashTable t, KeyType k, Info i){
int a = 0;
int hash = Hash(k);
while((a<HASHSIZE)
&& strcmp(t[hash].key,EMPTY)!=0
&& strcmp(t[hash].key,DELETED)!=0 ){
hash = (hash + 1) % HASHSIZE;
a++;
}
strncpy(t[hash].key,k,9);
t[hash].info = i;
printf("Value of info is %d\n",(int)t[hash].info);
}
int RetrieveTable_LP(HashTable t, KeyType k){
int a=0;
int hash = Hash(k);
while(a<HASHSIZE
&& strcmp(t[hash].key,k)!=0
&& strcmp(t[hash].key,EMPTY)!=0){
hash=(hash+1) % HASHSIZE;
a++;
}
if(strcmp(t[hash].key,k)==0)
return hash;
return -1;
}
int main(){
HashTable *t = malloc(HASHSIZE*sizeof(Entry));
int valores[] = {1,2,3,4,5,6,7,8,9};
ClearTable(*t);
InsertTable_LP(*t,"1",valores);
InsertTable_LP(*t,"2",valores+1);
InsertTable_LP(*t,"3",valores+2);
InsertTable_LP(*t,"4",valores+3);
InsertTable_LP(*t,"5",valores+4);
int pos = RetrieveTable_LP(*t,"2");
if(pos==-1){
printf("Error\n");
}
else
printf("Position %d\n",pos);
printf("okay %d\n",(int)t[pos]->info);
printf("asdasdas\n");
return 1;
}
My output is
Value of info is 1537727040
Value of info is 1537727044
Value of info is 1537727048
Value of info is 1537727052
Value of info is 1537727056
Position 2
okay 0
If anyone could explain me, thanks in advance.
valores is an array. You are inserting Info which has been typedefed to void *. You need to fix those things.
Your malloc is not necessary the reason why that wasn't obvious is because it was hidden by the way you typedefd the HashTable, don't ever do that, the following code works as you expected yours to do
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HASHSIZE 31
#define EMPTY ""
#define DELETED "-"
typedef char KeyType[9];
typedef void *Info;
typedef struct entry
{
KeyType key;
Info info;
}Entry;
typedef Entry HashTable[HASHSIZE];
int Hash(KeyType k){
return atoi(k)%HASHSIZE;
}
void InitializeTable(HashTable t) {
int i;
for(i=0; i < HASHSIZE; i++) {
strncpy(t[i].key, EMPTY, 9);
}
}
void ClearTable(HashTable t) {
InitializeTable(t);
}
void InsertTable_LP(HashTable t, KeyType k, Info i){
int a = 0;
int hash = Hash(k);
while((a<HASHSIZE) && strcmp(t[hash].key, EMPTY) !=0 && strcmp(t[hash].key, DELETED) !=0 ) {
hash = (hash + 1) % HASHSIZE;
a++;
}
strncpy(t[hash].key, k, 9);
t[hash].info = i;
printf("Value of info is %p\n", t[hash].info);
}
int RetrieveTable_LP(HashTable t, KeyType k){
int a=0;
int hash = Hash(k);
while(a<HASHSIZE
&& strcmp(t[hash].key,k)!=0
&& strcmp(t[hash].key,EMPTY)!=0){
hash=(hash+1) % HASHSIZE;
a++;
}
printf("%s, %s\n", t[hash].key, k);
if(strcmp(t[hash].key, k)==0)
return hash;
return -1;
}
int main(){
/*
* You don't need to malloc, since HashTable is an array,
* and it does not need to be a pointer, since it decays
* to one when passed as such.
*/
HashTable t;// = malloc(HASHSIZE * sizeof(Entry));
int valores[] = {1,2,3,4,5,6,7,8,9};
ClearTable(t);
InsertTable_LP(t,"1",valores);
InsertTable_LP(t,"2",valores+1);
InsertTable_LP(t,"3",valores+2);
InsertTable_LP(t,"4",valores+3);
InsertTable_LP(t,"5",valores+4);
int pos = RetrieveTable_LP(t, "2");
if(pos==-1) {
printf("Error\n");
}
else
{
printf("Position %d\n",pos);
printf("okay %p\n", t[pos].info);
}
printf("asdasdas\n");
return 1;
}
your typedef of the HashTable makes it hard to know what to do with a HashTable type variable, that is not a very good use of typedef.
Also the second printf will be executed regardless of the condition
else
printf("Position %d\n",pos);
printf("okay %d\n",(int)t[pos]->info);
you need to add {
else
{
printf("Position %d\n",pos);
printf("okay %d\n",(int)t[pos]->info);
}

Thread not working properly - C

I've made a queue header file and I've tried to use it with threads.
What I'm doing is making 2 threads, 1 for reading the characters from the code file and entering the characters to the queue and the other thread is trying to print the characters to the console.
The problem is that no characters are being printed to the console and I can't figure out why.
queue.h :
#ifndef QUEUE_INT
#define QUEUE_INT
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
int *elementData;
unsigned int queueSize;
unsigned int capacityIncrement;
unsigned int elementCount;
} Queue;
void queue_initialize(Queue*, unsigned int);
int queue_add(Queue*, int);
void queue_poll(Queue*);
int queue_peek(const Queue*);
void queue_destroy(Queue*);
bool queue_isEmpty(const Queue*);
void queue_setCapacityIncrement(Queue*, unsigned int);
unsigned int queue_getCapacityIncrement(const Queue*);
unsigned int queue_getNumberOfElements(const Queue*);
unsigned int queue_getSize(const Queue*);
void queue_initialize(Queue *p, unsigned int capacityIncrement)
{
p->elementData = NULL;
p->queueSize = 0;
p->capacityIncrement = capacityIncrement;
p->elementCount = 0;
}
int queue_add(Queue *p, int value)
{
if(p->elementCount == p->queueSize)
{
int newQueueSize = p->queueSize + p->capacityIncrement;
void *temp = realloc(p->elementData, sizeof(*p->elementData) * newQueueSize);
if(temp == NULL || newQueueSize == 0)
{
return 1;
}
p->queueSize = newQueueSize;
p->elementData = temp;
}
p->elementData[p->elementCount] = value;
p->elementCount++;
return 0;
}
void queue_poll(Queue *p)
{
if(!queue_isEmpty(p))
{
p->elementCount--;
if(p->queueSize - p->elementCount == p->capacityIncrement / 2 + p->capacityIncrement)
{
int newQueueSize = p->queueSize - p->capacityIncrement;
p->elementData = realloc(p->elementData, sizeof(*p->elementData) * newQueueSize);
p->queueSize = newQueueSize;
}
for(int i = 0; i < p->elementCount; i++)
{
p->elementData[i] = p->elementData[i + 1];
}
}
}
int queue_peek(const Queue *p)
{
if(!queue_isEmpty(p))
{
return p->elementData[0];
}
return 0;
}
void queue_destroy(Queue *p)
{
free(p);
}
bool queue_isEmpty(const Queue *p)
{
return p->elementCount == 0;
}
void queue_setCapacityIncrement(Queue *p, unsigned int capacityIncrement)
{
p->capacityIncrement = capacityIncrement;
}
unsigned int queue_getCapacityIncrement(const Queue *p)
{
return p->capacityIncrement;
}
unsigned int queue_getNumberOfElements(const Queue *p)
{
return p->elementCount;
}
unsigned int queue_getSize(const Queue *p)
{
return p->queueSize;
}
#endif
Code file :
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <process.h>
#include <time.h>
#include "queue.h"
bool isFillQueueThreadRunning;
bool isQueueProcessing;
void fillQueueThread(void*);
void popQueueThread(void*);
int main()
{
srand(time(NULL));
Queue q1;
queue_initialize(&q1, 4);
HANDLE hFillQueueThread = (HANDLE)_beginthread(fillQueueThread, 0, (void*)&q1);
HANDLE hPopQueueThread = (HANDLE)_beginthread(fillQueueThread, 0, (void*)&q1);
WaitForSingleObject(hFillQueueThread, 1000 * 300);
WaitForSingleObject(hPopQueueThread, 1000 * 300);
return 0;
}
void fillQueueThread(void *p)
{
isFillQueueThreadRunning = true;
Queue *q = (Queue*)p;
FILE *f = fopen(__FILE__, "r");
int b;
while((b = getc(f)) != EOF)
{
Sleep(rand() % 50);
while(isQueueProcessing)
{
}
isQueueProcessing = true;
if (queue_add(q, b) == 1)
{
break;
}
isQueueProcessing = false;
}
fclose(f);
isFillQueueThreadRunning = false;
}
void popQueueThread(void *p)
{
Queue *q = (Queue*)p;
Sleep(10);
int b;
while(isFillQueueThreadRunning || q->elementCount > 0)
{
while(isQueueProcessing)
{
}
isQueueProcessing = true;
b = queue_peek(q);
queue_poll(q);
putchar(b);
isQueueProcessing = false;
}
}
you _beginthread the fillQueueThread twice.
and
you never initialize isFillQueueThreadRunning reliably. Code may depend on uninitialized variable.
from what I've seen
Your queue implementation is far from thread safe. As Joachim mentioned, please education yourself with thread synchronization primitives. A simple mutex would go a long way here.
See: What is a mutex?
As to your large blocks of spaces, you use the output of queue_peek() unconditionally which can (and will frequently) be NULL.
Do you know that the result of putchar(NULL) is?

Code wont compile

I'm trying to use a queue in my program, but it won't compile and I don't know why. The relevant part of the code is as follows.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef CUSTOMER
#define CUSTOMER
typedef int bool;
int r;
typedef struct{
int arrival;
int leaving;
} Customer;
static const int MAX_LENGTH = 100;
typedef struct{
int head;
int length;
Customer customer[MAX_LENGTH];
} CustomerLine;
void initializeQueue(CustomerLine* queue)
{
(*queue).head = 0;
(*queue).length = 0;
}
bool hasNext(CustomerLine* queue)
{
return (*queue).length > 0;
}
bool isFull(CustomerLine* queue)
{
return (*queue).length == MAX_LENGTH;
}
bool enqueue(CustomerLine* queue, Customer* customer)
{
if(isFull(queue))
return 0;
int index = ((*queue).head + (*queue).length) % MAX_LENGTH;
(*queue).customer[index] = *customer;
(*queue).length++;
return 1;
}
Customer* dequeue(CustomerLine* queue)
{
if(!hasNext(queue))
return 0;
Customer* result = &(*queue).customer[(*queue).head];
(*queue).length--;
(*queue).head = ((*queue).head + 1) % MAX_LENGTH;
return result;
}
The error says "Variably Modified 'customer' at file scope" I am a beginner at programming and just doing this is starting to get beyond my abilities so any help would be very much appreciated.
The line
static const int MAX_LENGTH = 100
is the problem. Replace it with
#define MAX_LENGTH 100
See why here and more explanations here or here or again here.
Furthermore:
You need an #endif after the #ifndef.
You need a main function somewhere.
In C, const means read-only, not constant and usable just like a macro. You cannot use a variable to specify the dimension of an array as you do here:
static const int MAX_LENGTH = 100;
typedef struct{
int head;
int length;
Customer customer[MAX_LENGTH]; /* Wrong. MAX_LENGTH is not a compile time constant. */
} CustomerLine;

Resources