-1
char *dumpTB (TB tb){

    char* text = malloc(sizeof(char));

    int i = 0; // 
    int x = 0; //string index

    tNode* curr = tb->head;

    while(curr != NULL){

        while(curr->line[x] != '\n'){
            printf("%d", i);
            text[i] = curr->line[x];
            printf("%c\n", text[i]);

            text = realloc(text, i+1);

            i++;
            x++;
        }
        text[i] = '\n';
        printf("%c", text[i]);
        text = realloc(text, i+1); 
        i++;

        x = 0; 
        curr = curr->next; 
    }

    return text;
}

So I manage to print out the first 12 letters of my string using the print statements but for some reason it gives me a seg fault shortly after printing the 12th letter 'l', and based on the print statements it seems to occur around the realloc...can anyone tell me what I did wrong?

int i = 1; // 
    int x = 0; //string index

    tNode* curr = tb->head;

    while(curr != NULL){

        while(curr->line[x] != '\n'){
            printf("%d", i-1);
            text[i-1] = curr->line[x];
            printf("%c\n", text[i-1]);

            text = realloc(text, i+1);
            i++;
            x++;
        }
        printf("%d\n", i-1);
        text[i-1] = '\n';
        printf("%c", text[i-1]);
        text = realloc(text, i+1); 
        i++;

        x = 0; 
        curr = curr->next; 
        //printf("%c\n", curr->line[0]);
    }

I tried fixed the index errors, a really long looking sysmalloc assertion thing which aborts the program.

GCGSAUCE
  • 13
  • 3
  • If you step through the code line by line with a debugger your problem should become obvious. Hint: What is the value of `i` in the very first call to `realloc`? And what is then the result of `i+1`? – Some programmer dude Dec 20 '17 at 19:58
  • @GCGSAUCE What printf statement do you mean? – Vlad from Moscow Dec 20 '17 at 20:00
  • 1
    Furthermore, if you expect to treat the memory you allocate as a ***null-terminated** byte string*, then you're forgetting one very important thing. – Some programmer dude Dec 20 '17 at 20:00
  • @VladfromMoscow there are only 2 printf statements in the code, its in reference to that. – GCGSAUCE Dec 20 '17 at 20:14
  • @Someprogrammerdude Im really not sure if i am treating it as null-terminated, could u give me a bigger clue as to what you mean? – GCGSAUCE Dec 20 '17 at 20:17
  • 1
    Consider giving some [MCVE]. In its current form, your code is not one, we don't know what `TB` is, and we should not even try to guess. – Basile Starynkevitch Dec 20 '17 at 20:27
  • The pointer that you return, will you pass it to `strlen`? Will you do e.g. `printf("%s", returned_pointer)`? Will you treat it as a *string*? Then it needs to be *terminated* by a zero (the "null"). [Any good beginners book](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) should have told you that! – Some programmer dude Dec 20 '17 at 20:37

1 Answers1

1

You probably want getline(3), so use it if you have it. As explained by AnT's answer, you have undefined behavior (UB) because of a buffer overflow. Fix that as soon as possible. Be very scared of UB and take more time to read about it (it is tricky).

Also, remember that both malloc(3) and realloc(3) are somehow expensive calls (for some suitable notion of expensive; actually, they are quite fast - often less than a microsecond on a desktop for reasonable use), and they could fail (by giving NULL) and you should check that.

In practice, you'll better use realloc less often. As a rule of thumb, you want to keep both the used length and the allocated size somewhere (e.g. in additional local variables), and you might use some geometrical progression (e.g. newsize = 4*oldsize/3 + 10....) to avoid too frequent realloc-s. Doing a realloc for each additional byte is ugly.

Compile your code with all warnings and debug info, e.g. gcc -Wall -Wextra -g with GCC. Improve the code to get no warnings. Use the debugger gdb and valgrind to hunt memory leaks and other trouble.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547