Bloc de code:
#define kbarDefaultString "bar"
char *bar;
/* need, free the allocated storage */
/* strdup() */
/* need, free the allocated storage */
char *foo_encoding(char * c)
{
/*
....
*/
return foo_encoding_char;
}
char *get_val(char * c)
{
/*
....
*/
/* could be NULL */
return get_val;
}
int foo()
{
char *bar_copy;
bar = strdup(kbarDefaultString);
if( bar_copy = get_val(bar) )
{
val = foo_encoding(bar_copy);
/* do that */
realloc(bar,strlen(val)+1);
/* or do that ? */
free(bar);
/* or simply crush ? */
bar = strdup(val);
free(val);
}
return 0;
}
int main(void)
{
int res;
res = foo();
if(res)
{
/* new val */
printf("%s",bar);
free(bar);
exit(0);
}else {
/* kbarDefaultString */
printf("%s",bar);
free(bar);
exit(1);
}
return 0;
}