0

The code

#include <cstdint>
#include <cstring>

int main()
{
        int len = 10;
        uint8_t data[len] = {};
        uint8_t src[len] = { 1, 2, 3 };

        std::memcpy(data, src, len);

        return 0;
}

build with g++-12

➜  test g++ -Wall test.cpp
test.cpp: In function ‘int main()’:
test.cpp:7:30: warning: value computed is not used [-Wunused-value]
    7 |         uint8_t data[len] = {};
      |                              ^
test.cpp:8:38: warning: value computed is not used [-Wunused-value]
    8 |         uint8_t src[len] = { 1, 2, 3 };
      |                                      ^
➜  test g++ --version     
g++ (Ubuntu 12-20220319-1ubuntu1) 12.0.1 20220319 (experimental) [master r12-7719-g8ca61ad148f]
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

build with g++-11

➜  test g++-11 -Wall test.cpp # no errors, no warnings
➜  test g++-11 --version     
g++-11 (Ubuntu 11.2.0-19ubuntu1) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

The data and src is used, but why did the g++-12 report an "unused value" warning, and how should I fix the code so that g++-12 doesn't report "value computed is not used"

  • note that `uint8_t data[len] = {};` is not standard C++. Make `len` const or constexpr. – 463035818_is_not_an_ai Sep 13 '22 at 09:18
  • 5
    not sure if memcopying counts as using, but you arent using the values after the copying, so its just a matter of where to warn, but definitely both arrays could be removed without altering observable beahvior – 463035818_is_not_an_ai Sep 13 '22 at 09:20
  • @463035818_is_not_a_number after I make len as const , the warning is not reported any more but my use case is something like: ``` if(xx) len = 10; if(xxx) len = 20; data[len] = {}; //array init ``` – John Wang Sep 13 '22 at 09:30
  • then what you observed is a good feature (getting a warning) interfering with a somewhat quesitonable non-standard extension (VLAs). Compiler extensions should be used with care. gcc needs some pushing to be actually standard conforming (`-pedantic` et al). See also here: [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – 463035818_is_not_an_ai Sep 13 '22 at 09:34

0 Answers0