bratao
bratao

Reputation: 2080

Initializing Class with {0}

I´m running a legacy c++ MFC software into DevPartner Studio and they complain a lot about these lines:

PARAFORMAT pf = {0};

Where PARAFORMAT is a Class. Any class that is initialized this way, the DevPartner popups this error:

Write Overrun: Memory write to 0x002B9D18 (152) overruns local variable pf 0x002B9D14 (156) in function CServerWnd::OnInitDialog; parameter 1 in call to memset.

Looks obvious to me that is overriding the right variable. This construction is right ? I can safely ignore the warnings ?

EDIT: Here is PARAFORMAT

typedef struct _paraformat
{
    UINT    cbSize;
    DWORD   dwMask;
    WORD    wNumbering;
    WORD    wEffects;
    LONG    dxStartIndent;
    LONG    dxRightIndent;
    LONG    dxOffset;
    WORD    wAlignment;
    SHORT   cTabCount;
    LONG    rgxTabs[MAX_TAB_STOPS];
} PARAFORMAT;

Upvotes: 1

Views: 377

Answers (2)

Erik
Erik

Reputation: 91300

Without seeing PARAFORMAT, here's my take on what happens.

PARAFORMAT is a POD struct/class with a size of 156 bytes. Your initializer syntax forces the compiler to generate code to initialize all members of the class. All members of the class will effectively have 0 written when initialized.

So, the compiler sets the initial 0 that you specified in the initializer-list, and then is clever and memset's the rest of the struct to 0 instead of setting each member variable individually.

This memset call is caught by a check in DevPartner - it doesn't like that something memsets your local variable without you calling memset, and generates a warning.

So, pending more information, I'd say this is a broken check in DevPartner, which you can ignore. I'll reserve the right to change my mind if/when you provide the rest of the info we need :)

EDIT:

After seeing the PARAFORMAT struct I believe that the above description is correct.

Upvotes: 2

AndersK
AndersK

Reputation: 36102

generally speaking: you can only initialize a POD like that, a class is initialized by its constructor.

Upvotes: 1

Related Questions