Double indirection and structures passed into a function
I am curious why this code works:
typedef struct test_struct {
int id;
} test_str开发者_JAVA百科uct;
void test_func(test_struct ** my_struct)
{
test_struct my_test_struct;
my_test_struct.id=267;
*my_struct = &my_test_struct;
}
int main ()
{
test_struct * main_struct;
test_func(&main_struct);
printf("%d\n",main_struct->id);
}
This works, but pointing to the memory address of a functions local variable is a big no-no, right?
But if i used a structure pointer and malloc, that would be the correct way, right?
void test_func(test_struct ** my_struct)
{
test_struct *my_test_struct;
my_test_struct = malloc(sizeof(test_struct));
my_test_struct->id=267;
*my_struct = my_test_struct;
}
int main ()
{
test_struct * main_struct;
test_func(&main_struct);
printf("%d\n",main_struct->id);
}
The first version working is just dumb luck. Try randomly calling something else after test_func returns but before you call printf.
The second version is correct. Of course, you didn't free the heap memory, but whether that matters at the end of a program is a matter of debate.
You are right, passing a pointer to something that is allocated on the stack (and therefore goes away when the function returns, is wrong).
Passing a pointer to a heap allocated variable is fine.
"It works" is an illusion. You are returning a pointer to a stack-allocated variable in the first code example.
The pointer will point to garbage -- try dereferencing it...
精彩评论