Why is my string resizing? (FROM)

    fseek(fp, 0L, SEEK_SET);
        int i; char c;
        i = 0;
        for (c = getc(fp); c != EOF; c = getc(fp)) {
            c = tolower(c);
            file_string[i] = c;
            i++;
        }

      

In this code, I read each character in the file, convert it to lowercase and put it in a string. Now, let's say I allocate 21 bytes * sizeof (char) to file_string. Sometimes, after this piece of code shown here, strlen (file_string) will return 30 and not the expected 20. Is there something wrong with my pointer arithmetic? Some things I have collected:

1 - This only happens for a while.

2 - I made sure that I allocate the right amount of bytes in the file_string (which happens just before this code). The code looks like this:

fseek(fp, 0L, SEEK_END);
file_len = ftell(fp);
file_string = malloc( sizeof(char) * (file_len+1) );

      

printing file_len outputs the expected length.

3 - I printed out the value of i to make sure it repeats the exact number of times as line_file length and it is.

4 - Now, RIGHT after this code (after closing the file), when I print the length of file_string, sometimes it suddenly grows to some larger size. This was giving me problems elsewhere in my code.

Now, I suppose I can just stick a terminal null character in there and fix the problem (possibly leading to errors further down the line), but I'd rather know what's going on here under the hood.

Here is an example of my debug showing the before and after resizing. Keep in mind that the leading length matches the file_len variable.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> file_len: 24
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 1
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 2
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 3
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 4
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 5
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 6
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 7
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 8
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 9
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 10
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 11
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 12
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 13
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 14
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 15
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 16
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 17
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 18
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 19
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 20
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 21
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 22
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>i: 23
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> strlen(file_string): 30

      

+3


source to share


1 answer


Now, I suppose I can just stick a terminal null character in there and fix the problem (possibly leading to errors further down the line), but I'd rather know what's going on here under the hood.

It's literally a decision. strlen

searches the NULL

buffer for a terminator but doesn't find it because you haven't added it yet. After your read code, you must explicitly add a NULL terminator (i.e. file_string[i] = '\0'

).



Remember, the memory returned malloc

is not zeroed out, it is mostly random data (well ... the contents of the store returned malloc

is undefined). What happens is that you run from the end of the buffer and into arbitrary memory, and then just runs at zero byte further down the line and assumes it's the end of the line.

+4


source







All Articles