public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Passing member variable as parameter to the base class constructor: warning expected but not seen
@ 2022-06-22 11:28 Ronny Meeus
  2022-06-22 11:51 ` Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Ronny Meeus @ 2022-06-22 11:28 UTC (permalink / raw)
  To: gcc-help

Hello

I have a small test program (see below) that uses a member variable
(a) of a derived class (A) as a parameter when calling the constructor
of the base class (A_base).
In fact this is "wrong" code since the base class constructor is
called with an un-initialized variable as input.

I would expect that the compiler generates a warning/error for this
since the behavior completely depends on the contents of the memory
where the object was allocated from.
In the example code I use a placement new to actually show that the
value seen in the constructor of the base class is the value I used to
fill the memory with.

I tried it with different gcc versions but none of these are
generating a warning/error for this while other tools like coverity or
sonarqube just report the issue ...
Is it expected that there is no warning generated or do I need to pass
extra compiler options to generate this warning?

I compile the program like this:
$ g++ -Wall -Wextra -std=gnu++11 /tmp/a.cc

And the output it generates:
$ ./a.out
1 a=100
2 a=0


#include <stdio.h>
#include <string.h>
#include <new>

class A_base
{
public:
    A_base(bool a) {
        printf("1 a=%d\n", (int)a);
    }
};

class A: public A_base {
public:
    bool a = false;
    A(): A_base(a) {
        printf("2 a=%d\n", (int)a);
    }
    ~A() {}
};

int main(void)
{
    char mem[10];
    memset(mem,100,sizeof(mem));

    A *b = new ((void*)mem) A();

    return 0;
}

Note: The program basically prints the boolean values as an int to
make it clear in the output that it is just using the memory as is,
without initializing it.

Best regards,
Ronny

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Passing member variable as parameter to the base class constructor: warning expected but not seen
  2022-06-22 11:28 Passing member variable as parameter to the base class constructor: warning expected but not seen Ronny Meeus
@ 2022-06-22 11:51 ` Jonathan Wakely
  2022-06-22 13:08   ` Ronny Meeus
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2022-06-22 11:51 UTC (permalink / raw)
  To: Ronny Meeus; +Cc: gcc-help

On Wed, 22 Jun 2022 at 12:30, Ronny Meeus via Gcc-help
<gcc-help@gcc.gnu.org> wrote:
>
> Hello
>
> I have a small test program (see below) that uses a member variable
> (a) of a derived class (A) as a parameter when calling the constructor
> of the base class (A_base).
> In fact this is "wrong" code since the base class constructor is
> called with an un-initialized variable as input.
>
> I would expect that the compiler generates a warning/error for this
> since the behavior completely depends on the contents of the memory
> where the object was allocated from.
> In the example code I use a placement new to actually show that the
> value seen in the constructor of the base class is the value I used to
> fill the memory with.
>
> I tried it with different gcc versions but none of these are
> generating a warning/error for this while other tools like coverity or
> sonarqube just report the issue ...
> Is it expected that there is no warning generated or do I need to pass
> extra compiler options to generate this warning?
>
> I compile the program like this:
> $ g++ -Wall -Wextra -std=gnu++11 /tmp/a.cc

You didn't say which version of GCC you're using, but GCC 12 warns
about this now:

w.C: In constructor 'A::A()':
w.C:16:17: warning: member 'A::a' is used uninitialized [-Wuninitialized]
  16 |     A(): A_base(a) {
     |                 ^

Older versions do not warn.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Passing member variable as parameter to the base class constructor: warning expected but not seen
  2022-06-22 11:51 ` Jonathan Wakely
@ 2022-06-22 13:08   ` Ronny Meeus
  0 siblings, 0 replies; 3+ messages in thread
From: Ronny Meeus @ 2022-06-22 13:08 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: gcc-help

Op wo 22 jun. 2022 om 13:51 schreef Jonathan Wakely <jwakely.gcc@gmail.com>:
>
> On Wed, 22 Jun 2022 at 12:30, Ronny Meeus via Gcc-help
> <gcc-help@gcc.gnu.org> wrote:
> >
> > Hello
> >
> > I have a small test program (see below) that uses a member variable
> > (a) of a derived class (A) as a parameter when calling the constructor
> > of the base class (A_base).
> > In fact this is "wrong" code since the base class constructor is
> > called with an un-initialized variable as input.
> >
> > I would expect that the compiler generates a warning/error for this
> > since the behavior completely depends on the contents of the memory
> > where the object was allocated from.
> > In the example code I use a placement new to actually show that the
> > value seen in the constructor of the base class is the value I used to
> > fill the memory with.
> >
> > I tried it with different gcc versions but none of these are
> > generating a warning/error for this while other tools like coverity or
> > sonarqube just report the issue ...
> > Is it expected that there is no warning generated or do I need to pass
> > extra compiler options to generate this warning?
> >
> > I compile the program like this:
> > $ g++ -Wall -Wextra -std=gnu++11 /tmp/a.cc
>
> You didn't say which version of GCC you're using, but GCC 12 warns
> about this now:
>
> w.C: In constructor 'A::A()':
> w.C:16:17: warning: member 'A::a' is used uninitialized [-Wuninitialized]
>   16 |     A(): A_base(a) {
>      |                 ^
>
> Older versions do not warn.

Thanks for the feedback.
For completeness I tested with:
10.3.0
7.3.0
4.8.5
 (I know it are rather old versions but we work on embedded targets)

Best regards,
Ronny

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-22 13:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-22 11:28 Passing member variable as parameter to the base class constructor: warning expected but not seen Ronny Meeus
2022-06-22 11:51 ` Jonathan Wakely
2022-06-22 13:08   ` Ronny Meeus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).