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:

  1. i'm reserving space pointer not contents points to, or
  2. (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

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -