By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Why need static member variables?

P: n/a
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?

Jul 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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!

Jul 4 '06 #2

P: n/a
* 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?
Jul 4 '06 #3

P: n/a
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.

Jul 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.