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.

user3124812
  • 1,861
  • 3
  • 18
  • 39
  • Possible duplicate of [Disable GCC "may be used uninitialized" on a particular variable](https://stackoverflow.com/questions/5080848/disable-gcc-may-be-used-uninitialized-on-a-particular-variable) – Swordfish Nov 21 '18 at 04: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. – user3124812 Nov 21 '18 at 04:39
  • [Cannot reproduce](https://godbolt.org/z/Bv5ZvB). Please provide a [mcve]. – Passer By Nov 21 '18 at 04: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... – user3124812 Nov 21 '18 at 05:55
  • http://wandbox.org. Your question is *required* to make sense without an external link, please [edit] the question to be a [mcve]. – Passer By Nov 21 '18 at 06:12
  • @PasserBy, example edited. I could not reproduce an error on wandbox.org with 'gcc 8.2.0' :( – user3124812 Nov 22 '18 at 00:46

0 Answers0