I have text implemented in 2d arrays in c. There is some logic to free them in some cases. So this clearing does not work. text_from_file[i] and local_text[i] (the second one does not exist in that context) are both formatted with a high chance. But it should not happen. This is why heap corruption occurs.
int load_from_file() {
FILE* file = fopen(file_name, "r");
if (file == NULL) {
printf("Failed to open filen");
return -1;
}
if (text_from_file != NULL) {
printf("File already loadedn");
return -1;
}
text_from_file = calloc(text_from_file_rows, sizeof(char*));
// added checking for correct allocation
if (text_from_file == NULL) {
printf("Failed to allocate memoryn");
fclose(file);
return -1;
}
for (int i = 0; i < text_from_file_rows; i++) {
text_from_file[i] = calloc(text_from_file_chars, sizeof(char));
// same checking here
if (text_from_file[i] == NULL) {
printf("Failed to allocate memoryn");
fclose(file);
return -1;
}
}
int i = 0;
int length = 0;
while (fgets(text_from_file[i] + length, text_from_file_chars - length, file) != NULL) {
length = strlen(text_from_file[i]);
if (length == (text_from_file_chars - 1)) {
text_from_file_chars *= 2;
text_from_file[i] = realloc(text_from_file[i], text_from_file_chars);
if (text_from_file[i] == NULL) {
printf("Failed to reallocate memoryn");
fclose(file);
return -1;
}
}
if (text_from_file[i][strlen(text_from_file[i]) - 1] == 'n') {
text_from_file[i][strlen(text_from_file[i]) - 1] = '';
length = 0;
i++;
}
if (i >= text_from_file_rows) {
text_from_file_rows *= 2;
char** temp = realloc(text_from_file, text_from_file_rows * sizeof(char*));
if (temp == NULL) {
printf("Failed to reallocate memoryn");
fclose(file);
return -1;
}
text_from_file = temp;
for (int j = i; j < text_from_file_rows; j++) {
text_from_file[j] = calloc(text_from_file_chars, sizeof(char));
if (text_from_file[j] == NULL) {
printf("Failed to allocate memoryn");
fclose(file);
return -1;
}
}
}
}
text_from_file[i + 1] = NULL;
fclose(file);
return 0;
}
int concatenate_text() {
// concatenate text from file with local text
if (local_text == NULL) {
text_concatenator();
printf("Text concatenated with local successfully.n");
return 0;
}
else
{
printf("Local text is not empty. Cannot concatenate textsn");
if (remove_text()) {
concatenate_text();
}
else
{
return 1;
}
}
}
int text_concatenator() {
if (text_from_file == NULL) {
return 0;
}
if (local_text_rows < text_from_file_rows) {
local_text_rows = text_from_file_rows;
}
if (local_text_chars < text_from_file_chars)
{
local_text_chars = text_from_file_chars;
}
local_text = calloc(local_text_rows, sizeof(char*));
for (int i = 0; i < text_from_file_rows; i++) {
local_text[i] = calloc(local_text_chars, sizeof(char));
if (text_from_file[i] != NULL) {
strcpy(local_text[i], text_from_file[i]);
local_text[i][strlen(text_from_file[i])] = '';
}
}
return 0;
}
and the next given file have this exception:
int remove_text() {
printf("Do you want to save local text to file (1) or clear text from file only (0)?: ");
char answer;
scanf(" %c", &answer);
switch (answer) {
case '1':
save_to_file();
printf("Local text has been saved to file.n");
return 1;
case '0':
for (int i = 0; i < text_from_file_rows; i++) {
if (text_from_file[i] != NULL) {
free(text_from_file[i]);
text_from_file[i] = NULL;
}
}
free(text_from_file);
text_from_file = NULL;
printf("Local text has been cleared.n");
return 0;
default:
printf("Local text and text from file saved separatelyn");
printf("You will need to previously save local text to filen");
return 0;
}
}
it does occur on line
if (text_from_file[i] != NULL) {
free(text_from_file[i]);
text_from_file[i] = NULL;
}
there is debug console output at this program state:
- A breakpoint instruction (__debugbreak() statement or a similar call) was executed in Assignment1.exe.
debug console output
I was trying to debug in a lot of ways but nothing changes.
The huge problem is that one time everything can execute correctly, but in most cases it crashes. It seems like at one time processes synchronized correctly but at another, they both modified in the wrong way. But I’m not operating with any processes in this project
1