1

Consider this simple class

class A
{
 public:
    int a;
};

As you can see int a has never been initialized. I would like to get an error or at least warning in such cases. I'm using Xcode with GNU++11[-std=gnu++11] C++ dialect. Is it even possible?

UPDATE:

First of all I created an instance of A and used its a member and still received no warning:

A a;

std::cout << a.a;

Also I looked into Easy way to find initialized member variables, it suggests adding -Weffc++ flag. I tried to add it via custom compiler flags in the Xcode Build Settings (is it a wrong way to do so? I'm not sure) and it didn't work

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
  • Possible duplicate of [Easy way find uninitialized member variables](https://stackoverflow.com/questions/2099692/easy-way-find-uninitialized-member-variables) – Marek R Jan 03 '19 at 12:00
  • If the member variable is uninitialized or not depends on how you initialize instances of the class. If you use [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization) (as in `A a{123};`) then `a` *will* be initialized. – Some programmer dude Jan 03 '19 at 12:00
  • Since there are no instances of class `A` there is nothing to initialize here. Note that even though `a` is not initialized in class body it does not nessesery mean that it won't be initialized when class instances are created, for example one may write `A aa{3};` – user7860670 Jan 03 '19 at 12:01
  • What VTT said. GCC issues a warning if you try to create an instance of `A` and use `.a` without initializing it first: http://coliru.stacked-crooked.com/a/f2a20e487219a19a – HolyBlackCat Jan 03 '19 at 12:01
  • You're likely using `clang` not `gcc` (`gcc` is often an alias for `clang` on OS X), which will not warn you for this kind of uninitialized member, even with all warning flags turned on. – Holt Jan 03 '19 at 12:57
  • @Holt Yes, the compiler is Clang, but the language dialect is GNU++11[-std=gnu++11] (I'm not sure how it all fits together). So you mean there's no option for me to achieve that? – Andrey Chernukha Jan 03 '19 at 13:01
  • @AndreyChernukha The language dialect only says that you are compiling with `-std=gnu++11`. Maybe it add other things to XCode but not to the clang compilation, so I don't think there is a way to get a warning for this using `clang`. – Holt Jan 03 '19 at 13:02
  • @Holt thanks a lot! – Andrey Chernukha Jan 03 '19 at 13:03
  • @Holt Yet Clang does have -Wunitialized in its warning list. https://clang.llvm.org/docs/DiagnosticsReference.html#wuninitialized. Do I understand right that it cannot be enabled for c++? – Andrey Chernukha Jan 03 '19 at 13:08
  • @AndreyChernukha `-Wunitialized` will warn you about unitialized **variables**, e.g., `int c; std::cout << c;`, but not about unitialized **members** as in your example. – Holt Jan 03 '19 at 13:10

1 Answers1

1

GCC provides -Wuninitialized compiler option for warning about uninitialized variables.

This works for your case. See demo here.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • Thanks for the response, but now my question is how to enable it in Xcode? I added this option through custom compiler flags to Other C++ flags and Other Warning Flags and it didn't work – Andrey Chernukha Jan 03 '19 at 12:18
  • I am not familiar with Xcode. What could be happening is that this particular option is somehow superseded by some other option. Can you try to remove all other options except `-Wuninitialized` and see if it works? – P.W Jan 03 '19 at 12:20
  • @AndreyChernukha Xcode has an Uninitialized Variables build setting in the Apple Clang Warnings All languages section. If you start typing the word `uninitialized` in the search field in the build settings editor, you should be able to find it easily. – Swift Dev Journal Jan 03 '19 at 19:37