Compilation warning on Fedora release 28 #14640

issue MarkLTZ openend this issue on November 2, 2018
  1. MarkLTZ commented at 1:43 pm on November 2, 2018: none

    Compiling on Fedora release 28 I have the following warning:

     0In file included from bench/prevector.cpp:6:
     1./prevector.h: In instantiation of ‘void prevector<N, T, Size, Diff>::fill(T*, ptrdiff_t) [with unsigned int N = 28; T = nontrivial_t; Size = unsigned int; Diff = int; ptrdiff_t = long int]’:
     2./prevector.h:340:9:   required from ‘void prevector<N, T, Size, Diff>::resize(prevector<N, T, Size, Diff>::size_type) [with unsigned int N = 28; T = nontrivial_t; Size = unsigned int; Diff = int; prevector<N, T, Size, Diff>::size_type = unsigned int]’
     3bench/prevector.cpp:47:13:   required from ‘void PrevectorClear(benchmark::State&) [with T = nontrivial_t]’
     4bench/prevector.cpp:102:1:   required from here
     5./prevector.h:205:21: warning: ‘void* memset(void*, int, size_t)’ clearing an object of non-trivial type ‘struct nontrivial_t’; use assignment or value-initialization instead [-Wclass-memaccess]
     6             ::memset(dst, 0, count * sizeof(T));
     7             ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
     8bench/prevector.cpp:12:8: note: ‘struct nontrivial_t’ declared here
     9 struct nontrivial_t {
    10        ^~~~~~~~~~~~
    

    $ gcc –version gcc (GCC) 8.2.1 20181011 (Red Hat 8.2.1-4) Copyright (C) 2018 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.

    $ ld –version GNU ld version 2.29.1-23.fc28 Copyright (C) 2017 Free Software Foundation, Inc. This program is free software; you may redistribute it under the terms of the GNU General Public License version 3 or (at your option) a later version. This program has absolutely no warranty.

    $ g++ –version g++ (GCC) 8.2.1 20181011 (Red Hat 8.2.1-4) Copyright (C) 2018 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.

    Could someone help me to solve this warning.

    Thanks

  2. fanquake added the label Linux/Unix on Nov 2, 2018
  3. MarcoFalke commented at 2:05 pm on November 2, 2018: member

    The warning is implemented in the C++ front end and runs without the benefit of any sort of constant propagation or dead code elimination, so there is no way to avoid it in ordinary conditional statements.

    In C++ 17 and later the warning can be avoided by using the if constexpr statement:

    0  if constexpr (std::is_trivial<T>::value)
    1    memset(val, 0, sizeof(T);
    2  else
    3    *val = T();
    

    In older C++ versions it can be avoided by dispatching to different implementation functions at compile time.

    In any event, the preferred way to zero-initialize an object is by making use the appropriate initialization form. It’s safer and easier for compilers to analyze both to detect bugs and to emit efficient code (sometimes more efficient than memset), so I would recommend adopting it in favor or raw memory functions even for trivial types.

    Source: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84656#c1

  4. MarcoFalke closed this on Nov 2, 2018

  5. MarcoFalke locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-26 12:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me