MT#57408 warning: clearing an object of non-trivial type

This commit fixes this:

     In file included from sip_parser_async.cpp:1:
     sip_parser_async.h: In member function 'void parser_state::reset_hdr_parser()':
     sip_parser_async.h:32:37: warning: 'void* memset(void*, int, size_t)' clearing an object of non-trivial type 'struct sip_header'; use assignment or value-initialization instead [-Wclass-memaccess]
        32 |     memset(&hdr,0,sizeof(sip_header));
           |                                     ^
     In file included from sip_parser_async.h:4,
                      from sip_parser_async.cpp:1:
     parse_header.h:44:8: note: 'struct sip_header' declared here
        44 | struct sip_header
           |        ^~~~~~~~~~

     sip_parser_async.cpp: In function 'int parse_headers_async(parser_state*, char*)':
     sip_parser_async.cpp:215:36: warning: 'void* memset(void*, int, size_t)' clearing an object of non-trivial type 'struct sip_header'; use assignment or value-initialization instead [-Wclass-memaccess]
       215 |     memset(hdr,0,sizeof(sip_header));
           |                                    ^
     In file included from sip_parser_async.h:4,
                      from sip_parser_async.cpp:1:
     parse_header.h:44:8: note: 'struct sip_header' declared here
        44 | struct sip_header
           |        ^~~~~~~~~~

In general, using `var1 = {};` approach to initialize/clear a struct is
equivalent to using `memset(var1, 0, sizeof(MyOwnStruct1));`.
So there shouldn't be any big risks associated with swapping one for the other.

The `memset()` may be slightly more efficient in terms of performance,
especially when dealing with array of structures, but in this situation
we are dealing with quite a small struct, which represents: a type, a name and a value.

As long as `var1 = {};` approach is supported by our compiler and
we don't need to initialize a large array of structures (in our case it isn't),
there shouldn't be any big risks associated with swapping one for the other.

Change-Id: I10570b628d3e5a634d764b83f7558d38ecc6a757
mr11.4.1
Donat Zenichev 3 years ago
parent a18d614504
commit 678730d7b5

@ -212,7 +212,7 @@ int parse_headers_async(parser_state* pst, char* end)
}
// reset header struct
memset(hdr,0,sizeof(sip_header));
hdr = {}; //memset(hdr, 0, sizeof(sip_header));
st = 0;
saved_st = 0;
pst->beg = c;

@ -29,7 +29,7 @@ struct parser_state
}
void reset_hdr_parser() {
memset(&hdr,0,sizeof(sip_header));
hdr = {}; //memset(&hdr, 0, sizeof(sip_header));
st = saved_st = 0;
beg = c;
}

Loading…
Cancel
Save