c - Why is the compiler OK with this? -
i spent embarrassing amount of time last night tracking down segfault in application. ultimately, turned out i'd written:
anne_sprite_frame *desiredframe; *desiredframe = anne_sprite_copy_frame(&sprite->current);
instead of:
anne_sprite_frame desiredframe; desiredframe = anne_sprite_copy_frame(&sprite->current);
in line 1 created typed pointer, , in line 2 set value of dereferenced pointer struct returned anne_sprite_copy_frame()
.
why problem? , why did compiler accept @ all? can figure problem in example 1 either:
- i'm reserving space pointer not contents points to, or
- (unlikely) it's trying store return value in memory of pointer itself
i'm reserving space pointer not contents points to
yeah, exactly. compiler (unless static analysis) can't infer that. sees syntax valid , types match, compiles program. dereferencing uninitialized pointer undefined behavior, though, program work erroneously.
Comments
Post a Comment