Question Details

No question body available.

Tags

c gcc dynamic-memory-allocation realloc pointer-to-pointer

Answers (4)

September 2, 2025 Score: 1 Rep: 313,574 Quality: Low Completeness: 50%

After this line

workspacefile->flc = realloc(workspacefile->flc, (line+2)sizeof(char));

the added pointer has an indeterminate value. So using realloc in this statement

workspacefile->flc[line] = realloc(workspacefile->flc[line], (pos+2)*sizeof(char));

results in undefined behavior.

Pay attention to that in general the function realloc can return a null pointer. So you should use an intermediate pointer to store the result of a call of realloc and if it is not equal to null to assign it to pointers workspacefile->flc and workspacefile->flc[line]. Otherwise you will have at least a memory leak.

September 2, 2025 Score: 0 Rep: 153,049 Quality: Medium Completeness: 80%

There are multiple problems in the posted fragment:

  • you do not test for fopen success: the program will crash if the file cannot be opened.
  • fileInputBuffer should have type int to accommodate for all byte values and the special negative value EOF.
  • when you reallocate the array of char, you do not allocate a new empty line at the end of the array.
  • you do not check for memory allocation errors.
  • the variable names are too long, making the code harder to read.

Here is a modified version:

#include 
#include 
#include 
#include 
#include 

bool read_file(MyStruct
wp) { FILE fp = fopen(wp->filename, "r"); wp->flc = NULL; wp->len = 0; if (fp == NULL) { fprintf(stderr, "cannot open %s: %s\n", wp->filename, strerror(errno)); return false; } int line = 0; int pos = 0; wp->flc = malloc(1 sizeof(char )); wp->flc[0] = malloc(1); int c; while ((c = fgetc(fp)) != EOF) { if (c == '\n') { wp->flc[line][pos] = '\0'; line++; wp->flc = realloc(wp->flc, (line + 1) sizeof(char *)); pos = 0; wp->flc[line] = malloc(1); } else { wp->flc[line][pos] = (char)c; pos++; wp->flc[line] = realloc(wp->flc[line], pos + 1); } } if (pos) { // last line without a trailing newline wp->flc[line][pos] = '\0'; line++; } else { // free the empty line free(wp->flc[line]); } wp->len = line; fclose(fp); return true; }
September 2, 2025 Score: 0 Rep: 234,889 Quality: Low Completeness: 40%

In the else clause you extend the number of lines you're storing, but you don't initialize the new line that you're going to write to. So when you try to write the first character of the next line, you're dereferencing an invalid pointer.

workspacefile->flc = realloc(workspacefile->flc, (line+2)sizeof(char)); line++; pos = 0; workspace_file->flc[line] = malloc(1); // set up the next line
September 18, 2025 Score: 0 Rep: 39 Quality: Low Completeness: 40%

Yeah, I found a way to fix it. Sorry for not being here. I have replaced dynamic line counter increases with line counter and malloc in the beginning of this function. Now I just use int linesamount = linecounter(workspacefile->filename); workspacefile->flc = malloc(sizeof(char) lines_amount);

Thanks for help!