Home > other >  Why is my static array[] of my classes failing
Why is my static array[] of my classes failing

Time:12-06

I can certainly solve this half a dozen ways but I am curious why what I have coded is not working. (VS 2010 Pro C )

I have a small class

class Protocol_element
{
public:
    const char *Argument;
    int EnumID;
    int TrueEnum;
    int FalseEnum;
    bool IsEnabled;
    ...

It has an explicit ctor:

    Protocol_element(const char * arg, int id, int tru, int fals, bool isEnab)
    {
        Argument = arg;
        EnumID = id;
        TrueEnum = tru;
        FalseEnum = fals;
        IsEnabled = isEnab;
    {

Then I have an array of addresses of these class elements that is declared static

Protocol_element *Parms::Protocol[] = {
    &Protocol_element("SSLV2", GSK_PROTOCOL_SSLV2, GSK_PROTOCOL_SSLV2_ON, GSK_PROTOCOL_SSLV2_OFF, true),
    &Protocol_element("SSLV3", GSK_PROTOCOL_SSLV3, GSK_PROTOCOL_SSLV3_ON, GSK_PROTOCOL_SSLV3_OFF, true),
    &Protocol_element("TLSV1", GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_ON, GSK_PROTOCOL_TLSV1_OFF, true),
    &Protocol_element("TLSV1.1", GSK_PROTOCOL_TLSV1_1, GSK_PROTOCOL_TLSV1_1_ON, GSK_PROTOCOL_TLSV1_1_OFF, false),
    &Protocol_element("TLSV1.2", GSK_PROTOCOL_TLSV1_2, GSK_PROTOCOL_TLSV1_2_ON, GSK_PROTOCOL_TLSV1_2_OFF, false),
    &Protocol_element("TLSV1.3", GSK_PROTOCOL_TLSV1_3, GSK_PROTOCOL_TLSV1_3_ON, GSK_PROTOCOL_TLSV1_3_OFF, false),
    nullptr
};

I can trace with the debugger through the constructors. I can see my whole array get built just as I would expect. But if I breakpoint on int main() the array now is uninitialized storage.

Why? Why did my array "disappear"?

CodePudding user response:

Okay, thanks. I was not sure, but when it compiled, I expected that it might work. As I said, I can think of a dozen ways to solve the problem, and what I have done now, and works, is

Protocol_element Parms::Protocol[] = {
    { "SSLV2", GSK_PROTOCOL_SSLV2, GSK_PROTOCOL_SSLV2_ON, GSK_PROTOCOL_SSLV2_OFF, true },
    { "SSLV3", GSK_PROTOCOL_SSLV3, GSK_PROTOCOL_SSLV3_ON, GSK_PROTOCOL_SSLV3_OFF, true },
    { "TLSV1", GSK_PROTOCOL_TLSV1, GSK_PROTOCOL_TLSV1_ON, GSK_PROTOCOL_TLSV1_OFF, true },           // TODO: Document
    { "TLSV1.1", GSK_PROTOCOL_TLSV1_1, GSK_PROTOCOL_TLSV1_1_ON, GSK_PROTOCOL_TLSV1_1_OFF, false },
    { "TLSV1.2", GSK_PROTOCOL_TLSV1_2, GSK_PROTOCOL_TLSV1_2_ON, GSK_PROTOCOL_TLSV1_2_OFF, false },
    { "TLSV1.3", GSK_PROTOCOL_TLSV1_3, GSK_PROTOCOL_TLSV1_3_ON, GSK_PROTOCOL_TLSV1_3_OFF, false },
    nullptr
};

CodePudding user response:

The program is invalid. The address-of operator requires an lvalue.

You are however using a non-standard language extension of a very buggy and outdated compiler. This extension prevents the compiler from yelling at you and refusing to compile the program like the standard (any year) requires. Unfortunately it does not cause the compiler to emit meaningful code. So garbage in, garbage out.

You should upgrade to the latest version of MSVC and use the latest standard (the /std:c latest flag), or at the very least disable the non-standard extensions with the /Za flag. I cannot guarantee it will help specifically with VS 2010 though, but it does help with modern versions of MSVC.

  • Related