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 :( )
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 :( )
Share Improve this question edited Feb 1 at 20:29 Denis Tarakanov 172 bronze badges asked Feb 1 at 17:48 FulmineoFulmineo 111 silver badge2 bronze badges 3 |2 Answers
Reset to default 4Code 0xC0000374 is STATUS_HEAP_CORRUPTION. The heap corruption happens because realloc()
is used improperly.
realloc(chaine1, sizeof(char)*(taille1+taille2+1));
The returned new pointer is ignored, chaine1
pointer is unchanged and points to an old and freed memory location. Further lines use that wrong memory location and corrupt the heap.
You can allocate the required size a few lines above with malloc()
without using realloc()
.
If you still wish to try realloc()
then it should be
char* new_chaine1 = realloc(chaine1, taille1+taille2+1);
if (new_chaine1) {
chaine1 = new_chaine1;
new_chaine1 = NULL;
} else {
// handle the reallocation error
}
The code exhibits memory leaks in main()
without free()
.
Further notes. sizeof(char)
is odd, always 1, can be omitted. (char*)
cast from void*
is not needed at malloc()
.
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;
}
realloc(chaine1, sizeof(char)*(taille1+taille2+1));
you've thrown away the new pointer, and the old pointerchaine1
is now dead. But you continue to use it. – Weather Vane Commented Feb 1 at 17:51free(chaine1)
because it has already been freed byrealloc
. – Weather Vane Commented Feb 1 at 17:55