From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by sourceware.org (Postfix) with ESMTPS id 9D9893858002 for ; Wed, 22 Jun 2022 11:29:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9D9893858002 Received: by mail-lj1-x22f.google.com with SMTP id e4so18982489ljl.1 for ; Wed, 22 Jun 2022 04:29:10 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to:cc; bh=jllhnjApNsm46EHMlAZhFjbhDewZZYyyOEPXmOX7pBM=; b=zTUmi4o/vBp0mQ5O0o2rxsxZ/q/rVLXfXJt25Nn2HQdTZmZg3/U+Eyx8lu/9iPLsKW 11Ho2NHtx6GOAr/zpeQhPFb7Twf9xz4ZOTjg8GG0GX9gPIBQ3opuQQNJ61fwFR0Pg+mY vmmwSyhSDsIc7Ea0GwlzQ4A2E4+OGcZxBrpv5TFl7xxWXujGRAwscVybElxSEo4NS6OD bWXAnDvj9YAUf494lJnhvbNS0qTS1L81FMRYTuALFBSbQKwUGSLev03ay5dlfnNO/YTP lT26ZPttqO1bCsr8eQaCHv9PjefQoEA3DWgJK1Oa+1SNzH58eIUHJCoF5xv2jb2yfVM2 l6gw== X-Gm-Message-State: AJIora8YjdbxgOCx+IAfxxxNZmnr9hP9Gc/WG0cjJH72Mt5b2aH8R0wO kFkofdZvZliSQUSnYyJCZpvhYBNMc5kFHzSck3eHS3/DhJG+tA== X-Google-Smtp-Source: AGRyM1t8mxdUKJNs8NWMjAUlTjpHwE3AEeAnvs2F5FQuuDz2Os/XIh+CJ44bNSB2WyO3KS48lMmuFzgCcIAldp9vJrI= X-Received: by 2002:a2e:a289:0:b0:258:e917:36a4 with SMTP id k9-20020a2ea289000000b00258e91736a4mr1607184lja.510.1655897348637; Wed, 22 Jun 2022 04:29:08 -0700 (PDT) MIME-Version: 1.0 From: Ronny Meeus Date: Wed, 22 Jun 2022 13:28:56 +0200 Message-ID: Subject: Passing member variable as parameter to the base class constructor: warning expected but not seen To: gcc-help@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Jun 2022 11:29:12 -0000 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 #include #include 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