how to initialize a char array?

前端 未结 8 1854
名媛妹妹
名媛妹妹 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.

提交回复
热议问题