Why need static member variables?

Discussion in 'C++' started by Allen, Jul 4, 2006.

  1. Allen

    Allen Guest

    In our project, there is a strange problem. Please see the following
    codes.

    ////////////////////////////////////////////////////////////////////////
    /// SSControl.h

    #pragma pack(1)
    typedef struct {
    ...
    } Origin;

    typedef struct {
    ...
    } OPER_RESULT;

    typedef struct {
    ...
    } OPER_CTRL;

    #pragma pack()

    class CControl
    {
    public:
    CControl();
    ~CControl(){};

    // member function declarations
    ......

    private:
    char appMsgBuf[10240];
    char szMTreeBuf[4096];
    char ctlRef[256];
    char ctlRefFC[256];
    // some other member variables
    CMTree tree;
    ......
    };


    ////////////////////////////////////////////////////////////////////////////////
    /// SSControl.cpp

    CControl::CControl(): tree((unsigned char *)szMTreeBuf,
    SSCONTROL_BUF_SIZE)
    {
    }

    // some other member function implementations

    As the codes, the CMTree object tree cannot be used as if it was not
    initialized to
    assign the buffer szMTreeBuf. Then I declare char appMsgBuf[10240],
    char szMTreeBuf[4096], char ctlRef[256], char ctlRefFC[256] to be
    static, it does work!

    And in another CServices class delaration and implementation, it is the
    same as
    class CControl except #pragam pack (1) struct definitions.

    Why does class CControl have to declare static buffers?
    Allen, Jul 4, 2006
    #1
    1. Advertising

  2. Allen

    Allen Guest

    CServices class delaration and implementation is exactly the same as
    class CControl except #pragam pack (1) struct definitions.
    Class CServices has 6 char array member variables, and more than
    128kb in total.

    Both class CControl and CServices have only one global object.
    Class CServices work correctly, and class CControl must be declared
    with static char array member variables.

    I cannot understand it, please give me an answer. Thank you!
    Allen, Jul 4, 2006
    #2
    1. Advertising

  3. * Allen:
    > In our project, there is a strange problem. Please see the following
    > codes.


    The code you've shown does not illustrate the problem you describe
    (although it hints in a very vague sort of way, I frame no hypotheses).

    Please see the FAQ on how to post.

    However, some other problems are illustrated:


    > ////////////////////////////////////////////////////////////////////////
    > /// SSControl.h
    >
    > #pragma pack(1)
    > typedef struct {
    > ...
    > } Origin;


    #pragma is compiler-specific.

    typedef for a struct is a C'ism, best avoided; in C++ write just

    struct Origin { ... };


    > typedef struct {
    > ...
    > } OPER_RESULT;


    All uppercase should (preferentially) only be used for macro names.


    > typedef struct {
    > ...
    > } OPER_CTRL;
    >
    > #pragma pack()
    >
    > class CControl
    > {
    > public:
    > CControl();
    > ~CControl(){};
    >
    > // member function declarations
    > ......
    >
    > private:
    > char appMsgBuf[10240];
    > char szMTreeBuf[4096];
    > char ctlRef[256];
    > char ctlRefFC[256];


    These are both unsafe and memory hoggers: use std::string.

    Hungarian notation is just evil, get rid of it.


    > // some other member variables
    > CMTree tree;
    > ......
    > };
    >
    >
    > ////////////////////////////////////////////////////////////////////////////////
    > /// SSControl.cpp
    >
    > CControl::CControl(): tree((unsigned char *)szMTreeBuf,
    > SSCONTROL_BUF_SIZE)
    > {
    > }


    szMTreeBuf is uninitialized at this time.

    It isn't declared as size SSCONTROL_BUF_SIZE, and may have a different size.

    The C style cast is dangerous and should be removed.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 4, 2006
    #3
  4. Allen

    Allen Guest

    Thank you for your reply.

    Our project is involved HW and SW programmers. The HW programmers uses
    VxWorks environment, while SW programmers code in VC, C, C++ and Java.
    So the problem you mentioned is really existed. CControl class is
    written by
    HW programmer.

    To the point, you said szMTreeBuf is uninitialized at this time.
    > > CControl::CControl(): tree((unsigned char *)szMTreeBuf,
    > > SSCONTROL_BUF_SIZE)
    > > {
    > > }


    Why is ok in my CServices class? It is exactly the same.
    Allen, Jul 4, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Dehmann
    Replies:
    5
    Views:
    2,670
    red floyd
    Jul 1, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    855
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,744
    Smokey Grindel
    Dec 2, 2006
  4. dolphin
    Replies:
    3
    Views:
    1,324
    Pete Becker
    Dec 5, 2007
  5. Replies:
    9
    Views:
    924
Loading...

Share This Page