473,480 Members | 4,852 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Why need static member variables?

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
3 2338
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
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4565
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
1
2980
by: Torsten Mueller | last post by:
I have to create a shared library containing C++ classes with static member variables on HPUX with aCC. I can compile and link the library and a test application but when I start the program I get...
3
3948
by: paul.furber | last post by:
Hi all, I have some code which looks a bit like this: #define Offset(m, T) ((size_t)(&((T *)1)->m) - 1) class Point: private: int *x,*y;
2
1847
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
25
5115
by: Sahil Malik [MVP] | last post by:
So here's a rather simple question. Say in an ASP.NET application, I wish to share common constants as static variables in global.asax (I know there's web.config bla bla .. but lets just say I...
3
1238
by: Colin Desmond | last post by:
I have an MFC Extension DLL that I have flicked the /clr switch on and made a few other tweaks so it compiles and runs in a managed MFC applicaiton. The DLL contains a number of classes which...
1
3138
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
6
3859
by: Olumide | last post by:
Hi - I've got a class that contains static member functions alone, all of whose arguments are passed by reference as shown below: class MySpiffyClass{ // no constructor, destructor or...
17
8346
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
5798
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
0
7040
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2994
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2980
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.