0

Why is it that I'm able to do the following?

char *name;
scanf("%10s", name);
printf("%s\n", name);

And the program/compiler doesn't raise a warning? I thought that since the name pointer isn't initialized, that the compiler (or possibly runtime) would raise a warning/error saying it's trying to write to an uninitialized memory address.

phuclv
  • 37,963
  • 15
  • 156
  • 475
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
  • 2
    Because you're not passing `-Wall` (or `-Wuninitialized`)? – Siguza Feb 08 '21 at 01:36
  • This can be hard to detect at compile time because halting problem, and such checks are not normally done at runtime because of the performance hit. If you want checks like this, look for tools like valgrind, AddressSanitizer, etc. – Nate Eldredge Feb 08 '21 at 01:40
  • 2
    If using gcc, note that to have it even try to catch uses of uninitialized variables at compile time, you have to enable optimization. Try `-O -Wall`. – Nate Eldredge Feb 08 '21 at 01:42
  • @NateEldredge https://godbolt.org/z/ExqGcT doesn't enable optimization but does use -Wall and catches it. – Jerry Jeremiah Feb 08 '21 at 01:42
  • @Siguza cool, actually I only get it from the `-Wuninitialized`: `warning: ‘name’ is used uninitialized in this function [-Wuninitialized]` – samuelbrody1249 Feb 08 '21 at 01:43
  • @JerryJeremiah: I guess I misremembered. I think it's still true that it is *more* successful at catching such issues when optimization is enabled. – Nate Eldredge Feb 08 '21 at 01:44
  • @NateEldredge I don't doubt that. I guess I shouldn't have said anything. Just because it can catch this one example doesn't mean you aren't right. – Jerry Jeremiah Feb 08 '21 at 01:47
  • @JerryJeremiah: It's still good to know that it's not disabled completely with optimizations off, so I am glad you pointed it out. – Nate Eldredge Feb 08 '21 at 01:49
  • @NateEldredge https://gcc.gnu.org/wiki/Better_Uninitialized_Warnings says "GCC tries to detect some instances [of uninitialised variables] by using the information gathered by optimisers. ... There are a number of perceived shortcomings in current implementation. First, it only works when optimisation is enabled through -O1, -O2 or -O3. Second, the set of false positives or negatives varies according to the optimisations enabled." so you can't count on it always saving you when you really need it. – Jerry Jeremiah Feb 08 '21 at 01:57
  • And this is useful: https://stackoverflow.com/a/14132910/2193968 – Jerry Jeremiah Feb 08 '21 at 01:59

2 Answers2

0

Turn on compiler optimizations. Quite a few compilers default to optimizations nearly off, as in they compile each statement independently. In such an operating mode they can't tell if a pointer is uninitialized. This does make for easier debugging in general because single step and inspect and modify work the way you would expect, but it does come at a price. If you don't catch it sooner you'll catch it when you compile with optimizations on.

Also turn on compiler warnings while you're at it. You usually want -Wall.

Too bad the compiler can't stuff uninitialized pointers with fault-worthy garbage when making debug builds. It would save time.

Joshua
  • 40,822
  • 8
  • 72
  • 132
0

You need to use -Wall -Wextra instead of only -Wall to get "reasonable" warnings in GCC

  • -Wextra
    • This enables some extra warning flags that are not enabled by -Wall. (This option used to be called -W. The older name is still supported, but the newer name is more descriptive.)

      • ...
      • -Wuninitialized

https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html

phuclv
  • 37,963
  • 15
  • 156
  • 475