I have the following code:
int main()
{
char * str1 = (char*)malloc(101 * sizeof(char));
for (int i=0; i<100; i++)
{
str1[i] = 'b';
}
str1[100] = 0;
char * str2 = (char*)malloc(1001 * sizeof(char));
for (int i=0; i<1000; i++)
{
str2[i] = 'a';
}
str2[1000] = 0;
for (int i=0; i<7000; i++)
{
char * tmp = str2;
str2 = (char*) malloc((strlen(str2) + strlen(str1) + 1) * sizeof(char));
sprintf(str2, "%s%s", tmp, str1);
free(tmp);
}
free(str1);
free(str2);
}
When running it, task manager reports the following Memory Usage: beginning of the program - 1056K , end of the program - 17,748K
To my knowledge there are no memory leaks and I compiled it without debug symbols (release mode).
Any ideas why this might happen?