I am new to c and have been stuck on this bug for hours. My code reads each word from a txt file and then stores the word in a node in a trie.
I have narrowed the problem down to the area marked off by asterisks:
at that point I have successfully added the first word to the trie and check that the correct node's value matched the word. Then, I use fscanf to save the next word in the file as the char 'add'. Then, printing out the exact same thing as before, the node's word should have remained the same. However, it has somehow been changed to the new word that was just read from the file.
How is this even possible??
int main(int argc, char** argv) {
trie_t* trie = malloc(sizeof(trie_t));
trie_init(trie);
int ret;
char* add = malloc(128);
FILE* file = fopen(argv[5], "r");
if (file == NULL) {
/* Failed to open the file for reading */
return 0;
}
while (1) {*********************
if (trie->head->children != NULL) {
printf("%s\n", trie->head->children->word);
}
ret = fscanf(file, "%s", add);
//printf("word = %s\n",toAdd);
if (trie->head->children != NULL) {
printf("%s\n", trie->head->children->word);
}****************************
if (ret == EOF) {
/* End of file */
ret = 1;
break;
} else if (ret <= 0) {
printf("fails");
/* Failed to read a word from the file */
break;
} else {
printf("gets here\n");
/* Succesfully read a word */
int x = trie_add(trie, add);
printf("%d\n",x);
}
}
You are only assigning memory once for add at:
char* add = malloc(128);
You need to assign memory for each word, that is, you need to move the malloc to your read cycle.
What the code is doing as you posted it is: allocate 128 bytes once, and then overwrite that memory space once and again every time you scanf().
Also, at char* add = malloc(128); you should assign it as char* add = malloc(128 * sizeof(char)); just to be clear and portable :)
I assume you are storing the pointer add in trie_t in trie_add function. In this case, since you are reusing the same memory location add for reading the next string, the content of the pointer pointed by add changes. Since you are just storing this pointer in trie the content of that node also changes because of this. To solve this, you need to allocate memory again using malloc just before the new string is read from file using fscanf.
Related
I have a problem with a memory leak with temp_line.
This function read a whole text from a file and assign each word to a big linked list. I cant figure out who's the owner of temp_line whenever I exit this function, and whenever I am trying to replace temp_line with non-dynamic variable (like temp_line[1000]) every time I get a new line, its overwritten the data from the old line (and then I am getting a segmentation fault because of that).
So I really don't know how to solve it.
int fill(FILE *f, LinkedList *linkedlist) {
FILE *file = fopen((const char *)f, "r");
char line[MAX_LINE]; //MAX_LINE = 1000
while (fgets(line, MAX_LINE, file) != NULL) {
char *temp_line = malloc(MAX_LINE);
if (temp_line == NULL) {
fclose(file);
return false;
}
strcpy(temp_line, line);
read_line(linkedlist, temp_line);
}
fclose(file);
return true;
}
int read_line(LinkedList *linkedlist, char *line) {
char *word;
while (true) {
word = strtok(NULL, "\n ");
if (word == NULL) {
break;
}
add_node(linkedlist, word);
}
return true;
}
add_node adds the node to the linked list and returns the node.
There are multiple problems in the code:
FILE *file = fopen((const char *)f, "r"); with f defined as FILE *f is probably incorrect. If you are given a FILE *, just read from it.
strtok should be first called with the string, then with NULL until it returns NULL.
you insert nodes into the list from pointers into the middle of the block allocated by read_line. There is no way to determine the beginning of such a block, nor any way to determine how many pointers point into the block. Memory allocated and used this way cannot be freed. You should instead use strdup() to allocate individual copies of the words. These pointers can be later freed with free() in the del_node() function. Alternately, add_node() could make a copy of the string argument, which would be consistent with del_node() freeing this data.
regarding your question: Cannot find who is the owner of a malloc block... indeed it is the C programmers' responsibility to keep track of allocated memory. There is no way to test if a pointer is valid, not whether it points to an allocated block. You must design the program consistently so memory ownership can always be determined from context.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int read_line(LinkedList *linkedlist, char *line) {
char *word;
while ((word = strtok(line, "\n ")) != NULL) {
add_node(linkedlist, strdup(word));
line = NULL;
}
return true;
}
int fill(FILE *file, LinkedList *linkedlist) {
char line[MAX_LINE];
while (fgets(line, sizeof line, file) != NULL) {
read_line(linkedlist, line);
}
return true;
}
I am trying to replace temp_line with non-dynamic variable (like temp_line[1000])
temp_line[1000] is also called "automatic memory" because it is automatically deallocated at the end of its scope. What this means is temp_line would be deallocated on each iteration of the loop.
Perhaps the important thing to realize is strtok does not allocate any memory. word points at memory inside temp_line.
Let's say you did.
while (fgets (line, MAX_LINE, file) != NULL) {
char temp_line[MAX_LINE];
strcpy(temp_line, line);
read_line(linkedlist, temp_line);
}
On each loop temp_line will be allocated, passed into read_line, and then deallocated. The words you add to the linked list point at deallocated memory.
The question is who "owns" memory. Because it uses strtok, read_line assumes that it owns line and that line will not be deallocated. The safe thing to do is to change read_line so it copies each word from line.
int read_line(LinkedList *linkedlist, char *line) {
for(
char *word = strtok(line, "\n ");
word;
word = strtok(NULL, "\n ")
) {
// Duplicate the word so it no longer refers to line.
add_node(linkedlist, strdup(word));
}
return true;
}
This code reads a text file line by line. But I need to put those lines in an array but I wasn't able to do it. Now I am getting a array of numbers somehow. So how to read the file into a list. I tried using 2 dimensional list but this doesn't work as well.
I am new to C. I am mostly using Python but now I want to check if C is faster or not for a task.
#include <stdio.h>
#include <time.h>
#include <string.h>
void loadlist(char *ptext) {
char filename[] = "Z://list.txt";
char myline[200];
FILE * pfile;
pfile = fopen (filename, "r" );
char larray[100000];
int i = 0;
while (!feof(pfile)) {
fgets(myline,200,pfile);
larray[i]= myline;
//strcpy(larray[i],myline);
i++;
//printf(myline);
}
fclose(pfile);
printf("%s \n %d \n %d \n ","while doneqa",i,strlen(larray));
printf("First larray element is: %d \n",larray[0]);
/* for loop execution */
//for( i = 10; i < 20; i = i + 1 ){
// printf(larray[i]);
//}
}
int main ()
{
time_t stime, etime;
printf("Starting of the program...\n");
time(&stime);
char *ptext = "String";
loadlist(ptext);
time(&etime);
printf("time to load: %f \n", difftime(etime, stime));
return(0);
}
This code reads a text file line by line. But I need to put those lines in an array but I wasn't able to do it. Now I am getting an array of numbers somehow.
There are many ways to do this correctly. To begin with, first sort out what it is you actually need/want to store, then figure out where that information will come from and finally decide how you will provide storage for the information. In your case loadlist is apparently intended load a list of lines (up to 10000) so that they are accessible through your statically declared array of pointers. (you can also allocate the pointers dynamically, but if you know you won't need more than X of them, statically declaring them is fine (up to the point you cause StackOverflow...)
Once you read the line in loadlist, then you need to provide adequate storage to hold the line (plus the nul-terminating character). Otherwise, you are just counting the number of lines. In your case, since you declare an array of pointers, you cannot simply copy the line you read because each of the pointers in your array does not yet point to any allocated block of memory. (you can't assign the address of the buffer you read the line into with fgets (buffer, size, FILE*) because (1) it is local to your loadlist function and it will go away when the function stack frame is destroyed on function return; and (2) obviously it gets overwritten with each call to fgets anyway.
So what to do? That's pretty simple too, just allocate storage for each line as it is read using the strlen of each line as #iharob says (+1 for the nul-byte) and then malloc to allocate a block of memory that size. You can then simply copy the read buffer to the block of memory created and assign the pointer to your list (e.g. larray[x] in your code). Now the gnu extensions provide a strdup function that both allocates and copies, but understand that is not part of the C99 standard so you can run into portability issues. (also note you can use memcpy if overlapping regions of memory are a concern, but we will ignore that for now since you are reading lines from a file)
What are the rules for allocating memory? Well, you allocate with malloc, calloc or realloc and then you VALIDATE that your call to those functions succeeded before proceeding or you have just entered the realm of undefined behavior by writing to areas of memory that are NOT in fact allocated for your use. What does that look like? If you have your array of pointers p and you want to store a string from your read buffer buf of length len at index idx, you could simply do:
if ((p[idx] = malloc (len + 1))) /* allocate storage */
strcpy (p[idx], buf); /* copy buf to storage */
else
return NULL; /* handle error condition */
Now you are free to allocate before you test as follows, but it is convenient to make the assignment as part of the test. The long form would be:
p[idx] = malloc (len + 1); /* allocate storage */
if (p[idx] == NULL) /* validate/handle error condition */
return NULL;
strcpy (p[idx], buf); /* copy buf to storage */
How you want to do it is up to you.
Now you also need to protect against reading beyond the end of your pointer array. (you only have a fixed number since you declared the array statically). You can make that check part of your read loop very easily. If you have declared a constant for the number of pointers you have (e.g. PTRMAX), you can do:
int idx = 0; /* index */
while (fgets (buf, LNMAX, fp) && idx < PTRMAX) {
...
idx++;
}
By checking the index against the number of pointers available, you insure you cannot attempt to assign address to more pointers than you have.
There is also the unaddressed issue of handling the '\n' that will be contained at the end of your read buffer. Recall, fgets read up to and including the '\n'. You do not want newline characters dangling off the ends of the strings you store, so you simply overwrite the '\n' with a nul-terminating character (e.g. simply decimal 0 or the equivalent nul-character '\0' -- your choice). You can make that a simple test after your strlen call, e.g.
while (fgets (buf, LNMAX, fp) && idx < PTRMAX) {
size_t len = strlen (buf); /* get length */
if (buf[len-1] == '\n') /* check for trailing '\n' */
buf[--len] = 0; /* overwrite '\n' with nul-byte */
/* else { handle read of line longer than 200 chars }
*/
...
(note: that also brings up the issue of reading a line longer than the 200 characters you allocate for your read buffer. You check for whether a complete line has been read by checking whether fgets included the '\n' at the end, if it didn't, you know your next call to fgets will be reading again from the same line, unless EOF is encountered. In that case you would simply need to realloc your storage and append any additional characters to that same line -- that is left for future discussion)
If you put all the pieces together and choose a return type for loadlist that can indicate success/failure, you could do something similar to the following:
/** read up to PTRMAX lines from 'fp', allocate/save in 'p'.
* storage is allocated for each line read and pointer
* to allocated block is stored at 'p[x]'. (you should
* add handling of lines greater than LNMAX chars)
*/
char **loadlist (char **p, FILE *fp)
{
int idx = 0; /* index */
char buf[LNMAX] = ""; /* read buf */
while (fgets (buf, LNMAX, fp) && idx < PTRMAX) {
size_t len = strlen (buf); /* get length */
if (buf[len-1] == '\n') /* check for trailing '\n' */
buf[--len] = 0; /* overwrite '\n' with nul-byte */
/* else { handle read of line longer than 200 chars }
*/
if ((p[idx] = malloc (len + 1))) /* allocate storage */
strcpy (p[idx], buf); /* copy buf to storage */
else
return NULL; /* indicate error condition in return */
idx++;
}
return p; /* return pointer to list */
}
note: you could just as easily change the return type to int and return the number of lines read, or pass a pointer to int (or better yet size_t) as a parameter to make the number of lines stored available back in the calling function.
However, in this case, we have used the initialization of all pointers in your array of pointers to NULL, so back in the calling function we need only iterate over the pointer array until the first NULL is encountered in order to traverse our list of lines. Putting together a short example program that read/stores all lines (up to PTRMAX lines) from the filename given as the first argument to the program (or from stdin if no filename is given), you could do something similar to:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
enum { LNMAX = 200, PTRMAX = 10000 };
char **loadlist (char **p, FILE *fp);
int main (int argc, char **argv) {
time_t stime, etime;
char *list[PTRMAX] = { NULL }; /* array of ptrs initialized NULL */
size_t n = 0;
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
return 1;
}
printf ("Starting of the program...\n");
time (&stime);
if (loadlist (list, fp)) { /* read lines from fp into list */
time (&etime);
printf("time to load: %f\n\n", difftime (etime, stime));
}
else {
fprintf (stderr, "error: loadlist failed.\n");
return 1;
}
if (fp != stdin) fclose (fp); /* close file if not stdin */
while (list[n]) { /* output stored lines and free allocated mem */
printf ("line[%5zu]: %s\n", n, list[n]);
free (list[n++]);
}
return(0);
}
/** read up to PTRMAX lines from 'fp', allocate/save in 'p'.
* storage is allocated for each line read and pointer
* to allocated block is stored at 'p[x]'. (you should
* add handling of lines greater than LNMAX chars)
*/
char **loadlist (char **p, FILE *fp)
{
int idx = 0; /* index */
char buf[LNMAX] = ""; /* read buf */
while (fgets (buf, LNMAX, fp) && idx < PTRMAX) {
size_t len = strlen (buf); /* get length */
if (buf[len-1] == '\n') /* check for trailing '\n' */
buf[--len] = 0; /* overwrite '\n' with nul-byte */
/* else { handle read of line longer than 200 chars }
*/
if ((p[idx] = malloc (len + 1))) /* allocate storage */
strcpy (p[idx], buf); /* copy buf to storage */
else
return NULL; /* indicate error condition in return */
idx++;
}
return p; /* return pointer to list */
}
Finally, in any code your write that dynamically allocates memory, you have 2 responsibilities regarding any block of memory allocated: (1) always preserve a pointer to the starting address for the block of memory so, (2) it can be freed when it is no longer needed.
Use a memory error checking program to insure you haven't written beyond/outside your allocated block of memory, attempted to read or base a jump on an uninitialized value and finally to confirm that you have freed all the memory you have allocated.
For Linux valgrind is the normal choice. There are similar memory checkers for every platform. They are all simple to use, just run your program through it.
Look things over, let me know if you have any further questions.
It's natural that you see numbers because you are printing a single character using the "%d" specifier. In fact, strings in c are pretty much that, arrays of numbers, those numbers are the ascii values of the corresponding characters. If you instead use "%c" you will see the character that represents each of those numbers.
Your code also, calls strlen() on something that is intended as a array of strings, strlen() is used to compute the length of a single string, a string being an array of char items with a non-zero value, ended with a 0. Thus, strlen() is surely causing undefined behavior.
Also, if you want to store each string, you need to copy the data like you tried in the commented line with strcpy() because the array you are using for reading lines is overwritten over and over in each iteration.
Your compiler must be throwing all kinds of warnings, if it's not then it's your fault, you should let the compiler know that you want it to do some diagnostics to help you find common problems like assigning a pointer to a char.
You should fix multiple problems in your code, here is a code that fixes most of them
void
loadlist(const char *const filename) {
char line[100];
FILE *file;
// We can only read 100 lines, of
// max 99 characters each
char array[100][100];
int size;
size = 0;
file = fopen (filename, "r" );
if (file == NULL)
return;
while ((fgets(line, sizeof(line), file) != NULL) && (size < 100)) {
strcpy(array[size++], line);
}
fclose(file);
for (int i = 0 ; i < size ; ++i) {
printf("array[%d] = %s", i + 1, array[i]);
}
}
int
main(void)
{
time_t stime, etime;
printf("Starting of the program...\n");
time(&stime);
loadlist("Z:\\list.txt");
time(&etime);
printf("Time to load: %f\n", difftime(etime, stime));
return 0;
}
Just to prove how complicated it can be in c, check this out
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
struct string_list {
char **items;
size_t size;
size_t count;
};
void
string_list_print(struct string_list *list)
{
// Simply iterate through the list and
// print every item
for (size_t i = 0 ; i < list->count ; ++i) {
fprintf(stdout, "item[%zu] = %s\n", i + 1, list->items[i]);
}
}
struct string_list *
string_list_create(size_t size)
{
struct string_list *list;
// Allocate space for the list object
list = malloc(sizeof *list);
if (list == NULL) // ALWAYS check this
return NULL;
// Allocate space for the items
// (starting with `size' items)
list->items = malloc(size * sizeof *list->items);
if (list->items != NULL) {
// Update the list size because the allocation
// succeeded
list->size = size;
} else {
// Be optimistic, maybe realloc will work next time
list->size = 0;
}
// Initialize the count to 0, because
// the list is initially empty
list->count = 0;
return list;
}
int
string_list_append(struct string_list *list, const char *const string)
{
// Check if there is room for the new item
if (list->count + 1 >= list->size) {
char **items;
// Resize the array, there is no more room
items = realloc(list->items, 2 * list->size * sizeof *list->items);
if (items == NULL)
return -1;
// Now update the list
list->items = items;
list->size += list->size;
}
// Copy the string into the array we simultaneously
// increase the `count' and copy the string
list->items[list->count++] = strdup(string);
return 0;
}
void
string_list_destroy(struct string_list *const list)
{
// `free()' does work with a `NULL' argument
// so perhaps as a principle we should too
if (list == NULL)
return;
// If the `list->items' was initialized, attempt
// to free every `strdup()'ed string
if (list->items != NULL) {
for (size_t i = 0 ; i < list->count ; ++i) {
free(list->items[i]);
}
free(list->items);
}
free(list);
}
struct string_list *
loadlist(const char *const filename) {
char line[100]; // A buffer for reading lines from the file
FILE *file;
struct string_list *list;
// Create a new list, initially it has
// room for 100 strings, but it grows
// automatically if needed
list = string_list_create(100);
if (list == NULL)
return NULL;
// Attempt to open the file
file = fopen (filename, "r");
// On failure, we now have the responsibility
// to cleanup the allocated space for the string
// list
if (file == NULL) {
string_list_destroy(list);
return NULL;
}
// Read lines from the file until there are no more
while (fgets(line, sizeof(line), file) != NULL) {
char *newline;
// Remove the trainling '\n'
newline = strchr(line, '\n');
if (newline != NULL)
*newline = '\0';
// Append the string to the list
string_list_append(list, line);
}
fclose(file);
return list;
}
int
main(void)
{
time_t stime, etime;
struct string_list *list;
printf("Starting of the program...\n");
time(&stime);
list = loadlist("Z:\\list.txt");
if (list != NULL) {
string_list_print(list);
string_list_destroy(list);
}
time(&etime);
printf("Time to load: %f\n", difftime(etime, stime));
return 0;
}
Now, this will work almost as the python code you say you wrote but it will certainly be faster, there is absolutely no doubt.
It is possible that an experimented python programmer can write a python program that runs faster than that of a non-experimented c programmer, learning c however is really good because you then understand how things work really, and you can then infer how a python feature is probably implemented, so understanding this can be very useful actually.
Although it's certainly way more complicated than doing the same in python, note that I wrote this in nearly 10min. So if you really know what you're doing and you really need it to be fast c is certainly an option, but you need to learn many concepts that are not clear to higher level languages programmers.
When I ran a program, my console output showed the following:
malloc(): memory corruption (fast) and was followed by what seemed like an address in memory. I have narrowed it down to a few functions, but I feel that I free all memory that I allocate properly.
The following function takes a string representing a file name.
void readAndProcessFile(char* filename){
FILE *fileptr;
char* word = malloc(32*sizeof(char));
fileptr = fopen(filename, "r");
while(fscanf(fileptr,"%s",word) != EOF){
processWord(word);
}
fclose(fileptr);
free(word);
}
This function takes a word, removes any non-alphabetic characters and changes all letters to uppercase.
void processWord(char* text){
char* processedWord;
processedWord = trimAndCaps(text);
if(processedWord != NULL && processedWord[0] != '\0'){
addWord(processedWord);
}
free(processedWord);
}
Here's the trim and cap function
char* trimAndCaps(char* text)
{
int i = 0;
int j = 0;
char currentChar;
char* rv = malloc(sizeof(text));
while ((currentChar = text[i++]) != '\0')
{
if (isalpha(currentChar))
{
rv[j++] = toupper(currentChar);
}
}
rv[j] = '\0';
return rv;
}
And just for good measure here's the addWord function
void addWord(char* word)
{
// check if word is already in list
struct worddata* currentWord = findWord(word);
// word is in list
if(currentWord != NULL)
{
incrementCount(currentWord);
}
// word is not in list
else
{
currentWord = malloc(sizeof(struct worddata));
currentWord->count = 1;
strcpy(currentWord->word, word);
ll_add(wordList, currentWord, sizeof(struct worddata));
free(currentWord);
}
}
As you can see, all instances where I manually allocate memory, I free afterwards. This program works when there is a smaller amount of words, but not for larger. My thought process leads me to believe that there is some sort of leak, but when I have few enough words to the point that I can run it, I run the following code:
// The following code will print out the final dynamic memory used
struct mallinfo veryend = mallinfo();
fprintf(stderr, "Final Dynamic Memory used : %d\n", veryend.uordblks);
And this shows a 0 every time for memory used. What else can cause this? Any direction or fixes are much appreciated.
The following line doesn't do what you are hoping to:
char* rv = malloc(sizeof(text));
It only allocates 4 or8 bytes or memory depending on the size of pointers on your platform.
You need:
char* rv = malloc(strlen(text) + 1);
I have lineget function that returns char *(it detects '\n') and NULL on EOF.
In main() I'm trying to recognize particular words from that line.
I used strtok:
int main(int argc, char **argv)
{
char *line, *ptr;
FILE *infile;
FILE *outfile;
char **helper = NULL;
int strtoks = 0;
void *temp;
infile=fopen(argv[1],"r");
outfile=fopen(argv[2],"w");
while(((line=readline(infile))!=NULL))
{
ptr = strtok(line, " ");
temp = realloc(helper, (strtoks)*sizeof(char *));
if(temp == NULL) {
printf("Bad alloc error\n");
free(helper);
return 0;
} else {
helper=temp;
}
while (ptr != NULL) {
strtoks++;
fputs(ptr, outfile);
fputc(' ', outfile);
ptr = strtok(NULL, " ");
helper[strtoks-1] = ptr;
}
/*fputs(line, outfile);*/
free(line);
}
fclose(infile);
fclose(outfile);
return 0;
}
Now I have no idea how to put every of tokenized words into an array (I created char ** helper for that purpose), so that it can be used in qsort like qsort(helper, strtoks, sizeof(char*), compare_string);.
Ad. 2 Even if it would work - I don't know how to clear that line, and proceed to sorting next one. How to do that?
I even crashed valgrind (with the code presented above) -> "valgrind: the 'impossible' happened:
Killed by fatal signal"
Where is the mistake ?
The most obvious problem (there may be others) is that you're reallocating helper to the value of strtoks at the beginning of the line, but then incrementing strtoks and adding to the array at higher values of strtoks. For instance, on the first line, strtoks is 0, so temp = realloc(helper, (strtoks)*sizeof(char *)); leaves helper as NULL, but then you try to add every word on that line to the helper array.
I'd suggest an entirely different approach which is conceptually simpler:
char buf[1000]; // or big enough to be bigger than any word you'll encounter
char ** helper;
int i, numwords;
while(!feof(infile)) { // most general way of testing if EOF is reached, since EOF
// is just a macro and may not be machine-independent.
for(i = 0; (ch = fgetc(infile)) != ' ' && ch != '\n'; i++) {
// get chars one at a time until we hit a space or a newline
buf[i] = ch; // add char to buffer
}
buf[i + 1] = '\0' // terminate with null byte
helper = realloc(++numwords * sizeof(char *)); // expand helper to fit one more word
helper[numwords - 1] = strdup(buffer) // copy current contents of buffer to the just-created element of helper
}
I haven't tested this so let me know if it's not correct or there's anything you don't understand. I've left out the opening and closing of files and the freeing at the end (remember you have to free every element of helper before you free helper itself).
As you can see in strtok's prototype:
char * strtok ( char * str, const char * delimiters );
...str is not const. What strtok actually does is replace found delimiters by null bytes (\0) into your str and return a pointer to the beginning of the token.
Per example:
char in[] = "foo bar baz";
char *toks[3];
toks[0] = strtok(in, " ");
toks[1] = strtok(NULL, " ");
toks[2] = strtok(NULL, " ");
printf("%p %s\n%p %s\n%p %s\n", toks[0], toks[0], toks[1], toks[1],
toks[2], toks[2]);
printf("%p %s\n%p %s\n%p %s\n", &in[0], &in[0], &in[4], &in[4],
&in[8], &in[8]);
Now look at the results:
0x7fffd537e870 foo
0x7fffd537e874 bar
0x7fffd537e878 baz
0x7fffd537e870 foo
0x7fffd537e874 bar
0x7fffd537e878 baz
As you can see, toks[1] and &in[4] point to the same location: the original str has been modified, and in reality all tokens in toks point to somewhere in str.
In your case your problem is that you free line:
free(line);
...invalidating all your pointers in helper. If you (or qsort) try to access helper[0] after freeing line, you end up accessing freed memory.
You should copy the tokens instead, e.g.:
ptr = strtok(NULL, " ");
helper[strtoks-1] = malloc(strlen(ptr) + 1);
strcpy(helper[strtoks-1], ptr);
Obviously, you will need to free each element of helper afterwards (in addition to helper itself).
You should be getting a 'Bad alloc' error because:
char **helper = NULL;
int strtoks = 0;
...
while ((line = readline(infile)) != NULL) /* Fewer, but sufficient, parentheses */
{
ptr = strtok(line, " ");
temp = realloc(helper, (strtoks)*sizeof(char *));
if (temp == NULL) {
printf("Bad alloc error\n");
free(helper);
return 0;
}
This is because the value of strtoks is zero, so you are asking realloc() to free the memory pointed at by helper (which was itself a null pointer). One outside chance is that your library crashes on realloc(0, 0), which it shouldn't but it is a curious edge case that might have been overlooked. The other possibility is that realloc(0, 0) returns a non-null pointer to 0 bytes of data which you are not allowed to dereference. When your code dereferences it, it crashes. Both returning NULL and returning non-NULL are allowed by the C standard; don't write code that crashes regardless of which behaviour realloc() shows. (If your implementation of realloc() does not return a non-NULL pointer for realloc(0, 0), then I'm suspicious that you aren't showing us exactly the code that managed to crash valgrind (which is a fair achievement — congratulations) because you aren't seeing the program terminate under control as it should if realloc(0, 0) returns NULL.)
You should be able to avoid that problem if you use:
temp = realloc(helper, (strtoks+1) * sizeof(char *));
Don't forget to increment strtoks itself at some point.
I am building a program for class that is supposed to function as a very basic text-based text editor. It has 9 commands that can be passed to it, each resulting in a different command and we are using a doubly linked list to manage the lines of text that can be appended, inserted, removed and navigated through. I have been given a few functions to work with, and although most of my questions I think are more conceptual, I'll provide these given functions as a basis of background information:
// Function: get_line
// Reads a line (of arbitrary length) from an input source (stdin or file)
// and returns a pointer to the array that stores the line read
//
char *get_line(FILE *f)
{
int size = 80; // initial size of memory block allocated
char *linePtr = malloc(size); // allocate memory block
if (linePtr != NULL) { // if allocation successful
int c = EOF;
// read a line of text and replace newline character
// with a string terminator character
int i = 0;
while ((c = getc(f)) != '\n' && c != EOF) {
linePtr[i++] = (char) c;
if (i == size) {
size *= 2;
linePtr = realloc(linePtr, size);
}
}
linePtr[i] = '\0';
// if end-of-file before any characters were read,
// release the whole buffer
if (c == EOF && i == 0) {
free(linePtr);
linePtr = NULL;
} else {
// release unused portion of memory
linePtr = realloc(linePtr, i+1);
}
}
return linePtr;
}
My self-defined "append" function:
//
// Function: append_line
// Inserts a line after the current line (or node) in the linked-list
//
void append_line(char *t)
{
line *new_stack;
line *tmp;
new_stack = malloc(sizeof(line));
tmp = malloc(sizeof(line));
if((new_stack == NULL) || (tmp == NULL)) {
fprintf(stderr, "Insufficient memory to allocate. Closing application.\n");
exit(1);
}
if(current_line == NULL) {
if(head == NULL) {
head = new_stack;
current_line = new_stack;
new_stack->prev = NULL;
new_stack->next = NULL;
}
}
else {
tmp = current_line->next;
current_line->next = new_stack->prev;
new_stack->next = tmp;
current_line = new_stack;
}
new_stack->text = t;
free(tmp);
}
And this is my read_file function that doesn't really do anything yet, but I'm not exactly sure if I've got the right mind-set going into the creation of this function:
// Function: read_file
// Reads text from the specified file and calls append_line to insert lines
// into the linked-list. It returns the number of lines read.
//
int read_file(char *filename)
{
char * temp, no_command;
temp = strtok(filename, " ");
while(temp != NULL) {
no_command = temp;
temp = strtok (NULL, " ");
}
/* By doing this --^, I hope it will set temp to the actual
// file name after tokenization and completely ignore
// the command that comes with filename */
FILE *fin;
int counter = 0;
fin = fopen(no_command, "r");
if(fin == NULL) {
printf("You have entered a file that does not exist.");
exit(0);
}
get_line(fin);
fclose(fin);
}
If I wanted to send get_line input entered by the user from another function, could I send get_line stdin for it to recognize user input typed on-screen? Or would I have to make use of some form of fgets to send it the information?
If the user should be allowed to enter in multiple lines by separating them with the enter key (aka \n), and is expected to be allowed to press CTRL+D to continue with the function, how does one tell the application to make CTRL+D the EOF?
My get_line function takes in an entire file, and outputs a line. I am instructed to make use of multiple calls of get_line to take files of multiple lines and send each line into their own respective stack entry. How do I tell the application, "Here's the same file, but I want you to NOW check the next line instead of the one you output previously"? I assume that once I figure this out, I can apply the same logic to input entered in on the fly by the user.
I have been instructed that the get_line function is complete, so I feel like in the places I would call get_line (such as in read_file), I would need to control how much is sent to get_line at a time by making read_file read up to the point where a \n is encountered, change it to an end-of-line symbol (aka '\0') and send it to get_line, and then somehow have read_file continue after that point doing the same thing until EOF is reached. But I also feel like a lot of this functionality is within the get_line function as well... so I suppose I'm confused on the implementation of my given functions.
I'll probably have more questions in the future, but for now, I believe this initial question is long-winded enough. I'm hoping that figuring these things out, the rest of it will just click in place within my mind. Thank you for your time!
There are a couple of more errors in the append_line function, for example you use new_stack->prev before it's initialized.
Here comes my take on it:
void append_line(char *t)
{
/* Allocate and clear (i.e. set all to 0) */
line *new_stack = calloc(1, sizeof(line));
if(current_line == NULL) {
if(head == NULL) {
head = current_line = new_stack;
}
}
else {
new_stack->next = current_line;
new_stack->prev = current_line->prev;
if (current_line->prev)
current_line->prev->next = new_stack;
else
head = new_stack; /* No previous node, means current_line is at head */
current_line = new_stack;
}
new_stack->text = t;
}