GCC warning to identify copy structures containing pointers

Is there a GCC warning warning when I try to copy structures containing pointers with an assignment operator, instead of deep copy?

+4
source share
2 answers

The answer is no. See the gcc options list.

On the same page warning messages:

Warnings are diagnostic messages that report constructs that are inherently faulty, but are risky or suggest that there may have been a mistake.

And a shallow copy instead of a deep copy is neither risky nor erroneous, as this may be the intended behavior. Therefore, there is no reason for the existence of such a warning option.

What you might need is a static analyzer such as clang one , although as far as I know, it does not offer such functionality.

+4
source

I remember exactly such a warning with -Weffc++

Of course, you should be ready to compile in C ++ mode. (See below)

Edit I tested this: Unfortunately, this will not warn about POD types (i.e.) C. Here is a test:

 struct HasPointer { int* resource; HasPointer() {}; ~HasPointer() {}; }; 

Compiled with

 E:\mingw64>g++ test.c -Weffc++ 

Outputs

 test.c:1:8: warning: 'struct HasPointer' has pointer data members [-Weffc++] struct HasPointer ^ test.c:1:8: warning: but does not override 'HasPointer(const HasPointer&)' [-Weffc++] test.c:1:8: warning: or 'operator=(const HasPointer&)' [-Weffc++] test.c: In constructor 'HasPointer::HasPointer()': 

But exiting ctor / dtor, the warning is not even selected, therefore this option does not work for your code, even in C ++ compilation mode .


Compiling C code in C ++ mode:

(Use extern "C" ) to ensure binary compatibility. It is usually as simple as

 extern "C" { # include "my.h" # include "stuff.h" // ... } 
+1
source

All Articles