[05/22] Replace C-style initialization with shorter C++-style

Submitted by Christophe de Dinechin on Feb. 28, 2018, 3:43 p.m.

Details

Message ID 20180228154325.25791-6-christophe@dinechin.org
State New
Headers show
Series "streaming-agent: C++ refactoring" ( rev: 2 1 ) in Spice

Not browsing as part of any series.

Commit Message

Christophe de Dinechin Feb. 28, 2018, 3:43 p.m.
From: Christophe de Dinechin <dinechin@redhat.com>

The proper C++ equivalent of
    struct foo x;
    memset(&x, 0, sizeof(x));
is
    struct foo x = {};

Not only is it shorter, it is also safer for any non-POD type, since
it will not overwrite vtable pointers, incorrectly zero
pointer-to-member fields, or clobber default initializations if any
was specified in the struct/class. So it is overall a good habit to
take to avoid mistakes if the C style was copy-pasted for C++ structs.

Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>
---
 src/spice-streaming-agent.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
index 7304576..acae939 100644
--- a/src/spice-streaming-agent.cpp
+++ b/src/spice-streaming-agent.cpp
@@ -291,9 +291,7 @@  static void handle_interrupt(int intr)
 
 static void register_interrupts(void)
 {
-    struct sigaction sa;
-
-    memset(&sa, 0, sizeof(sa));
+    struct sigaction sa = { };
     sa.sa_handler = handle_interrupt;
     if ((sigaction(SIGINT, &sa, NULL) != 0) &&
         (sigaction(SIGTERM, &sa, NULL) != 0)) {

Comments

> 
> From: Christophe de Dinechin <dinechin@redhat.com>
> 
> The proper C++ equivalent of
>     struct foo x;
>     memset(&x, 0, sizeof(x));
> is
>     struct foo x = {};
> 
> Not only is it shorter, it is also safer for any non-POD type, since
> it will not overwrite vtable pointers, incorrectly zero
> pointer-to-member fields, or clobber default initializations if any
> was specified in the struct/class. So it is overall a good habit to
> take to avoid mistakes if the C style was copy-pasted for C++ structs.
> 
> Signed-off-by: Christophe de Dinechin <dinechin@redhat.com>

Acked-by: Frediano Ziglio <fziglio@redhat.com>

Frediano

> ---
>  src/spice-streaming-agent.cpp | 4 +---
>  1 file changed, 1 insertion(+), 3 deletions(-)
> 
> diff --git a/src/spice-streaming-agent.cpp b/src/spice-streaming-agent.cpp
> index 7304576..acae939 100644
> --- a/src/spice-streaming-agent.cpp
> +++ b/src/spice-streaming-agent.cpp
> @@ -291,9 +291,7 @@ static void handle_interrupt(int intr)
>  
>  static void register_interrupts(void)
>  {
> -    struct sigaction sa;
> -
> -    memset(&sa, 0, sizeof(sa));
> +    struct sigaction sa = { };
>      sa.sa_handler = handle_interrupt;
>      if ((sigaction(SIGINT, &sa, NULL) != 0) &&
>          (sigaction(SIGTERM, &sa, NULL) != 0)) {