I'm (attempting?) returning memory allocated inside a function.
Assuming there are no obvious problems with what I wrote here (and didn't test) how is the memory freed or not freed? I need to free(bob)
later because he's on the heap not the stack, right?
I read something about reference counting in C in another accepted answer just now but I really really don't remember C having anything like a garbage collector.
char* strCat5000(char *fmt, ...)
{
char buff[5000];
char *ret_;
va_list arg_ptr;
va_start(arg_ptr, fmt);
vsnprintf(buff, sizeof(buff), fmt, arg_ptr);
va_end(arg_ptr);
//ret_ = malloc((char*)(strlen(buff)+1)*sizeof(char)); //Allocated inside function
ret_ = (char*)malloc((strlen(buff)+1)*sizeof(char)); //moved (char*) .. typo
ret_ = strcpy(ret_,buff);
return (ret_);
}
...
void findBob()
{
char * bob;
bob = strCat1000("Server is %s for %d seconds.", "on fire", 35329);
printf("%s", bob);
free(bob); //bob needs to be freed here or he'll leak when findBob ends???
}