473,652 Members | 3,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with class type in static instance of struct

We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works as
expected and outputs the string "+". When it is set to 7, it outputs garbage
(a part of a non-initialized storage area).
We are using gcc version 3.3 on SuSE Linux.

#include <iostream>
using namespace std;

const unsigned int SHORT_STRING_SI ZE_LIMIT = 0;
//const unsigned int SHORT_STRING_SI ZE_LIMIT = 7;
// String
class ----------------------------------------------------------------
class String
{
public:
String(const char* s);
friend ostream& operator<< (ostream& os, const String& sStr);

private:
char* allocate();

char m_cShortString[SHORT_STRING_SI ZE_LIMIT + 1];
unsigned int m_uiCapa;
unsigned int m_uiSize;
char* m_p;
};

String::String( const char* s)
{
if (s == NULL) {
m_p = allocate();
m_p[0] = '\0';
return;
}
m_uiSize = strlen(s);
m_p = allocate();
strcpy(m_p, s);
}

char* String::allocat e()
{
char* p;
if (m_uiSize <= SHORT_STRING_SI ZE_LIMIT) {
m_uiCapa = SHORT_STRING_SI ZE_LIMIT;
p = m_cShortString;
} else {
m_uiCapa = m_uiSize;
p = new char[m_uiCapa + 1];
}
return p;
}

ostream& operator<< (ostream& os, const String& sStr)
{
os << sStr.m_p;
return os;
}
// End of String
class ---------------------------------------------------------
struct Operators
{
String m_sOperator;
};
static Operators operators= {"+"};
int main()
{
cout << operators.m_sOp erator << endl;
}
Jul 22 '05 #1
11 1679
Robert Sturzenegger wrote:
We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works as
expected and outputs the string "+". When it is set to 7, it outputs garbage
(a part of a non-initialized storage area).
We are using gcc version 3.3 on SuSE Linux.
Well, looks like a bug in the compiler. Did you use the debugger to find
out what's wrong?
[...]


V
Jul 22 '05 #2

"Robert Sturzenegger" <no****@nowhere .net> wrote in message news:cq******** **@news.hispeed .ch...
We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works as
expected and outputs the string "+". When it is set to 7, it outputs garbage
(a part of a non-initialized storage area).
We are using gcc version 3.3 on SuSE Linux.

#include <iostream>
using namespace std;

const unsigned int SHORT_STRING_SI ZE_LIMIT = 0;
file://const unsigned int SHORT_STRING_SI ZE_LIMIT = 7;
// String
class ----------------------------------------------------------------
class String
{
public:
String(const char* s);
friend ostream& operator<< (ostream& os, const String& sStr);

private:
char* allocate();

char m_cShortString[SHORT_STRING_SI ZE_LIMIT + 1];
unsigned int m_uiCapa;
unsigned int m_uiSize;
char* m_p;
};

String::String( const char* s)
{
if (s == NULL) {
m_p = allocate();
m_p[0] = '\0';
return;
}
m_uiSize = strlen(s);
m_p = allocate();
strcpy(m_p, s);
}

char* String::allocat e()
{
char* p;
if (m_uiSize <= SHORT_STRING_SI ZE_LIMIT) {
m_uiCapa = SHORT_STRING_SI ZE_LIMIT;
p = m_cShortString;
} else {
m_uiCapa = m_uiSize;
p = new char[m_uiCapa + 1];
}
return p;
}

ostream& operator<< (ostream& os, const String& sStr)
{
os << sStr.m_p;
return os;
}
// End of String
class ---------------------------------------------------------
struct Operators
{
String m_sOperator;
};
static Operators operators= {"+"};
int main()
{
cout << operators.m_sOp erator << endl;
}


I had no problems with this program using
DigitalMars (have purchased CD) in Win2K
Pro SP4 - in both cases the output is "+"

Alan
Jul 22 '05 #3
jd
Le Mon, 27 Dec 2004 17:39:24 +0100, Robert Sturzenegger a écrit*:
We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works as
expected and outputs the string "+". When it is set to 7, it outputs garbage
(a part of a non-initialized storage area).
We are using gcc version 3.3 on SuSE Linux.

#include <iostream>
using namespace std;

const unsigned int SHORT_STRING_SI ZE_LIMIT = 0;
//const unsigned int SHORT_STRING_SI ZE_LIMIT = 7;
// String
class ----------------------------------------------------------------
class String
{
public:
String(const char* s);
friend ostream& operator<< (ostream& os, const String& sStr);

private:
char* allocate();

char m_cShortString[SHORT_STRING_SI ZE_LIMIT + 1];
unsigned int m_uiCapa;
unsigned int m_uiSize;
char* m_p;
};

String::String( const char* s)
{
if (s == NULL) {
m_p = allocate();
m_p[0] = '\0';
return;
}
m_uiSize = strlen(s);
m_p = allocate();
strcpy(m_p, s);
}

char* String::allocat e()
{
char* p;
if (m_uiSize <= SHORT_STRING_SI ZE_LIMIT) {
m_uiCapa = SHORT_STRING_SI ZE_LIMIT;
p = m_cShortString;
} else {
m_uiCapa = m_uiSize;
p = new char[m_uiCapa + 1];
}
return p;
}

ostream& operator<< (ostream& os, const String& sStr)
{
os << sStr.m_p;
return os;
}
// End of String
class ---------------------------------------------------------
struct Operators
{
String m_sOperator;
};
static Operators operators= {"+"};
int main()
{
cout << operators.m_sOp erator << endl;
}


This doesn't look a compiler error for me.
Your Operators struct simply doesn't use the constructors you have
providen for your String class.

Jul 22 '05 #4
Robert Sturzenegger wrote:
We had a strange problem in a large project with the use of a string class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see
below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works as


Your class needs a copy constructor (and a assignment operator and a destructor,
and perhaps even a default constructor).

The problem is that when the limit is set to zero, the implicitly defined
copy constructor copies the pointer but since it points to memory you never
free, the thing works OK. In your case where you have the small string
support, the pointer is copied, but it points to the array of a temporary
object whose lifetime expires (and obviously gets written over).

Jul 22 '05 #5
Muk
strcpy(m_p, s);

Before you do this make sure the src string s is null terminated (\0).
In your case it looks like it is not ("+"). Now when
SHORT_STRING_SI ZE_LIMIT = 7, you are copying into char
m_cShortString[8]; Since the dest array m_cShortString may not have \0
at each index, the string is basically not null terminated. This may
cause os << sStr.m_p to print vague results since it does not know the
end of the array (no \0 at end). Make sure all your arrays and strings
are null terminated, especially when using C style functions like
strcpy. When SHORT_STRING_SI ZE_LIMIT = 0 you are creating new
allocation using new char[m_uiCapa + 1] and it looks like the resultant
memory had nulls in it and hence it worked.

Jul 22 '05 #6
"Muk" <rs******@hotma il.com> wrote...
strcpy(m_p, s);

Before you do this make sure the src string s is null terminated (\0).
In your case it looks like it is not ("+"). [...]


Every string literal is null-terminated, see 2.13.4p4.
Jul 22 '05 #7

"Ron Natalie" <ro*@sensor.com > wrote in message
news:41******** *************** @news.newshosti ng.com...
Robert Sturzenegger wrote:
We had a strange problem in a large project with the use of a string
class
in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program
(see
below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works
as


Your class needs a copy constructor (and a assignment operator and a
destructor,
and perhaps even a default constructor).

The problem is that when the limit is set to zero, the implicitly defined
copy constructor copies the pointer but since it points to memory you
never
free, the thing works OK. In your case where you have the small string
support, the pointer is copied, but it points to the array of a temporary
object whose lifetime expires (and obviously gets written over).


What I posted, is only a small extract of the class. As a matter of course
the class does have a default ctor, several other ctors, a copy ctor and a
dtor. But these do not affect the behaviour in question and that's why I
leaved them out.

Jul 22 '05 #8

"jd" <zi******@free. fr> schrieb im Newsbeitrag
news:pa******** *************** *****@free.fr.. .
Le Mon, 27 Dec 2004 17:39:24 +0100, Robert Sturzenegger a écrit :
We had a strange problem in a large project with the use of a string class in a static instance of a struct.
I managed to reproduce the exactly same problem in a very short program (see below).
When the constant SHORT_STRING_SI ZE_LIMIT is set to 0, the program works as expected and outputs the string "+". When it is set to 7, it outputs garbage (a part of a non-initialized storage area).
We are using gcc version 3.3 on SuSE Linux.

#include <iostream>
using namespace std;

const unsigned int SHORT_STRING_SI ZE_LIMIT = 0;
//const unsigned int SHORT_STRING_SI ZE_LIMIT = 7;
// String
class ----------------------------------------------------------------
class String
{
public:
String(const char* s);
friend ostream& operator<< (ostream& os, const String& sStr);

private:
char* allocate();

char m_cShortString[SHORT_STRING_SI ZE_LIMIT + 1];
unsigned int m_uiCapa;
unsigned int m_uiSize;
char* m_p;
};

String::String( const char* s)
{
if (s == NULL) {
m_p = allocate();
m_p[0] = '\0';
return;
}
m_uiSize = strlen(s);
m_p = allocate();
strcpy(m_p, s);
}

char* String::allocat e()
{
char* p;
if (m_uiSize <= SHORT_STRING_SI ZE_LIMIT) {
m_uiCapa = SHORT_STRING_SI ZE_LIMIT;
p = m_cShortString;
} else {
m_uiCapa = m_uiSize;
p = new char[m_uiCapa + 1];
}
return p;
}

ostream& operator<< (ostream& os, const String& sStr)
{
os << sStr.m_p;
return os;
}
// End of String
class ---------------------------------------------------------
struct Operators
{
String m_sOperator;
};
static Operators operators= {"+"};
int main()
{
cout << operators.m_sOp erator << endl;
}


This doesn't look a compiler error for me.
Your Operators struct simply doesn't use the constructors you have
providen for your String class.


Yes, it does! I also tried with cout in the ctor. The ctor is run in both
cases.
Jul 22 '05 #9
Robert Sturzenegger wrote:


What I posted, is only a small extract of the class. As a matter of course
the class does have a default ctor, several other ctors, a copy ctor and a
dtor. But these do not affect the behaviour in question and that's why I
leaved them out.

Perhaps you should put them back in, because it sure as hell makes the
example WRONG without them.

What does your copy constructor look like?
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8013
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned in "Learning Python" by Mark Lutz and David Ascher. It seems like they are a relatively new feature... It seems to me that any truly OO programming language should support these so I'm sure that Python is no exception, but how can these be...
8
2341
by: Pent | last post by:
Hi All, Why is this code valid? How can the static ctor be used? It doesn't act as class ctor at all. struct A { static A() { } }
2
2174
by: Andrew G. J. Fung | last post by:
Can anyone please explain to me why the code below throws an exception? It seems to occur when deserializing an instance of a subclass, whose base class holds a struct, where said struct holds an instance to a reference type (but not System.String). The commented out code are lines that when enabled (and any conflicting lines disabled) stop the exception from being thrown. Thanks kindly, Andrew.
9
2664
by: thomson | last post by:
Hi all, Would you please explain me where will be the heap stored if it is declared inside the Class, As class is a reference type, so it gets stored on the heap, but struct is a value type-stored on the stack Regards thomson
2
4442
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
4
3503
by: PSL | last post by:
Hi, I am going through the training C# For Programmers made by TestOut and like the presentation. The only problem is that this tutorial has a nastie bug that renders this software practically useless. I've tried using TestOut's support form to submit the problem but each time I submit I get a "ERROR MESSAGE: Could not modify CSV file.". My problem is that after I run a simulation, the simualtion report comes up. One section of this...
8
8921
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it. Like, for instance the Objective-C method: +(void)initialize Which has the following characteristics: It is guaranteed to be run
6
3263
by: per9000 | last post by:
An interesting/annoying problem. I created a small example to provoke an exception I keep getting. Basically I have a C-struct (Container) with a function-pointer in it. I perform repeated calls to the function in the container. I allocate (and free in c) arrays to get garbage collection in both C and C#. After a few seconds I get an exception. I assume GarbageCollector moves the delegate (or collects is) and when I don't find it in C...
7
2157
by: pallav | last post by:
I'm having some trouble with my copy constructor. I've tried using gdb to find the bug, but it seg faults in the destructor. I'm not able to see what I'm doing wrong. Since I'm using pointers, I need deep copy and I believe I'm doing that in my constructors. Can someone help me see what I'm missing out? Here is my code. typedef boost::shared_ptr<FactorFactorPtr; enum FactorTypeT
0
8279
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8811
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8467
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8589
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7302
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4291
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2703
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1914
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.