How can I get rid of memory leaks from for example this function:
void AddData(Data **head, char *sentence, int number) {
Words *words = NULL;
char delimiters[] = " \n\0";
char *token = strtok(sentence, delimiters);
while (token != NULL) {
if (IsAlphabetical(token) == 1) {
char *string = (char *)malloc((strlen(token) + 1) * sizeof(char));
strcpy(string, token);
AddWords(&words, string);
free(string);
}
token = strtok(NULL, delimiters);
}
Data *temp = *head;
Data *newData = (Data *)malloc(sizeof(Data));
newData->lineNumber = number;
newData->words = words;
newData->pNext = NULL;
if (*head == NULL)
*head = newData;
else {
while (temp->pNext != NULL)
temp = temp->pNext;
temp->pNext = newData;
}
}
My personal opinion is the leaks appear because of newData, temp and words variables.
I have a few similar functions that cause the same problem.
I have also function for deleting Data struct, but when I call it at the end of previous function in such way DeleteData(&temp) program will not execute. I think it's because my whole list is deleted.
void DeleteData(Data **head) {
Data *temp = *head;
while (temp->pNext != NULL) {
Data *next = temp->pNext;
DeleteWords(&temp->words);
free(temp);
temp = next;
}
free(temp); /* to check that */
*head = NULL;
}
How can I fix this?
Here are some problems I identified:
The trailing \0 in char delimiters[] = " \n\0"; is useless.
Testing if (IsAlphabetical(token) == 1) might be too restrictive. In C anything non 0 is true so you might test if (IsAlphabetical(token) != 0) or just if (IsAlphabetical(token)).
Why allocate a copy of the string to pass to AddWords(&words, string); and then free(string) thereafter? If Addwords() does not keep the pointer it gets, there is no need to allocate a copy.
Does AddWords() call strtok()? strtok() is non-reentrant, which means that if AddWords() or any other function called in this loop, such as IsAlphabetical() calls strtok(), the context used in the loop will be corrupted. You should use strtok_r() instead.
in function DeleteData, why do you iterate testing while (temp->pNext != NULL)? The last item in the list does not get freed properly, the DeleteWords(&temp->words); is not called. This may cause a memory leak. You should just write:
void DeleteData(Data **head) {
Data *temp = *head;
while (temp != NULL) {
Data *next = temp->pNext;
DeleteWords(&temp->words);
free(temp);
temp = next;
}
*head = NULL;
}
Related
I am required to have a list of structs of sentence nodes that point to a struct of word nodes. I am trying to print the user's input.
I have a program that runs properly when I manually give it the input (see test section of the code). It does not, however, work when I use my input1() function.
I've tried debugging it, but I can't seem to find the problem.
I removed all printf lines that I used to debug. I also removed all the irrelevant code.
I am looking to know how to fix it and what is wrong so I can run it with no problems.
What I learned from debugging it is that (only when using input1() and not in the test) the head is overwritten every time and all the nodes as well.
I also tried using a double pointer instead of returning para but that didn't help.
any help will be appreciated,
thanks in advance
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct word
{
char * ch;//poiter to char
}
W;
typedef struct sentence
{
W * currentWord;//pointer to a word
int lineNumber;// holds the line number
int numbersOfWords;//holds the number of words
struct sentence* link;
}
sent;
typedef struct list
{
sent* head;
int count;
}
LISTS;
LISTS* createList()
{
LISTS* list;
list= (LISTS*) malloc (sizeof (LISTS));
if (list)
{
list-> head = NULL;
list-> count = 0;
}
return list;
} // createList
void printList(LISTS* list)
{
sent *temp = list -> head;
//iterate the entire linked list and print the data
while(temp != NULL)
{
printf("%s\n", temp->currentWord->ch);
temp = temp->link;
}
// printf("NULL\n");
}
void insertSentList (LISTS* list, W* itemPtr)
{
sent* newPtr; //new node
if (!(newPtr = (sent * ) malloc(sizeof(sent)))){
printf(" Memory can not be allocated.");
return;
}
newPtr->currentWord = itemPtr;
newPtr->link = NULL;
if(list->head == NULL)
{
list->head = newPtr;
}else{
sent* current = list->head;
while(current->link != NULL){
current = current->link;
}
current -> link = newPtr;
}
(list->count)++;
return;
} // insertList
LISTS * input1(LISTS *para)
{
char * line;
line = (char * ) malloc(1000 * sizeof(char));
line[0] = '\0';
while (line[0] != '\n')
{
W word;
word.ch = (char * ) malloc(100);
printf(" Please input a line : ");
fgets(line, 1000, stdin);
if(line[0] != '\n'){
strcpy(word.ch, line);
insertSentList(para,&word);
}
}
free(line);
return para;
}
int main()
{
///////////////////test////////////////
LISTS* list = createList();
W word;
word.ch= "word0 ";
W word1;
word1.ch= "word1 ";
W word2;
word2.ch= "word2";
insertSentList(list,&word);
insertSentList(list,&word1);
insertSentList(list,&word2);
insertSentList(list,&word);
insertSentList(list,&word1);
insertSentList(list,&word2);
printList(list);
///////////////////test////////////////
LISTS *para = createList();
para= input1(para);
printList(para);
return 0;
}
Main problem with the posted code is that "ownership" of the sent and W objects in a list is not well defined. For example word.ch= "word0 "; in main sets the ch pointer pointing to a string literal (which it does not own), but word.ch = malloc(100); in input1 points it to dynamically allocated memory (which it should own, and remember to free later). Because of this, memory allocations cannot be tracked reliably and, even in the cases where things appear to "work", there are multiple memory leaks. It also breaks when the inserted objects are local variables that do not live for the entire lifetime of the list object.
The simplest (if not necessarily the best or most efficient) solution would be to dynamically allocate all objects that go into the list, make the list own them all, and add a function to cleanup once done. To that end insertSentList could be modified as follows.
void insertSentList (LISTS* list, W* itemPtr)
{
sent* newPtr; //new node
if (!(newPtr = malloc(sizeof(sent)))){
printf(" Memory can not be allocated.\n");
return;
}
W *newItem = malloc(sizeof(W)); // <-- make a deep copy of the `itemPtr` argument
newItem->ch = strdup(itemPtr->ch); // including a copy of the string itself
newPtr->currentWord = newItem; // <-- save the copy in the list, not the argument
newPtr->link = NULL;
if(list->head == NULL)
{
list->head = newPtr;
}else{
sent* current = list->head;
while(current->link != NULL){
current = current->link;
}
current->link = newPtr;
}
list->count++;
} // insertList
For proper cleanup and to avoid memory leaks, the following freeList should be called for each list pointer returned by createList and filled by insertSentList.
void freeList(LISTS *list)
{
sent *temp = list->head;
while(temp != NULL)
{
sent *next = temp->link;
free(temp->currentWord->ch);
free(temp->currentWord);
free(temp);
temp = next;
}
free(list);
}
I am writting a program on doubly linked list with the following data structure:
typedef struct telephoneBookNode {
int id;
char name[NAME_LENGTH];
char telephone[TELEPHONE_LENGTH];
struct telephoneBookNode * previousNode;
struct telephoneBookNode * nextNode;
} TelephoneBookNode;
typedef struct telephoneBookList {
TelephoneBookNode * head;
TelephoneBookNode * tail;
TelephoneBookNode * current;
} TelephoneBookList;
In the following function, I read data from a text file to the linked list, the file content look like this:
/*100, Alice, 0411112222
101, Bob, 0411112222
102, Ali, 0411112223*/
TelephoneBookList * commandLoad(char* fileName) {
TelephoneBookList *(*createList)(TelephoneBookNode*, char[]) = createTelephoneBookList;
char entry[100], *temp1, *temp2;
TelephoneBookList* aList = NULL;
TelephoneBookNode* aNode = NULL;
FILE* telephoneListFile = NULL;
int countEntry = 0;
Boolean check;
telephoneListFile = fopen(fileName, "r");
if (!telephoneListFile)
return NULL;
else {
while (fgets(entry, 100, telephoneListFile)) {
temp2 = strcpy(temp2, entry);
temp1 = strtok(entry, "\n");
check = addressBookEntryCheck(temp1);
if (!check)
return NULL;
else
//here I pass aNode pointer to the below function
aList = (*createList)(aNode, temp2);
}
fclose(telephoneListFile);
printf("printed"); //This line is reached when program complied
return aList;
}
}
This is the function to create the list, problem may be here: it doesnot add new node to the list, it just replaces the first node with the new one. Finally, the linked list only has 1 record which was the last one in text file. How can I fix the code? Thank you!
TelephoneBookList * createTelephoneBookList(TelephoneBookNode* node, char entry[]) {
TelephoneBookList* aList = malloc(sizeof *aList);
TelephoneBookNode* aNode = (TelephoneBookNode*) malloc(sizeof *aNode);
char *tokens;
tokens = strtok(entry, ", ");
aNode->id = atoi(tokens);
tokens = strtok(NULL, ", ");
strcpy(aNode->name, tokens);
tokens = strtok(NULL, ", ");
strcpy(aNode->telephone, tokens); //Just assigning values to a node
//program always go to this block, means `node` is always null
if (node == NULL) {
aNode->nextNode = NULL;
aNode->previousNode = NULL;
node = aNode;
aList->current = node;
aList->head = node;
aList->tail = node;
}
else { //This block is not reached
while (node->nextNode)
node = node->nextNode;
node->nextNode = aNode;
aNode->previousNode = node;
aList->tail = node->nextNode;
}
return aList;
}
This is the function to check entry:
Boolean addressBookEntryCheck(char entry[]) {
char *tokens;
tokens = strtok(entry, ", ");
if(!tokens || strlen(tokens) < 1 || strlen(tokens) > 3)
return FALSE;
else {
if (!isNumber(tokens))
return FALSE;
else {
tokens = strtok(NULL, ", ");
if (!tokens)
return FALSE;
else
{
tokens = strtok(NULL, ", ");
if (!tokens)
return FALSE;
else if (!isNumber(tokens) || strlen(tokens) != 10)
return FALSE;
else
return TRUE;
}
}
}
}
Every time you call
createTelephoneBookList
you create a new list
TelephoneBookList* aList = malloc(sizeof *aList);
You also copy to an uninitialized pointer
temp2 = strcpy(temp2, entry);
I would suggest you create one function to create the list header, one function to add new items e.g.
aList = createList()
while (fgets(entry,sizeof(entry),fp)!=NULL)
{
if (!addEntry(aList,entry))
{
fprintf(stderr, "failed additem item %s\n", entry);
}
}
...
In addEntry parse the string
int id = 0;
char name[NAME_LENGTH];
char telephone[TELEPHONE_LENGTH];
p = strtok(entry, ","); // id
if (p != NULL)
{
id = atoi(p);
p = strtok(NULL, ","); // name, store to temporary string
if (p != NULL )
{
strcpy(name,p);
p = strtok(NULL, ","); // telephone number, store to temporary string
if ( p != NULL )
{
strcpy(telephone,p);
// here you can allocate the new node
}
}
}
// disclaimer omitted checks for length etc which any good program should have. also make sure you have room for \0
if any of the strtok above fail return 0 otherwise allocate a new entry
TelephoneBookNode* aNode = malloc(sizeof(TelephoneBookNode));
aNode->id = id;
strcpy(aNode->name, name);
strcpy(aNode->telephone, telephone);
Then add to your aList
//program always go to this block, means `node` is always null
if (node == NULL) {
....
This is because the caller of the function passed aNode and it never changes within that loop. So it will always pass same value of aNode which is NULL.
I haven't looked at the logic of your code in details, but I think you may want to pass aList->head or rather you already pass aList so just use that.
I'm trying to read line input from a file, correctly parse the line, and add the three fields of information from the line onto a node in a linked list.
Here's my read from file function:
int readFile(char* file)
{
FILE *fp = fopen(file,"r");
char ch;
char line[50];
char string1[100];
char string2[100];
char string3[100];
char endLine[2];
int i = 0;
while(fscanf(fp, "%[^\t]\t%[^\t]\t%[^\n]", string1, string2, string3) == 3)
{
printf( "%s\t%s\t%s\n", string1, string2, string3);
addNode(string1, string2, string3, head, tail);
}
printNodes();
fclose(fp);
return 0;
}
And here is my addNode function:
// create stuff
Entry *entry = malloc(sizeof(Entry));
entry->name = string1;
entry->address = string2;
entry->number = string3;
Node* node = malloc(sizeof(Node));
node->entry = entry;
node->next = NULL;
// Empty list
if(head->next == NULL)
{
head->next = node;
}
// Else, add to the end of the list
else
{
Node* temp = head->next;
while(temp->next!= NULL)
{
temp = temp->next;
}
temp->next = node;
}
I get problems when I call printNodes, and only the last read node's information is printed X times, where X is the number of unique nodes I'm supposed to have. I think I'm having a problem where I'm overwriting an old node each time I create a new node, but I'm not entirely sure, as this is my first time with raw C code.
Thanks again!
EDIT:
here's the printNodes() function:
int printNodes(Node* head)
{
Node *temp = head->next;
while(temp->next != NULL)
{
printf("\n%s\t%s\t%s\n", temp->entry->name, temp->entry->address, temp->entry->number);
temp = temp->next;
}
return 0;
}
Your problem is here:
entry->name = string1;
entry->address = string2;
entry->number = string3;
You are providing the same memory location to every node. Those strings contain the last value you read in when you call printNodes().
I am writing a program that will read in two files (hw8.data and codex.data) The hw8.data contains a poem and the codex.data contains words to replace in that poem. I have gotten the files to be loaded and saved in a linked list. I am having trouble finding matched words and replacing them. Also I am having trouble carrying over punctuation from the hw8 on to the newly replaced word for example. hw8: Hello World., codex: World sup, so new word in poem would be Hello sup.
Here is the hw8.data
Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.
The Codex.data (not all of it)
Eye I
eye I
chequer checker
Pea P
Sea C
plane plainly
lee skip
four for
revue review
Miss Mistakes
Steaks skip
knot not
sea see
quays keys
.
.
.
Here is my code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char * word;
struct node* next;
};
struct codex {
char *word1;
char *word2;
struct codex *next;
};
struct node *loadWords(FILE *stream);
struct codex *loadCodex(FILE *stream);
struct node *exchange(struct node *head, struct codex *headC);
int main()
{
FILE *stream = NULL;
struct node *head;
struct node *temp;
struct node *loop;
struct codex *headC;
struct codex *tempC;
struct codex *loopC;
head = loadWords(stream);
if (head == NULL) {
return 1;
}
temp = head;//print each word
while (temp != NULL) {
//printf ( "%s\n", temp->word);
temp = temp->next;
}
headC = loadCodex(stream);
if (headC == NULL) {
return 1;
}
tempC = headC;//print each word
while (tempC != NULL) {
//printf ( "%s %s\n", tempC->word1, tempC->word2);
tempC = tempC->next;
}
struct node *exchangedHead;
struct node *temp2;
exchangedHead = exchange(head, headC);
if (exchangedHead == NULL) {
return 1;
}
temp2 = exchangedHead;//print each word
while (temp2 != NULL) {
//printf ( "%s ", temp2->word);
temp2 = temp2->next;
}
temp = head;// free memory
while(temp != NULL) {
loop = temp->next;
free ( temp->word);
free ( temp);
temp = loop;
}
tempC = headC;// free memory
while (tempC != NULL) {
loopC = tempC->next;
free ( tempC->word1);
free ( tempC->word2);
free ( tempC);
tempC = loopC;
}
return 0;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct node *loadWords(FILE *stream) {
struct node *loop = NULL;
struct node *temp = NULL;
struct node *head = NULL;
char *words = NULL;
char *currentWord;
size_t chrCount = 0;
if ((stream = fopen("hw8.data", "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while(getline( &words, &chrCount, stream) > 0) {//read a line from file
currentWord = strtok(words, " ");//get first token
while (currentWord != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word = strdup ( currentWord);//allocate memory and copy token to word
if ( head == NULL) {
head = temp;//first structure
}
else {
loop = head;
while ( loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = temp;
}
currentWord = strtok(NULL, " ");//next token
}
free (words);//release memory
chrCount = 0;//so readline will allocate memory for next line
words = NULL;
}
return head;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct codex *loadCodex(FILE *stream) {
struct codex *loop = NULL;
struct codex *temp = NULL;
struct codex *head = NULL;
char *words = NULL;
char *currentWord;
char *currentWord2;
size_t chrCount = 0;
if (( stream = fopen("codex.data", "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while(getline( &words, &chrCount, stream) > 0) {//read a line from file
currentWord = strtok(words, " ");//get first token
currentWord2 = strtok(NULL, "\n");
while (currentWord != NULL && currentWord2 != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word1 = strdup ( currentWord);//allocate memory and copy token to word
temp->word2 = strdup ( currentWord2);
if (head == NULL) {
head = temp;//first structure
}
else {
loop = head;
while ( loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = temp;
}
currentWord = strtok(NULL, " ");//next token
}
free (words);//release memory
chrCount = 0;//so readline will allocate memory for next line
words = NULL;
}
return head;
}
struct node *exchange(struct node *head, struct codex *headC) {
struct node *temp;
temp = head;
while(head != NULL && headC != NULL) {
if(strcmp(head->word, headC->word1)) {
head->word = relloc(head->word, strlen(headC->word2));
head->word = headC->word2;
head->next = temp->next;
headC = headC->next;
temp = temp->next;
}
}
}
The issue you are having in exchange (other than misspelling realloc and failing to return a value) is failing to allocate space for the null-terminating character on realloc. (note I haven't checked the list operations for correctness)
head->word = realloc(head->word, strlen(headC->word2));
should be
head->word = realloc(head->word, strlen(headC->word2) + 1);
Which would explain why you are losing the 1-char punctuation off the end of each line.
You should also realloc to a tmp pointer and only assign head->word = tmp; if the reallocation succeeded. (otherwise you lose the original contents of head->word) Also, if you are not going to return a value from exchange, just make the function void (otherwise return head). Let me know if this doesn't correct the issue and I'll dig deeper.
More Issues
Looking at the code, I saw several more issues (some significant). Primarily, your exchange function was a train wreck. Generally, when iterating a linked list, you create a temporary pointer to iterate, rather than using the list address (as you did in main). Why didn't you do that in exchange? Given that, I scrapped exchange and rewrote it. It is included below. In your read functions, wise choice in use of getline, but there is no need to reset chrCount = 0; or words = NULL; in order for getline to allocate the next line. It does it automatically.
However, you committed a foul when you sent the line words to strtok -- without first making a copy. Why? getline allocated the space and needs to manage/free the block of memory it allocated. When you send words to strtok it modifies the memory, inserting null-terminating characters which can lead to stray errors when getline attempts to free/realloc the block. Always make a copy of the line allocated by getline before sending the line to strtok. Additionally, since you must free the copy, create a pointer to your copy so you preserve the start address for your memory block in order to pass to free when you are done.
There is no need to pass an unopened FILE* pointer to your read functions. That can be declared within the function. What you do need to pass is a filename so your read functions to know what to open. I changed main to include its arguments so you now pass the words/codex filenames as arguments.
Note: I just used your sample codex values for my test.
Look over the code at the modifications and the comments:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
char * word;
struct node* next;
};
struct codex {
char *word1;
char *word2;
struct codex *next;
};
struct node *loadWords(char *filename);
struct codex *loadCodex(char *filename);
struct node *exchange(struct node *head, struct codex *headC);
int main (int argc, char **argv)
{
if (argc < 3) {
fprintf (stderr, "error: insufficient input. Usage: %s nodefile codexfile\n", argv[0]);
return 1;
}
// FILE *stream = NULL;
struct node *head;
struct node *temp;
struct node *loop;
struct codex *headC;
struct codex *tempC;
struct codex *loopC;
head = loadWords(argv[1]);
if (head == NULL) {
return 1;
}
printf ("\nOriginal:\n\n");
temp = head;//print each word
while (temp != NULL) {
printf ( " %s", temp->word);
temp = temp->next;
}
printf ("\n");
headC = loadCodex(argv[2]);
if (headC == NULL) {
return 1;
}
tempC = headC;//print each word
while (tempC != NULL) {
// printf ( "%s %s\n", tempC->word1, tempC->word2);
tempC = tempC->next;
}
struct node *exchangedHead;
struct node *temp2;
exchangedHead = exchange(head, headC);
if (exchangedHead == NULL) {
return 1;
}
printf ("\nExchanged:\n\n");
temp2 = exchangedHead;//print each word
while (temp2 != NULL) {
printf ( " %s", temp2->word);
temp2 = temp2->next;
}
printf ("\n");
temp = head;// free memory
while(temp != NULL) {
loop = temp->next;
free ( temp->word);
free ( temp);
temp = loop;
}
tempC = headC;// free memory
while (tempC != NULL) {
loopC = tempC->next;
free ( tempC->word1);
free ( tempC->word2);
free ( tempC);
tempC = loopC;
}
return 0;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct node *loadWords(char *filename) {
FILE *stream = NULL;
struct node *loop = NULL;
struct node *temp = NULL;
struct node *head = NULL;
char *words = NULL;
char *currentWord;
size_t chrCount = 0;
ssize_t nchr = 0;
if ((stream = fopen(filename, "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while((nchr = getline (&words, &chrCount, stream)) != -1) {//read a line from file
char *wtemp = strdup (words); /* copy words, strtok will modify */
char *sp = wtemp; /* save wtemp address, pass sp */
currentWord = strtok(sp, " ");//get first token
while (currentWord != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word = strdup (currentWord);//allocate memory and copy token to word
if ( head == NULL) {
head = temp;//first structure
}
else {
loop = head;
while ( loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = temp;
}
currentWord = strtok(NULL, " ");//next token
}
if (wtemp) free (wtemp);
wtemp = NULL;
// chrCount = 0; //so readline will allocate memory for next line (***NOT REQUIED***)
// words = NULL; (***NOT REQUIED***)
}
if (words) free (words);//release memory
if (stream) fclose (stream);
return head;
}
/*
* This function will go through and load the words from the text file and save it to
* the struct and link the current struct to the next one
*/
struct codex *loadCodex(char *filename) {
FILE *stream = NULL;
struct codex *loop = NULL;
struct codex *temp = NULL;
struct codex *head = NULL;
char *words = NULL;
char *currentWord;
char *currentWord2;
size_t chrCount = 0;
ssize_t nchr = 0;
if (( stream = fopen(filename, "r")) == NULL) {
printf ("could not open file\n");
return NULL;
}
while(getline( &words, &chrCount, stream) > 0) {//read a line from file
while (nchr > 0 && (words[nchr-1] == '\n' || words[nchr-1] == '\r'))
words[--nchr] = 0; /* strip newline or carriage rtn */
char *wtemp = strdup (words); /* copy words, strtok will modify */
char *sp = wtemp; /* save wtemp address, pass sp */
currentWord = strtok(sp, " ");//get first token
currentWord2 = strtok(NULL, "\n");
while (currentWord != NULL && currentWord2 != NULL) {//loop through tokens
if((temp = calloc(1, sizeof(struct node))) == NULL) {
printf("ERROR - Could not allocate memory.\n");
exit(0);
}
temp->word1 = strdup ( currentWord);//allocate memory and copy token to word
temp->word2 = strdup ( currentWord2);
if (head == NULL) {
head = temp;//first structure
}
else {
loop = head;
while ( loop->next != NULL) {//loop to last structure
loop = loop->next;//add structure to end
}
loop->next = temp;
}
currentWord = strtok(NULL, " ");//next token
}
if (wtemp) free (wtemp);
wtemp = NULL;
// chrCount = 0;//so readline will allocate memory for next line
// words = NULL;
}
if (words) free (words);//release memory
if (stream) fclose (stream);
return head;
}
// struct node *exchange(struct node *head, struct codex *headC) {
// struct node *temp;
// temp = head;
// while(head != NULL && headC != NULL) {
// if(strcmp(head->word, headC->word1)) {
// head->word = realloc(head->word, strlen(headC->word2));
// head->word = headC->word2;
// head->next = temp->next;
// headC = headC->next;
// temp = temp->next;
// }
// }
// }
struct node *exchange(struct node *head, struct codex *headC)
{
struct node *temp = head;
while(temp != NULL)
{
char *wtemp = NULL;
struct codex *tempC = headC;
while (tempC != NULL)
{
if (strcmp(temp->word, tempC->word1) == 0) {
size_t tclen = strlen(tempC->word2);
wtemp = realloc(temp->word, tclen + 1); /* add 1 for null-term */
if (!wtemp) {
fprintf (stderr, "%s() error: realloc failed.\n", __func__);
return head;
}
temp->word = wtemp;
memcpy (temp->word, tempC->word2, tclen + 1); /* copies the null-term also */
}
tempC = tempC->next;
}
temp = temp->next;
}
return head;
}
Output
$ ./bin/ll_single_replace_w dat/ll_replace_poem.txt dat/ll_replace_codex.txt
Original:
Eye have a spelling chequer,
It came with my Pea Sea.
It plane lee marks four my revue,
Miss Steaks I can knot sea.
Eye strike the quays and type a whirred,
And weight four it two say,
Weather eye am write oar wrong,
It tells me straight aweigh.
Eye ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My chequer tolled me sew.
A chequer is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when eye rime.
Each frays come posed up on my screen,
Eye trussed too bee a joule.
The chequer pours over every word,
Two cheque sum spelling rule.
Exchanged:
I have a spelling chequer,
It came with my P Sea.
It plainly skip marks for my revue,
Mistakes skip I can not sea.
I strike the keys and type a whirred,
And weight for it two say,
Weather I am write oar wrong,
It tells me straight aweigh.
I ran this poem threw it,
Your shore real glad two no.
Its vary polished in its weigh.
My checker tolled me sew.
A checker is a bless thing,
It freeze yew lodes of thyme.
It helps me right all stiles of righting,
And aides me when I rime.
Each frays come posed up on my screen,
I trussed too bee a joule.
The checker pours over every word,
Two cheque sum spelling rule.
I am trying to get user input from fgets() and save the entry into a linked list but it isn't saving the entry into the linked lists, but if I directly put it in the call, it does. ex. add_to_list(&list, "hello"); How do use fgets to save into a character array (called word), which I can stick into add_to_list call?
void
add_to_list(struct linked_list *list, char *x)
{
struct node *n = malloc(sizeof *n);
n->data = x;
n->next = NULL;
if (list->head == NULL)
list->head = n;
if (list->tail != NULL)
list->tail->next = n;
list->tail = n;
}
int
main(void)
{
struct linked_list list = { .head = NULL, .tail = NULL };
char word[50];
do {
printf("Enter string: ");
fgets(word, 50, stdin)
add_to_list(&list, word);
} while (word[0] != '\n');
//add_to_list(&list, "hello");
print_list_rec(&list);
free_list(&list);
return 0;
}
Here is the problem:
n->data = x;
This statement assigns the pointer to the buffer to the data pointer of the list. However, this is the same pointer in all calls. Moreover, the buffer is in the automatic storage, because word is a local variable.
In order to fix this problem you need to add copying of the string into dynamically allocated buffers. Use strlen to decide how much memory you need, strcpy to copy the data, and don't forget to free the string after you are done with the struct.
size_t len = strlen(x);
n->data = malloc(len+1);
strcpy(n->data, x);
This goes inside free_list:
free(n->data);
free(n);