Why need static member variables?

A

Allen

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?
 
A

Allen

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!
 
A

Alf P. Steinbach

* 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

Allen

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.
Why is ok in my CServices class? It is exactly the same.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top