R
Robert Sturzenegger
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_SIZE_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_SIZE_LIMIT = 0;
//const unsigned int SHORT_STRING_SIZE_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_SIZE_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::allocate()
{
char* p;
if (m_uiSize <= SHORT_STRING_SIZE_LIMIT) {
m_uiCapa = SHORT_STRING_SIZE_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_sOperator << endl;
}
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_SIZE_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_SIZE_LIMIT = 0;
//const unsigned int SHORT_STRING_SIZE_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_SIZE_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::allocate()
{
char* p;
if (m_uiSize <= SHORT_STRING_SIZE_LIMIT) {
m_uiCapa = SHORT_STRING_SIZE_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_sOperator << endl;
}