C
c.arnet
Hi there,
I've written a function that 'converts' a structure of type timeval to
a c-string:
char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);
return cpResult;
}
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?
Thanks,
Cornel
I've written a function that 'converts' a structure of type timeval to
a c-string:
char *tv2Str(struct timeval tvX)
{
char caStr[20];
char *cpResult;
sprintf(caStr, "%ld.%ld", tvX.tv_sec, tvX.tv_usec);
cpResult = (char *)malloc(sizeof(char)*strlen(caStr));
strcpy(cpResult, caStr);
return cpResult;
}
I use it like this:
printf("%s", tv2Str((struct timeval){123, 456}) );
Question:
How (if at all) does the compiler (gcc) ensure that the memory
allocated in the above function is deallocated after the printf call ?
Thanks,
Cornel