1

There is a part of code:

void func0(xxx* obj)
{
    void* ptr;
    size_t size;
    obj->getBuffer(ptr, size); // Init ptr & size

    while(size) // ERROR: may be used uninitialized
    {
        // processing buffer
    }
}

'xxx.h'

#include <cstddef>

class xxx
{
 public:
    xxx();
    xxx(void* p, size_t s);
    void getBuf(void*& p, size_t& s);
 private:
    void* pp;
    size_t ss;
};


'xxx.cpp'

#include "xxx.h"

xxx::xxx()
{
    pp = (void*)0xFFFF;
    ss = 0x100;
}

xxx::xxx(void* p, size_t s)
{
    pp = p;
    ss = s;
}

void xxx::getBuf(void*& p, size_t& s)
{
    p = pp;
    s = ss;
}


GCC generates 'may be used uninitialized in this function' error.

GCC version: 'aarch64-elf-g++ (GNU Toolchain for the A-profile Architecture 8.2-2018-08 (arm-rel-8.23)) 8.2.1 20180802'


Is there a way to hint compiler that variables are actually inited without explicitly assign them with some default values in declaration?

PS: Error comes with a new version of toolchain.
please no 'rewrite everything' / 'bad design' advices.

6
  • Possible duplicate of Disable GCC "may be used uninitialized" on a particular variable
    – Swordfish
    Nov 21, 2018 at 4:16
  • not exactly, my question. I'm interesting in changing getBuffer attributes. For example, for now getBuffer assigns variables always. Lets say it's modified and skips initialization of a parameter. "-Wuninitialized" would suppress a warning, while variable is indeed uninited. Additional inconvenience of this way that getBuffer is used in different files, all these files should be modified. Changing multiple files, rather than only one, is exactly what I want to avoid. Nov 21, 2018 at 4:39
  • Cannot reproduce. Please provide a minimal reproducible example.
    – Passer By
    Nov 21, 2018 at 4:50
  • @PasserBy Those pieces of code should be in different compilation units. Is there an online tool which allow to create a few files? 'godbolt' and similar work with only one 'main' file and compiler effectively inline everything... Nov 21, 2018 at 5:55
  • wandbox.org. Your question is required to make sense without an external link, please edit the question to be a minimal reproducible example.
    – Passer By
    Nov 21, 2018 at 6:12

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.