#include <string.h>
#include <stdio.h>
#include <stdlib.h>
class A {
public:
A(char* buf, int len, bool own)
: buf_(own? new char[len_] : buf_)
, len_(len)
, own_(own) {
printf("buf=%p, buf_=%p\n", buf, buf_);
if(own_) memcpy(buf_, buf, len);
}
~A() {
printf("%02x\n", buf_[0]);
if(own_) delete[] buf_;
}
private:
char* const buf_;
int const len_;
bool const own_;
};
int main() {
char buf[100] = {0};
A a(buf, (int)sizeof(buf), true);
return 0;
}
obviously line:
buf_ (native? new char [len_]: buf _)
use the uninitialized value len_ and buf _.
I will compile it
g ++ a.cpp -Wall -Wextra -Wunused-but-set-parameter -Winit-self -Wmaybe-uninitialized
What confuses me a lot is that gcc does not report any warnings about uninitialized member buf_and len_.
Which gcc option might trigger a warning in this scenario?
Update:
g ++ - 6 does not warn, g ++ - 7 -O1 warns about len_, g ++ - 8 -O0 and clang ++ warn about all len_ and buf_. - Mark Gliss
source
share