I have a problem with below function.
When I try to realloc()
memory I get more than I actually asked for!
In this instance I try to concatenate 2 strings, one who is 14 characters long and one who is 11 characters long, but the end result is that memTemp
is 38 characters long even though memNewSize
shows that it is in fact 25, does anyone know what to do?
int dstring_concatenate(DString* destination, DString source)
{
assert(destination != NULL); // Precondition: destination ar ej NULL
assert(*destination != NULL); // Precondition: *destination ar ej NULL
assert(source != NULL); // Precondition: source ar ej NULL
//Dstring looks like this = "typedef char* Dstring;"
int memNewSize = strlen(*destination) + strlen(source);
char *memTemp;
memTemp = (char*)realloc(memTemp, sizeof(char) * memNewSize);
printf("%d\n", memNewSize);
printf("%d\n", strlen(memTemp));
if(memTemp == NULL)
{
printf("Could not allocate new memory.\n");
return 0;
}
else
{
*destination = memTemp;
strcat(*destination, source);
return 1;
}
}