So I'm learning to code (in C) for a few months now and I'm focusing here on memory allocation.
My code is very simple, everything works, and yet at the end, it doesn't return a 0 but instead I get a -1073740940 (0xC0000374) which, I noticed, have something to do with memory allocation (lol). And seriously I don't know how to correct it.
Here it is :
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char* chaine1 = (char*)malloc(10*sizeof(char));
char* chaine2 = (char*)malloc(10*sizeof(char));
if(chaine1 == NULL || chaine2 == NULL)
return -2;
strcpy(chaine1, "Salut les");
strcpy(chaine2, " codeurs!");
printf("Chaine 1: %s\n", chaine1);
printf("Chaine 2: %s\n", chaine2);
int taille1 = strlen(chaine1);
int taille2 = strlen(chaine2);
char* tmp_chaine = (char*)malloc(sizeof(char)*10);
if(tmp_chaine == NULL)
return -2;
strcpy(tmp_chaine, chaine1);
realloc(chaine1, sizeof(char)*(taille1+taille2+1));
for(int i = 0; i <= taille1; i++)
chaine1[i] = tmp_chaine[i];
for(int i = 0; i <= taille2; i++)
chaine1[taille1+i] = chaine2[i];
printf("%s", chaine1);
return 0;
}
I noticed that when I try to free my strings, the program crashes before the end, but that's all I could figure out.
(I know the code has nothing optimized, please don't yell at me :( )
hmmmmm
$ make so
cc -O2 -pipe so.c -o so
so.c:30:2: warning: ignoring return value of function declared with 'warn_unused_result' attribute [-Wunused-result]
30 | realloc(chaine1, sizeof(char)*(taille1+taille2+1));
| ^~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1 warning generated.
This warning, is indeed a serious error, as if realloc()
needs to reallocate the memory in another place, it will return a valid pointer pointing to a different place, invalidating the one you passed to the routine.
You can do:
chaine1 = realloc(chaine1, sizeof(char)*(taille1 + taille2 + 1));
instead. If you don't and realloc()
moves the buffer to elsewhere, the memory pointed by chaine1
will be unallocated, and Undefined Behavior will be in force. Look that I check the pointer again, after realloc()
has return, as it could be wrong (the original value will have been lost if you don't save it first, but you cannot continue, once realloc
has failed)
Next is how your code should appear, after the modifications proposed:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
char* chaine1 = (char*)malloc(10*sizeof(char));
char* chaine2 = (char*)malloc(10*sizeof(char));
if(chaine1 == NULL || chaine2 == NULL)
return -2;
strcpy(chaine1, "Salut les");
strcpy(chaine2, " codeurs!");
printf("Chaine 1: %s\n", chaine1);
printf("Chaine 2: %s\n", chaine2);
int taille1 = strlen(chaine1);
int taille2 = strlen(chaine2);
char* tmp_chaine = (char*)malloc(sizeof(char)*10);
if(tmp_chaine == NULL)
return -2;
strcpy(tmp_chaine, chaine1);
chaine1 = realloc(chaine1, sizeof(char)*(taille1+taille2+1));
if (chaine1 == NULL)
return -3;
for(int i = 0; i <= taille1; i++)
chaine1[i] = tmp_chaine[i];
for(int i = 0; i <= taille2; i++)
chaine1[taille1+i] = chaine2[i];
printf("%s", chaine1);
return 0;
}