C ++では、クラス/構造体は(初期化に関して)同一です。
非POD構造体には、メンバーを初期化できるようにコンストラクターが含まれている場合もあります。
構造体がPODの場合は、初期化子を使用できます。
struct C
{
int x;
int y;
};
C c = {0}; // Zero initialize POD
または、デフォルトのコンストラクターを使用することもできます。
C c = C(); // Zero initialize using default constructor
C c{}; // Latest versions accept this syntax.
C* c = new C(); // Zero initialize a dynamically allocated object.
// Note the difference between the above and the initialize version of the constructor.
// Note: All above comments apply to POD structures.
C c; // members are random
C* c = new C; // members are random (more officially undefined).
valgrindが不満を言っているのは、それがC++の機能だったからだと思います。(C ++がゼロ初期化のデフォルト構造でアップグレードされた時期は正確にはわかりません)。最善の策は、オブジェクトを初期化するコンストラクターを追加することです(構造体はコンストラクターとして許可されています)。
補足として:
多くの初心者はinitを評価しようとします:
C c(); // Unfortunately this is not a variable declaration.
C c{}; // This syntax was added to overcome this confusion.
// The correct way to do this is:
C c = C();
「MostVexingParse」をすばやく検索すると、私よりもわかりやすい説明が得られます。