how to initialize a char array?

前端 未结 8 1803
名媛妹妹
名媛妹妹 2020-12-14 14:39
char * msg = new char[65546];

want to initialize to 0 for all of them. what is the best way to do this in C++?

相关标签:
8条回答
  • 2020-12-14 15:17
    char * msg = new char[65546]();
    

    It's known as value-initialisation, and was introduced in C++03. If you happen to find yourself trapped in a previous decade, then you'll need to use std::fill() (or memset() if you want to pretend it's C).

    Note that this won't work for any value other than zero. I think C++0x will offer a way to do that, but I'm a bit behind the times so I can't comment on that.

    UPDATE: it seems my ruminations on the past and future of the language aren't entirely accurate; see the comments for corrections.

    0 讨论(0)
  • 2020-12-14 15:18
    memset(msg, 0, 65546)
    
    0 讨论(0)
提交回复
热议问题