I am having some issues with dynamically allocating a string for a node in a tree. I have included my node structure below for reference.
struct node
{
char *string;
struct node *left;
struct node *right;
};
typedef struct node node;
I am supposed to read words from a text file and then store those words into a tree. I am able to store char arrays that have been defined, such as char string[20] without problems, but not strings that are supposed to be dynamically allocated.
I am only going to post the code I am using to read my file and try to create the dynamically allocated array. I have already created the file pointer and checked that it is not NULL. Every time I try to run the program, it simply crashes, do I need to try and read the words character by character?
//IN MAIN
node *p, *root ;
int i;
int u;
root = NULL;
char input[100];
while(fscanf(fp, "%s", &input) != EOF)
{
//Create the node to insert into the tree
p = (node *)malloc(sizeof(node));
p->left = p->right = NULL;
int p = strlen(input); //get the length of the read string
char *temp = (char*) malloc(sizeof(char)*p);
//malloc a dynamic string of only the length needed
strcpy(local, input);
strcpy(p->word,local);
insert(&root, p);
}
To be completely clear, I only want advice regarding the logic of my code, and only would like someone to help point me in the right direction.
You are invoking many undefined behaviors by
passing pointer to object having wrong type to scanf(). i.e. In fscanf(ifp, "%s", &input), char(*)[100] is passed where char* is expected
accessing out-of-range of allocated buffer when storeing terminating null-character in strcpy(local, input);
using value of buffer allocated via malloc() and not initialized in strcpy(curr->word,local);
Your code should be like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node_t {
struct node_t* left, *right;
int count;
char* word;
} node;
void insert(node ** tree, node * item);
int main(void) {
FILE* ifp = stdin;
node * curr, * root;
int i;
int u;
root = NULL;
char input[100];
/* you should specify the maximum length to read in order to avoid buffer overrun */
while(fscanf(ifp, "%99s", input) != EOF)
{
//Create the node to insert into the tree
curr = malloc(sizeof(node));
if(curr == NULL) /* add error check */
{
perror("malloc 1");
return 1;
}
curr->left = curr->right = NULL;
curr->count = 1;
int p = strlen(input); //get the length of the read string
char *local = malloc(sizeof(char)*(p + 1)); /* make room for terminating null-character */
if (local == NULL) /* add error check again */
{
perror("malloc 2");
return 1;
}
//malloc a dynamic string of only the length needed
//To lowercase, so Job and job is considered the same word
/* using strlen() in loop condition is not a good idea.
* you have already calculated it, so use it. */
for(u = 0; u < p; u++)
{
/* cast to unsigned char in order to avoid undefined behavior
* for passing out-of-range value */
input[u] = tolower((unsigned char)input[u]);
}
strcpy(local, input);
curr->word = local; /* do not use strcpy, just assign */
insert(&root, curr);
}
/* code to free what is allocated will be here */
return 0;
}
//Separate insert function
void insert(node ** tree, node * item)
{
if(!(*tree))
{
*tree = item;
return;
}
if(strcmp(item->word,(*tree)->word) < 0)
insert(&(*tree)->left, item);
else if(strcmp(item->word,(*tree)->word) > 0)
insert(&(*tree)->right, item);
/* note: memory leak may occur if the word read is same as what is previously read */
}
Related
Recently started to practice linked lists. I am aware of the basic algorithm and concept and thought of implementing LL to store a bunch of strings which are input by the user.
But apparently I keep getting Segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct _node{
char *s;
struct _node *next;
}
node;
int main()
{
node *head = NULL;
int a = 0;
char ch;
char *str = malloc(10);
do
{
printf("\nDude %i:", a);
fgets(str, 10, stdin);
node *n = malloc(sizeof(node));
if(n == NULL)
{
printf("\ninsufficient memory");
return 1;
}
if(a == 0)
{
strcpy(n->s, str);
n->next = NULL;
head = n;
}
else
{
strcpy(n->s, str);
n->next = head;
head = n;
}
a++;
printf("\n continue?(y/n): ");
scanf("\n%c", &ch);
}while(ch == 'y');
for(node *temp = head; temp != NULL; temp = temp -> next)
{
printf("\n%s", temp->s);
}
return 0;
}
I do understand that my logic/code is flawed somewhere since I am touching memory I should not touch but cannot seem to point out where since it is my first time dealing with linked lists.
When you are malloc'ing space for the struct, you are only allocating space for the pointer to the string in your _node struct. You need to allocate some memory where you store the string and point the pointer s to it, before you do the strcpy.
i.e.
n->s = malloc(sizeof(char)*100);
Remember that you also need to have a strategy to de-allocate this memory.
As the others hinted, these sort of errors are usually easily caught by looking/debugging with gdb. Remember that it's useful to compile with the -g flag to get useful debugging info.
The reason you catch a ''Segmentation fault' is because you don't allocate memory for s variable of struct node before copying actual string: strcpy(n->s, str).
So, allocate memory for s:
n->s = (char *) malloc(10 * sizeof(char));
Note that you cannot write anything into an unallocated space, so you need to call malloc for the string in each node.
If the string lengths are fixed, then you can specify the length in the definition of struct node to avoid the malloc problem.
Also, it is suggested to always free the objects that will no longer be referenced.
With a few revisions, the codes below may be helpful:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 10
typedef struct node {
char str[LEN];
struct node *next;
} Node;
int main() {
Node *head = NULL;
int n = 0;
char c = 'y';
while (c == 'y') {
Node *node = malloc(sizeof(Node));
printf("Node #%d: ", n);
scanf(" ");
/* Store the string directly into the node. */
fgets(node->str, 10, stdin);
/* Remove the newline character. */
node->str[strcspn(node->str, "\n")] = 0;
node->next = head;
head = node;
++n;
printf("Continue? (y/N): ");
scanf("%c", &c);
};
Node *curr = head;
while (curr != NULL) {
printf("%s\n", curr->str);
Node *temp = curr;
curr = curr->next;
/* Remember to free the memory. */
free(temp);
}
return 0;
}
I'm attempting to tokenize an input file and store its individual words within a linked list organized by word count. I've been struggling with storing the tokenized string into a node, and am struggling to understand what is incorrect in my tokenizing/inserting process. Currently, when printing the stored strings out, the first letter of each string is truncated off, and there is seemingly random garbage and the end of each string. I have tried the following to fix my error:
Null-terminating each string after tokenization (I've left that in
my program as it appears to be correct)
Using strncpy() instead of new_word->str = str;
Passing a pointer to the tokenized string to my insert function,
instead of just passing the string itself.
Below is my code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <strings.h>
typedef struct word{
int length;
char *str;
struct word *left;
struct word *right;
struct word *down;
}word;
void print_list(word **head){
word *temp_traverse = *head;
word *temp_down;
for( ; temp_traverse!=NULL; temp_traverse = temp_traverse->right){
temp_down = temp_traverse;
for( ; temp_down!=NULL; temp_down = temp_down->down){
printf("Count: %d, String: %s\n", temp_down->length, temp_down->str);
}
}
}
int is_empty(word **head, word **tail){
if((*head == NULL)||(*tail == NULL))
return 1;
return 0;
}
void insert(word **head, word **tail, word *new_word){
if(is_empty(head, tail)){
(*head) = new_word;
(*tail) = new_word;
return;
}
if((new_word->length)<((*head)->length)){
new_word->right = (*head);
(*head)->left = new_word;
(*head) = new_word;
return;
}
word *temp = *head;
while(((temp->right)!=NULL) && ((temp->length)<(new_word->length)))
temp = temp->right;
if((temp->length) == (new_word->length)){
while(temp->down != NULL)
temp = temp->down;
temp->down = new_word;
return;
}
if(temp->right == NULL){
word* last = (*tail);
last->right = new_word;
new_word->left = last;
(*tail) = new_word;
return;
}
word* next = temp->right;
temp->right = new_word;
next->left = new_word;
new_word->left = temp;
new_word->right = next;
return;
}
void create(word **head, word **tail, char **str){
word *new_word = (word*)malloc(sizeof(word));
int length = strlen(*str);
if(new_word == NULL){
fprintf(stderr, "Error creating a new word node.\n");
exit(0);
}
new_word->str = (char*)malloc(sizeof(*str));
strncpy(new_word->str, *str, length);
//new_word->str = *str;
new_word->length = length;
printf("%s ", new_word->str); //test print
new_word->left = NULL;
new_word->right = NULL;
new_word->down = NULL;
insert(head, tail, new_word);
return;
}
void tokenize(word **head, word **tail, char words_buffer[]){
char *cur;
cur = strtok(words_buffer, " .,;()\t\r\v\f\n");
*cur++ = '\0';
create(head, tail, &cur);
/* tokenize the next string and reset the "duplicate" variable */
while((cur = strtok(NULL, " .,;()\t\r\v\f\n")) != NULL){
//cur = strtok(NULL, " .,;()\t\r\v\f\n");
*cur++ = '\0';
if(cur){
create(head, tail, &cur);
}
}
}
int main(int argc, char *argv[]){
FILE *fp;
word *head = NULL;
word *tail = NULL;
/*if(argc<3){
printf("Failure: not enough arguments");
return -1;
}*/
fp = fopen(argv[1], "r");
fseek(fp, 0, SEEK_END);
char words_buffer[ftell(fp)+1];
fseek(fp, 0, SEEK_SET);
if(fp==NULL){
printf("Failure: unreadable file");
return -1;
}
while(fgets(words_buffer, sizeof(words_buffer), fp)){
if(strlen(words_buffer)>1)
tokenize(&head, &tail, words_buffer);
}
//print_list(&head);
fclose(fp);
return 0;
}
I've left my test string printing for your reference. You will also notice that I don't use print_list right now, as I have yet to store strings correctly.
Because of the garbage at the end, I am assuming I am either incorrectly using the pointer to the string, or am malloc()ing too much space. As for the truncation, I'm not sure, but I presume it is related to my *cur++ = '\0'; line.
Any help is greatly appreciated, thanks for taking the time to take a look.
You are not copying the whole string with your strncpy().
In fact, you are copying one character too few when you obtain the length with:
int length = strlen(*str);
As stated in the strncpy() manpage:
Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
So make sure that when you use functions operating on null-terminated strings, such as most of the standard library str*() functions, that you account for the '\0' terminator with:
int length = strlen(*str) + 1;
Also, as an aside, the void * returned by malloc() is implicitly converted to any object pointer type, so instead of:
word *new_word = (word*)malloc(sizeof(word));
you should simply use:
word *new_word = malloc(sizeof(word));
or even better:
word *new_word = malloc(sizeof *new_word);
to avoid errors caused by changing the pointer type in the declaration but not the malloc() call.
The sizeof operator doesn't evaluate non-variable-length array expressions, so this is a much more reliable way to obtain an object's size.
EDIT
As to the first character of each string missing, I would assume that is due to:
*cur++ = '\0';
as that just uselessly sets cur[0] to '\0', and then increments the pointer; the string now starts at the second letter of your word.
I'm a beginner in developing, so my sensei gave me a task to complete in which I need to enter a couple of strings in linked lists and after I enter print, they need to be printed in the correct order, from the first to last.
Here is what I got:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char data;
struct Node *next;
}node;
char createlist(node *pointer, char data[100]) {
while (pointer->next != NULL) {
pointer = pointer->next;
}
pointer->next = (node*) malloc(sizeof(node));
pointer = pointer-> next;
pointer->data = *data;
pointer->next = NULL;
}
int main() {
node *first, *temp;
first = (node*) malloc(sizeof(node));
temp = first;
temp->next = NULL;
printf("Enter the lines\n");
while (1) {
char data[100];
gets(data);
createlist(first, data);
if (strcmp(data, "print") == 0)
printf("%s\n", first->data);
else if (strcmp(data, "quit") == 0)
return (0);
};
}
When I run it I get:
Enter the lines:
asdfasdf
print
(null)
Any help would be appreciated since this is my first time using linked lists.
You should format your code properly.
first->data is allocated via malloc() and isn't initialized, so using its value invokes undefined behavior.
In order not to deal the first element specially, you should use pointer to pointer to have createlist() modify first.
Since createlist() won't return anything, type of its return value should be void.
I guess you wanted to copy the strings instead of assigning the first character of each strings.
To print all of what you entered, code to do so have to be written.
You shouldn't use gets(), which has unavoidable risk of buffer overrun.
You should free() whatever you allocated via malloc().
improved code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
char *data;
struct Node *next;
} node;
void createlist(node **pointer, char data[100])
{
while (*pointer != NULL)
{
pointer = &(*pointer)->next;
}
*pointer = malloc(sizeof(node));
if (*pointer == NULL)
{
perror("malloc 1");
exit(1);
}
(*pointer)->data = malloc(strlen(data) + 1);
if ((*pointer)->data == NULL)
{
perror("malloc 2");
exit(1);
}
strcpy((*pointer)->data, data);
(*pointer)->next = NULL;
}
int main(void)
{
node *first = NULL;
printf("Enter the lines\n");
while (1)
{
char data[100], *lf;
if (fgets(data, sizeof(data), stdin) == NULL) strcpy(data, "quit");
if ((lf = strchr(data, '\n')) != NULL) *lf = '\0'; /* remove newline character */
createlist(&first, data);
if (strcmp(data, "print") == 0)
{
node *elem = first;
while (elem != NULL)
{
printf("%s\n", elem -> data);
elem = elem->next;
}
}
else if (strcmp(data, "quit") == 0)
{
while (first != NULL)
{
node *next = first->next;
free(first->data);
free(first);
first = next;
}
return(0);
}
}
}
Inside createlist(), you are iterating to the end of the list. There, you are adding a new node and setting a new text entered. By doing so, you are missing that you have already a first node. Because you are iterating to the end in every call of createlist(), you are jumping over your first node every time, so it remains without text and delivers NULL.
In order not to jump over the first initial node, you could alter createlist() like this:
char createlist(node *pointer, char data[100])
{
while (pointer->data != NULL && pointer->next != NULL)
{
pointer = pointer->next;
}
...
...
}
Or you could create the first node not initially, but only after the first line of text was entered.
edit: Here are two additional style hints:
What happens if somebody enters 120 characters? The text will outrun your char[100] array and will fill RAM that is used otherwise. This is a buffer overflow. You could try to grab only the first 100 chars, get the substring. Alternatively, use the length argument of fgets()
Create a constant for 100, like #define MAX_BUFFER_LENGTH 100, and use it every time.
I'm trying to traverse a tree using the parent, in backwards. using the following code
FIXED CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node { struct node *parent; char *name; };
char *buildPath(node* node)
{
struct node *temp = node;
int length =0;
do{
length+=strlen(temp->name);
temp = temp->parent;
length++; // for a slash
} while(temp !=NULL);
char * buffer = malloc(length+1);
buffer[0] = '\0';
do {
char *name = strdup(node->name);
strrev(name);
strcat(buffer,name);
node = node->parent;
if(node!=NULL)
{
strcat(buffer,"/");
}
free(name);
} while (node != NULL);
strrev(buffer);
return buffer;
}
int main(void)
{
struct node node1 = { NULL, "root" };
struct node node2 = { &node1, "child" };
struct node node3 = { &node2, "grandchild" };
char * result = buildPath(&node3);
printf(result);
return EXIT_SUCCESS;
}
The problem now
I get a crash at free(name);
If I remove it, I get a buffer like "root/child/grandchildþ««««««««ýýýýÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ"with strange symbols
I don't know why it gets a segementation fault while trying to free(name) and to get a correct results, but with weird letters at the end.
I get the following image,
You need space in buffer for a string-terminating zero; malloc(length + 1).
(Never cast the result of malloc.)
You need space in buffer for the path separators.
Add them up as you compute length.
strcat expects a zero-terminated parameter to concatenate to; do buffer[0] = '\0'; first.
I am creating a binary tree from a bitstring in c. ie 1100100 creates a tree:
1
/ \
1 1
I decided to use a recursive function to build this tree however i keep getting the error
Debug assertion failed...
Expression : CrtIsValidHeapPointer(pUserData)
here is a fragment of my code
typedef
struct Node {
char key;
struct Node *left;
struct Node *right;
} Node;
char string[1000];
int i = 0;
void insertRecursivePreorder(Node **node)
{
Node* parent = *node;
if(string[i] == '0')
{
parent = NULL;
i++;
}
else
{
Node *newn = (Node*)malloc(sizeof(Node));
newn->key = string[i];
parent = newn;
i++;
insertRecursivePreorder(&newn->left); //errors occur here
insertRecursivePreorder(&newn->right); //errors occur here
free(newn);
free(parent);
}
}
int main(void)
{
void printTree(Node* node);
Node* root = NULL;
scanf("%s", string);
insertRecursivePreorder(&root);
//... do other junk
}
i was wondering why this error comes about and what i can do to fix it.
The immediate problem is likely to be calling free on a pointer twice. In insertRecursivePreorder, you set parent to newn, and then call free on both. As an example of this, the following program fails (but works if you comment out one of the free(..)s):
#include <stdlib.h>
int main() {
int *a = malloc(sizeof(int)),
*b = a;
free(a);
free(b);
return 0;
}
However, there are several problems with your logic here. You should only call free when you have completely finished with the pointer, so if you are using your tree later you can't free it as you construct it. You should create a second function, recursiveDestroyTree, that goes through and calls free on the tree (from the bottom up!).
And, you probably want *node = newn rather than parent = newn, since the latter is the only one that actually modifies node.
(You could also change your function to return a Node * pointer, and then just go:
root = insertRecursivePreorder();
and
newn->left = insertRecursivePreorder();
newn->right = insertRecursivePreorder();
instead of trying to keep track of pointers to pointers etc.)
(Furthermore, on a stylistic point, using global variables is often bad practice, so you could have your insertRecursivePreorder take int i and char * string parameters and use them instead of global variables.)
The problem was: you were never assigning to the double pointer in 'insertRecursivePreorder', so root always stayed NULL.
#include <stdio.h>
#include <stdlib.h>
typedef
struct Node {
char key;
struct Node *left;
struct Node *right;
} Node;
/* slightly changed the syntax for the str
** ; now '.' indicates a NULL pointer, values represent themselves.
*/
char *string = "12..3.." ;
/* Removed the global index 'i' */
void printTree(Node* node, int level);
unsigned insertRecursivePreorder(Node **pp, char *str);
unsigned insertRecursivePreorder(Node **pp, char *str)
{
unsigned pos =1;
if (!*str) { *pp = NULL; return 0; } /* safeguard for end of string */
if (*str == '.') { *pp = NULL; return pos; }
*pp = malloc(sizeof **pp);
(*pp)->key = *str;
pos += insertRecursivePreorder(&(*pp)->left, str+pos);
pos += insertRecursivePreorder(&(*pp)->right, str+pos);
return pos;
}
void printTree(Node* node, int level)
{
unsigned pos,len;
len = level> 0 ? level : -level;
for (pos =0; pos < len; pos++) putchar (' ');
if (!level) printf ("Root=");
else if (level<0) printf ("Left=");
else printf ("Right=");
if (!node) { printf( "Null\n" ); return; }
printf("Key=%c\n", node->key );
printTree(node->left, -(len+1) ) ;
printTree(node->right, len+1) ;
}
int main(void)
{
Node *root = NULL;
unsigned result = 0;
result = insertRecursivePreorder(&root, string);
printf( "Result=%u\n", result);
printTree(root, 0);
return 0; printTree(root, 0);
}
Output:
Result=7
Root=Key=1
Left=Key=2
Left=Null
Right=Null
Right=Key=3
Left=Null
Right=Null