代码之家  ›  专栏  ›  技术社区  ›  Mahmoud Al-Qudsi

初始化对象时,0是什么意思?

c c++
  •  247
  • Mahmoud Al-Qudsi  · 技术社区  · 16 年前

    什么时候? {0} 用于初始化对象,这意味着什么?我找不到任何关于 { 0 } 任何地方,而且由于大括号的缘故,谷歌搜索没有帮助。

    示例代码:

    SHELLEXECUTEINFO sexi = {0}; // what does this do?
    sexi.cbSize = sizeof(SHELLEXECUTEINFO);
    sexi.hwnd = NULL;
    sexi.fMask = SEE_MASK_NOCLOSEPROCESS;
    sexi.lpFile = lpFile.c_str();
    sexi.lpParameters = args;
    sexi.nShow = nShow;
    
    if(ShellExecuteEx(&sexi))
    {
        DWORD wait = WaitForSingleObject(sexi.hProcess, INFINITE);
        if(wait == WAIT_OBJECT_0)
            GetExitCodeProcess(sexi.hProcess, &returnCode);
    }
    

    如果没有它,上述代码将在运行时崩溃。

    9 回复  |  直到 16 年前
        1
  •  296
  •   Don Neufeld    9 年前

    {0} 0

    struct S { int a; char* b; int c; };
    S ss = { 1, "asdf" };
    

    ss.a 1 ss.b "asdf" ss.c int()

    here

        2
  •  87
  •   Harold Ekstrom    16 年前

    struct foo
    {
        char c;
        int  i;
    };
    
    foo a = {0};
    

    foo a;
    memset(&a,0,sizeof(a));
    

        3
  •  19
  •   dalle    16 年前

    SHELLEXECUTEINFO sexi = {};
    char mytext[100] = {};
    
        4
  •  11
  •   Ziezi sethi    9 年前

    ShellExecuteEx() SHELLEXECUTEINFO

    sexi.lpDirectory

    SHELLEXECUTEINFO sexi = {0};
    

        5
  •  7
  •   Adam Pierce    16 年前

    char mytext[100] = {0};
    
        6
  •  7
  •   Keith Thompson    10 年前

    {0}

    N1570 draft

    0 0.0

    { }

    int arr[2][2] = { { 1, 2 }, {3, 4} };
    
    int arr[2][2] = { 1, 2, 3, 4 };
    

    { { 0 } }

    some_type obj = { 0 };
    

    obj

    sexi.cbSize SHELLEXECUTEINFO { 0 }

    memset

        7
  •  3
  •   µBio    16 年前

        8
  •  2
  •   Ingo Blackman    11 年前

    struct foo bar = { 0 };
    

    struct f {
        int x;
        char a;
    } my_zero_struct;
    
    int main(void)
    {
        return my_zero_struct.x;
    }
    

    gcc -O2 -o check check.c readelf -s check | sort -k 2

    59: 0000000000601018     0 NOTYPE  GLOBAL DEFAULT  ABS __bss_start
    48: 0000000000601018     0 NOTYPE  GLOBAL DEFAULT  ABS _edata
    25: 0000000000601018     0 SECTION LOCAL  DEFAULT   25 
    33: 0000000000601018     1 OBJECT  LOCAL  DEFAULT   25 completed.6531
    34: 0000000000601020     8 OBJECT  LOCAL  DEFAULT   25 dtor_idx.6533
    62: 0000000000601028     8 OBJECT  GLOBAL DEFAULT   25 my_zero_struct
    57: 0000000000601030     0 NOTYPE  GLOBAL DEFAULT  ABS _end
    

    my_zero_struct __bss_start main wikipedia on .bss

    } my_zero_struct = { 0 };
    

    .bss

    memset

        9
  •  -5
  •   nitin prajapati    10 年前