i am trying to use arrays as hashtable, each array ponits to its own linked list
the size is to check that the number of nodes of the linked list is 32.
my problem here is that i get segmentation fault, but i cant see any error in my pointers, here is the full code.
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
typedef int bool;
enum { false, true };
void main(int argc, char *argv[])
{
// create linked list---------------------------
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *first, *last, *temp, *newNode=0 ;
int count = 0;
first = NULL;
last=NULL;
temp=NULL;
newNode=0;
//-----------------------------------------------------------------------
//filling the text file with billions of integers------------------------------------------------
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
int i = 0;
unsigned long long randomvalue;
for (i = 0; i < 10000000; i++)
{
randomvalue = random();
randomvalue <<= 16; // just picked 16 at random
randomvalue ^= random(); // you could also use + but not "or";
randomvalue %= 10000000000ULL;
fprintf(f,"%lld \n",randomvalue);
}
fclose(f);
NODE* array[312500];
first = (NODE *)malloc(sizeof(NODE));
last= (NODE *)malloc(sizeof(NODE));
temp = (NODE *)malloc(sizeof(NODE));
newNode = (NODE *)malloc(sizeof(NODE));
FILE *file = fopen ("file.txt", "r");
int x=0;
for ( x=0; x<=312500; x++)
{
while (count <=32)
{
fscanf (file, "%d", &temp->num);
temp->ptr=NULL;
newNode->num=temp->num;
newNode->ptr=NULL;
if (first != 0)
{
last->ptr=newNode;
last=newNode;
count=count+1;
}
else
{
first = newNode;
last = newNode;
count=count+1;
}
fflush(stdin);
newNode->ptr=0;
newNode=NULL;
}
count =0;
array[x]->ptr=first;
first->ptr=0;
first=NULL;
last->ptr=0;
last=NULL;
}
fclose (file);
temp->ptr = 0;
temp=NULL;
}
According to gdb:
Program received signal SIGSEGV, Segmentation fault. 0x00401618 in
main (argc=1, argv=0x8d0ce0) at tryy.c:77 77
newNode->num=temp->num;
(gdb) p newNode
$10 = (NODE *) 0x0
So you are getting a SIGSEGV because you are trying to access memory at 0x0.
Problems I see with your code(they may not be directly related to why newNode becomes NULL):
first = (NODE *)malloc(sizeof(NODE));
0x0 is one of the valid return values from malloc. Almost always I prefer to have a if check after a malloc call. Similarly for other api calls where a null ptr can be returned
You have declared NODE* array[312500]; but the value of x goes upto 312500 so you may end up accessing array[312500] which is undefined behavior. (Often causes memory corruption by overwriting values of other variables on stack)
Related
I am building a program for a project. One of the requirements for the project is a function that selects a random node from my linked list of 3000 words.
I tried to do this by creating a function that generates a random number from 0 to 2999. After this, I created another function that follows a for loop starting from the head and moving to the next node (random number) times.
My random number generator is working fine, but my chooseRand() function is not.
Please help, the random number generator and the chooseRand() function are the last two functions above main. Also, my code is a bit messy, sorry.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int nodeNum;
int chances;
char* secret;
/*Node of linked list*/
typedef struct node {
char *data;
struct node *next;
} node;
node *start = NULL;
node *current;
/*Void function to print list*/
void printList(struct node *node)
{
while (node != NULL) {
printf("%s ", node->data);
node = node->next;
}
}
/*Appending nodes to linked list*/
void add(char *line) {
node *temp = malloc(sizeof(node));
temp->data = strdup(line);
temp->next = NULL;
current = start;
if(start == NULL) {
start = temp;
} else {
while(current->next != NULL) {
current = current->next;
}
current->next = temp;
}
}
void readfile(char *filename) {
FILE *file = fopen(filename, "r");
if(file == NULL) {
exit(1);
}
char buffer[512];
while(fgets(buffer, sizeof(buffer), file) != NULL) {
add(buffer);
}
fclose(file);
}
node *listSearch(node* start, char *nodeSearched){
node *p;
for (p = start; p != NULL; p = p->next)
if (strcmp(p->data, nodeSearched) == 0)
printf("%s", p->data);
return NULL;
}
node *letterSearch(node* start, int i){
node *p;
for (p = start; p != NULL; p = p->next)
if (strlen(p->data) == i)
{
printf("\n %s", p->data);
free(p);
p = NULL;
}
return NULL;
}
void chooseRand(struct node* start)
{
node* p;
int n;
p = start;
for(n = 0; n != nodeNum; n++)
{
p = p->next;
}
printf("%s", p->data);
}
void randNum(int lower, int upper)
{
srand(time(0));
nodeNum = (rand() % (upper - lower + 1)) + lower;
}
int main(){
randNum(0, 2999);
chooseRand(start);
return 0;
}
As others has said, the problem is that you don't have initialized the linked list yet, because of what your are getting a segmentation fault. So, in addition to initializing the list first, you must also introduce checks in the implementation of the chooseRand function, to check that if you reach the end of the list, without reaching the desired index, you stop executing the foor loop, otherwise you will be potentially exposed to segmentation faults.
Improve chooseRand implementation, to prevent segmentation fault either, when the linked list is empty, or when the randomly generated nodeNum is grater than the the index of the list's last item:
void chooseRand(struct node* start)
{
node* p;
int n;
p = start;
if(p == NULL){
printf("The list is empty!");
return;
}
// Also, we must stop the iteration, if we are going to pass the end of the list, you don't want a segmentation fault because trying to access a NULL pointer:
for(n = 0; n != nodeNum && p->next != NULL; n++)
{
p = p->next;
}
// If p == NULL, the list was not big enough to grab an item in the `nodeNum` index:
printf("%s", (n != nodeNum) ? "Not found!" : p->data);
}
Initialize the linked list, with the content of some file on disk:
int main(){
randNum(0, 2999);
// Fill the linked list with the content of a file in disk, calling your method:
char fileName[] = "PutYourFileNameHere.txt";
readfile(fileName);
chooseRand(start);
return 0;
}
There is another fix that you must do, and it is free the memory being hold by the pointer field data of your structure, in the implementation of your method letterSearch. Inside the if statement, you're de-allocating the memory hold by the p pointer, but you aren't de-allocating the memory assigned to the pointer p->data, this will cause a memory leak. When you in the function add, initialized p->data with the result of the call to the function strdup(line), what this function does is allocate enough memory in the heap, copies to it the buffer pointed by the line argument, and give to you back a pointer to the new allocated memory, that you're storing in the p.data field; a pointer that you should free when you're done with it, otherwise your program will have potential memory leaks. So I will modify your function letterSearch as folollows:
node *letterSearch(node* start, int i){
node *p;
for (p = start; p != NULL; p = p->next)
if (strlen(p->data) == i)
{
printf("\n %s", p->data);
// Free p->data before free p:
free(p->data);
free(p);
p = NULL;
}
return NULL;
}
References:
strdup
I am trying to understand the linked list in C. So i am trying to write a program which will read from a file and create a linked list. But I hit a roadblock which I couldn't find a reason why.
Although I set the head value node *h to n only one time it looks like the value is automatically changing to the next value of n. To check I used printf at the end. All of them are returning the same result. Can anyone help please?
P.S - This is my first time using stackoverflow.
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
char *name;
struct node *next;
} node;
int main (void)
{
//load the file.
FILE *fp = fopen("dictionary.txt", "r");
if (fp == NULL)
{
printf("Unable to open the file\n");
}
char characters[45];
//initialize the linked list.
node *n , *t, *h;
int flag = 0;
while(fgets(characters, 45, fp) != NULL)
{
//define node for n
n = malloc(sizeof(node));
if (n == NULL)
{
printf("Out of memory!!!");
}
//set the value of n
n -> name = characters;
//set the temp & head value to n first time
if (flag == 0)
{
t = n;
h = n;
}
//set the temp -> next value to n after first time
else
{
t -> next = n;
}
flag = 1;
}
printf("%s\n", h -> name);
printf("%s\n", t -> name);
printf("%s\n", n -> name);
}
name member in your node struct is only a pointer to a string (character array).
each node you assign name to point to the very same character array:
char characters[45];
you should allocate the character array for any node:
#define MAX_LEN 45
typedef struct node
{
char name[MAX_LEN];
struct node *next;
} node;
and copy the string:
//set the value of n
strncpy(n -> name,characters, MAX_LEN);
// ensure null terminated
n->name[MAX_LEN-1] = '\0';
I'm trying to setup a graph in C. I tried the graph with user input and it works perfectly. However, i am trying to implement a read from file. The last else statement is where the error is coming from because when i commented it out it compiles without any problems. I have included a comment over the block i think that has the problem. Please let me know if there is anything else needed for this question.
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node* next;
};
//int counter and mainVertex would be used to determine if graph is connected.
// void graphConnection(){
//
//
//
//
//
//
// }
char* deblank(char* input)
{
int i,j;
char *output=input;
for (i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else
j--;
}
output[j]=0;
return output;
}
struct node *G[1000];
int counter = 0;
char *mainVertex;
void readingEachLine(){
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
//Read file and exit if fail
fp = fopen("test.txt", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
line = deblank(line);
int i = 0;
struct node* cursor = malloc(sizeof(struct node));
struct node* secondcursor = malloc(sizeof(struct node));
struct node* tempitem;
while(line[i] != '\n'){
//If its the first of the line look into the array and set struct cursor to the corresponding
//array position
if (i == 0){
mainVertex[counter] = line[0];
int convertor = line[i] - '0';
cursor = G[convertor];
counter++;
}
//if its not the first, then set a struct with that number as data
else{
tempitem = malloc(sizeof(struct node));
int convertor = line[i] - '0';
tempitem->data = convertor;
tempitem->next = NULL;
}
//if there is no element connected to the struct in array, connect the tempitem
if (cursor->next == NULL){
cursor->next = tempitem;
}
//If there are already connected elements, loop until the end of the linked list
//and append the tempitem
//ERROR: I GET SEGMENTATION FAULT FROM HERE. TRIED AFTER COMMENTING IT OUT
else{
secondcursor = cursor;
while(secondcursor->next != NULL){
secondcursor = secondcursor->next;
}
secondcursor->next = tempitem;
}
i++;
}
printf("\n");
}
}
int main(void){
for (int i = 1; i < 1000; i++)
{
G[i]= malloc(sizeof(struct node));
G[i]->data = i;
G[i]->next = NULL;
}
readingEachLine();
}
EDIT: This is how the text file looks like:
1 3 4
2 4
3 1 4
4 2 1 3
Your code has several misconceoptions:
Apparently, you can have a maximum of 1,000 nodes. You have an array G of 1,000 head pointers to linked lists. Don't allocate memory for all 1,000 nodes at the beginning. At the beginning, all lists are empty and an empty linked list is one that has no node and whose head is NULL.
In your example, cursor is used to iterate oer already existing pointers, so don't allocate memory for it. If you have code like this:
struct node *p = malloc(...);
// next use of p:
p = other_node;
you shouldn't allocate. You would overwrite p and lose the handle to the allocated memory. Not all pointers have to be initialised with malloc; allocate only if you create a node.
Your idea to strip all spaces from a line and then parse single digits will fail if you ever have more then 9 nodes. (But you cater for 1,000 node.) Don't try to parse the numbers yourself. There are library functions for that, for example strtol.
It is not clear what mainVertex is supposed to be. You use it only once, when you assign to it. You treat it like an array, but it is a global pointer, initialised to NULL. When you dereference it, you get undefined behaviour, which is where your segmentation fault probably comes from.
Here's a program that does what you want to do. (It always inserts nodes at the head for simplicity and it should have more allocation checks.)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
maxNodes = 1000
};
struct node{
int data;
struct node* next;
};
struct node *G[maxNodes];
size_t nnode = 0;
int read_graph(const char *fn)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
fp = fopen(fn, "r");
if (fp == NULL) return -1;
while (getline(&line, &len, fp) != -1) {
char *p;
char *end;
int id;
int n;
id = strtol(line, &end, 10);
if (end == line) continue;
if (id < 1 || id > maxNodes) break;
if (id > nnode) nnode = id;
id--;
p = end;
n = strtol(p, &end, 10);
while (p != end) {
struct node *nnew = malloc(sizeof(*nnew));
nnew->data = n - 1;
nnew->next = G[id];
G[id] = nnew;
p = end;
n = strtol(p, &end, 10);
}
}
fclose(fp);
free(line);
return 0;
}
int main(void)
{
if (read_graph("test.txt") < 0) {
fprintf(stderr, "Couldn't gread raph.\n");
exit(1);
}
for (int i = 0; i < nnode; i++) {
struct node *p = G[i];
if (p) {
printf("%d:", i + 1);
for (; p; p = p->next) {
printf(" %d", p->data + 1);
}
puts("");
}
}
for (int i = 0; i < nnode; i++) {
struct node *p = G[i];
while (p) {
struct node *old = p;
p = p->next;
free(old);
}
}
return 0;
}
I am going through the cs50x course, doing speller check program. In my fourth implementation of this program I've ran into malloc problem.
This time I decided to implement a binary tree.
I've read a lot of threads about this problem and checked my code for several times, but I still can't understand what I'm doing wrong.
Problem appears in the recursive function that loads dictionary into ram.
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"
// standart node of the trie
typedef struct node
{
char word[LENGTH + 1];
struct node* less;
struct node* more;
}
node;
// Function definitions
void unload_node(node* pr_node);
void ld_bin_tree(int min, int max, node* node);
bool check_word(char* lword, node* parent);
// Global variables
// root of the tree
node* root;
FILE* dict;
//size of dictionary
int dict_size = 0;
bool load(const char* dictionary)
{
// open dictionary file
dict = fopen(dictionary, "r");
int nwords = 0;
int min = 0;
int max = 0;
root = malloc(sizeof(node));
//if file wasn't open
if(dict == NULL)
{
printf("Error opening ditionary file!");
return false;
}
// tmp storage for read word
char buffer[LENGTH + 1];
// count words in the dictionary
while(fscanf(dict, "%s", buffer) > 0)
{
nwords++;
}
max = nwords;
rewind(dict);
ld_bin_tree(min, max, root);
// close file
fclose(dict);
return false;
}
/*
* Recursion function to fill in binary tree
*/
void ld_bin_tree(int min, int max, node* node)
{
// tmp word holder
char buffer[LENGTH + 1];
// next mid value
int mid = (min + max) / 2;
// if mid == 0 then the bottom of the brunch reached, so return
if(max - min < 2)
{
if(min == 0)
{
fscanf(dict, "%s", node->word);
dict_size++;
return;
}
return;
}
// go through the dict to the mid string
for(int i = 0; i <= mid; i++)
{
fscanf(dict, "%s", buffer);
}
// fill in word
strcpy(node->word, buffer);
// go at the beginning of the dict
rewind(dict);
// fill in input node
// fill in new children nodes
struct node* new_node = malloc(sizeof(node));
node->less = new_node;
// send lesser side
ld_bin_tree(min, mid, node->less);
new_node = malloc(sizeof(node));
node->more = new_node;
// send greater side
ld_bin_tree(mid, max, node->more);
dict_size++;
return;
}
I've tried to get this error using valgrind but it gives me a lot of warnings about reading and writing in unappropriated memory blocks. But because I'm not very good with programming yet, this warnings didn't give me a clue of what's happening.
So I'm asking for more precise help, if it's possible. Thank you in advance.
Other parts of the speller program may be found here:
https://www.dropbox.com/sh/m1q1ui2g490fls7/AACnVhjjdFpv1J0mUUhY2uV2a?dl=0
In function ld_bin_tree() you have
struct node* new_node = malloc(sizeof(node));
Here node is a pointer not an object of type struct node.
You have
node *node;
So the global definition of node is being overwritten which makes it a pointer.
So you are not allocating memory for your whole structure. You should have
struct node* new_node = malloc(sizeof(struct node));
I coded a simple source. It contains a queue and some of the function a queue needs but for some reason malloc() only works once.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define QUEUE sizeof(Queue)
Definition of the Node, which is an element of the list, and the queue.
typedef struct node {
char * value;
struct node * next;
} Node;
typedef struct queue {
Node * head;
Node * tail;
} Queue;
int initialization(void ** list, int type){
int code = -1;
//create an empty list.
//if queue dynamically allocate memory and assign NULL to both properties head and tail.
return code;
}
enqueue() add one element in the queue at a time. but for some reason it can only add one element then the program crashes.
int enqueue(Queue * q, char * instruction){
int code = -1;
if(q != NULL){
printf("Prepare to enqueue!\n");
Node * n = NULL;
n = (Node*)malloc(sizeof(Node));
if(n != NULL){
printf("Node created!\n");
strcpy(n->value, instruction);
n->next = NULL;
//if first value
if(q->head == NULL){
q->head = n;
q->tail = n;
printf("Enqueue first Node\n");
}
else {
q->tail->next = n;
q->tail = n;
printf("Enqueue another Node\n");
}
code = 0;
printf("Node \"%s\" Enqueued\n", instruction);
}
}
return code;
}
int dequeue(Queue * q){
int code = -1;
//dequeuing code here.
return code;
}
int isEmpty(void * list, int type){
int code = 0;
//check if the list is empty
return code;
}
the for loop in the main() function never reaches 3
int main(int argc, char * argv[]){
Queue * queue = NULL;
initialization((void*)&queue, QUEUE);
int i = 0;
for(i = 0; i < 3; i++){
if(enqueue(queue, "some value") != 0){
printf("couldn't add more Node\n");
break;
}
}
while(!isEmpty(queue, QUEUE)){
dequeue(queue);
}
return 0;
}
The initialization function is written this way because it should also be able to initialize stacks (I removed the stack code to reduce the source but even without it the bug persist). I also put printfs to debug the code. And I have more than enough memory to make this simple code run how it should.
Thanks in Advance!
Running this, I crash with a segmentation fault, as I'd expect:
n = (Node*)malloc(sizeof(Node));
n is allocated, it's contents uninitialized and effectively random
if(n != NULL){
n is not NULL, so...
strcpy(n->value, instruction);
And we crash.
See the problem? n->value is a pointer to nowhere. Or, to somewhere, but nowhere known. Nowhere good. And we're just dumping a string into that space.
Either change the Node struct so that value is a char [SOME_SIZE], or use strdup() instead of strcpy(), to actually allocate some memory for the poor thing.
n->value = strdup(instruction);