I'm having an issue where, when I run my already compiled program ./main.exe (gcc main.c -o main.exe) through a Makefile, a realloc call fails when the size goes over 40 bytes. However, if I run ./main.exe manually, everything works as expected.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char **array;
int size;
} stringList;
void add_string(stringList *list, char *string)
{
printf("Arr size: %zu. ", ((list->size) + 1) * sizeof(char *));
char **newAddress = realloc(list->array, ((list->size) + 1) * sizeof(char *));
if (newAddress != NULL)
{
char *newString = malloc((strlen(string) + 1) * sizeof(char));
if (newString != NULL)
{
strcpy(newString, string);
list->array = newAddress;
list->array[list->size] = newString;
list->size++;
printf("Fine. ");
}
else
{
printf("Allocating new string failed. ");
}
}
else
{
printf("Allocating new array failed. ");
}
}
int main(int argc, char *argv[])
{
stringList list = { NULL, 0 };
if (argc != 3)
{
argv[1] = malloc(3 * sizeof(char)); argv[1] = "in";
argv[2] = malloc(3 * sizeof(char)); argv[2] = "out";
}
FILE *input = fopen(argv[1], "r");
FILE *output = fopen(argv[2], "w");
// I found that the line above may cause the problem (but I left it because I will use it)
char buffer[256] = {0};
while (fscanf(input, "%s", buffer) != EOF)
{
add_string(&list, buffer);
}
fclose(input);
fclose(output);
return 0;
}
I used this simple "in" file for test:
1 2 3 4 5 6 7 8 9 10 11 12 13
So here, if I manually start ./main.exe, adding goes just fine
PS C:\Users\Ivan\Desktop\maketest> ./main.exe
Arr size: 4. Fine. Arr size: 8. Fine. Arr size: 12. Fine. Arr size: 16. Fine. Arr size: 20. Fine. Arr size: 24. Fine. Arr size: 28. Fine. Arr size: 32. Fine.
Arr size: 36. Fine. Arr size: 40. Fine. Arr size: 44. Fine. Arr size: 48. Fine. Arr size: 52. Fine.
PS C:\Users\Ivan\Desktop\maketest> ./main.exe
Arr size: 4. Fine. Arr size: 8. Fine. Arr size: 12. Fine. Arr size: 16. Fine. Arr size: 20. Fine. Arr size: 24. Fine. Arr size: 28. Fine. Arr size: 32. Fine.
Arr size: 36. Fine. Arr size: 40. Fine. Arr size: 44. Fine. Arr size: 48. Fine. Arr size: 52. Fine.
But running makefile make run (this is the entire file below)
.PHONY: run
run:
./main.exe
It has 50% chance either to work correctly or give errors as the size exceeds 40 till the end.
PS C:\Users\Ivan\Desktop\maketest> make run
./main.exe
Arr size: 4. Fine. Arr size: 8. Fine. Arr size: 12. Fine. Arr size: 16. Fine. Arr size: 20. Fine. Arr size: 24. Fine. Arr size: 28. Fine. Arr size: 32. Fine.
Arr size: 36. Fine. Arr size: 40. Fine. Arr size: 44. Allocating new array failed. Arr size: 44. Allocating new array failed. Arr size: 44. Allocating new array failed.
PS C:\Users\Ivan\Desktop\maketest> make run
./main.exe
Arr size: 4. Fine. Arr size: 8. Fine. Arr size: 12. Fine. Arr size: 16. Fine. Arr size: 20. Fine. Arr size: 24. Fine. Arr size: 28. Fine. Arr size: 32. Fine.
Arr size: 36. Fine. Arr size: 40. Fine. Arr size: 44. Fine. Arr size: 48. Fine. Arr size: 52. Fine.
I appreciate any explanation! I expect to start the exe via makefile normaly and understand my mistake.
#includes and the whole makefile to reproduce the problem? How are you compiling it? How is the makefile compiling it?list->arrayoriginally set by allocation, or was it explicitly initialized toNULL? A Minimal Reproducible Example would help. Erratic behaviour can be a symptom of undefined behaviour, so perhaps it was never explictly set, and only happened to beNULLin the successful one.%din theprintfshould be%zu, because thestrlenand thesizeofevaluates to the typesize_t.makevs running the same command directly from the command line is what makes a difference here. Possibly the particular makefile used contains additional code, not shown, that that causes a meaningfully different environment to be provided in the run-via-makecase, but then the difference would be a function of the makefile, not ofmakeitself.