I know this is bad:
int * f1( void )
{
int j = 999;
return &j;
}
and this is good:
char * f2( void )
{
char * array;
array = (char *) malloc(8);
return array;
}
But is the following allowed?
char * f3 ( void )
{
char * array2;
array2 = f2();
return array2;
}
void main ( void )
{
char* array3 = f3();
printf("%c\n", array3[1]);
return;
}
Will the memory pointed by array2 still be valid after f3 returns? I would think it would be since you malloc()'ed memory for the array. But, I also think it wouldn't because array2 was a local variable within f3.
Any help?
int * f1( void )
{
int j = 999;
return &j;
}
and this is good:
char * f2( void )
{
char * array;
array = (char *) malloc(8);
return array;
}
But is the following allowed?
char * f3 ( void )
{
char * array2;
array2 = f2();
return array2;
}
void main ( void )
{
char* array3 = f3();
printf("%c\n", array3[1]);
return;
}
Will the memory pointed by array2 still be valid after f3 returns? I would think it would be since you malloc()'ed memory for the array. But, I also think it wouldn't because array2 was a local variable within f3.
Any help?
