struct的静态实例中的类类型问题 [英] Problem with class type in static instance of struct
问题描述
我们在一个大型项目中遇到一个奇怪的问题,在一个结构的静态实例中使用字符串类
。
我设法重现完全相同在一个非常短的程序中的问题(参见下面的
)。
当常量SHORT_STRING_SIZE_LIMIT设置为0时,程序的工作原理为
输出字符串+。当它设置为7时,它输出垃圾
(非初始化存储区域的一部分)。
我们在SuSE Linux上使用gcc 3.3版。 />
#include< iostream>
使用命名空间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);
$ b $朋友ostream&运营商LT;< (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;
}否则{
m_uiCapa = m_uiSize;
p =新字符[m_uiCapa + 1];
}
返回p;
}
ostream&运营商LT;< (ostream& os,const String& sStr)
{
os<< sStr.m_p;
返回操作系统;
}
//字符串结尾
class ---- -------------------------------------------------- ---
结构运算符
{
String m_sOperator;
};
static运算符operator = {" +"};
int main()
{
cout<< operators.m_sOperator<< endl;
}
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;
}
推荐答案
Robert Sturzenegger写道:
Robert Sturzenegger wrote:
我们遇到了一个奇怪的问题在一个结构的静态实例中使用字符串类
的大型项目。
我设法在一个非常短的程序中重现完全相同的问题(参见下面的
)。当常量SHORT_STRING_SIZE_LIMIT设置为0时,程序按预期工作,并输出字符串+。当它设置为7时,它输出垃圾
(非初始化存储区域的一部分)。
我们在SuSE Linux上使用gcc 3.3版。
好吧,看起来像编译器中的一个错误。您是否使用调试器找出了什么是错的?
[...]
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.
Well, looks like a bug in the compiler. Did you use the debugger to find
out what''s wrong?
[...]
V
V
" Robert Sturzenegger" <无**** @ nowhere.net>在消息新闻中写道:cq ********** @ news.hispeed.ch ...
"Robert Sturzenegger" <no****@nowhere.net> wrote in message news:cq**********@news.hispeed.ch...
我们在使用字符串类的大型项目中遇到了一个奇怪的问题br />在一个结构的静态实例中。
我设法在一个非常短的程序中重现完全相同的问题(参见下面的
)。
当常量SHORT_STRING_SIZE_LIMIT设置为0时,该程序按预期工作,并输出字符串+。当它设置为7时,它输出垃圾
(非初始化存储区域的一部分)。
我们在SuSE Linux上使用gcc 3.3版。
#include < iostream>
使用命名空间std;
const unsigned int SHORT_STRING_SIZE_LIMIT = 0;
file:// const unsigned int SHORT_STRING_SIZE_LIMIT = 7;
// String
class ------------------------------------------ ----------------------
class String
{
public:
String(const char * s) ;朋友ostream&运营商LT;< (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'';
返回;
}
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;
}否则{
m_uiCapa = m_uiSize;
p = new char [m_uiCapa + 1];
}
返回p;
}
ostream& ;运营商LT;< (ostream& os,const String& sStr)
{
os<< sStr.m_p;
返回操作系统;
}
//字符串结尾
类-------------------- -------------------------------------
struct Operators
{
字符串m_sOperator;
};
静态运算符运算符= {" +"};
int main()
{
cout<< operators.m_sOperator<< endl;
}
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;
file://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;
}
我使用
DigitalMars(已购买CD)使用此程序没有任何问题Win2K
Pro SP4 - 在两种情况下输出都是+
Alan
I had no problems with this program using
DigitalMars (have purchased CD) in Win2K
Pro SP4 - in both cases the output is "+"
Alan
Le Mon,2004年12月27日17:39:24 +0100,Robert Sturzeneggeraécrit*:
Le Mon, 27 Dec 2004 17:39:24 +0100, Robert Sturzenegger a écrit*:
我们在使用字符串类的大型项目中遇到了一个奇怪的问题br />在一个结构的静态实例中。
我设法在一个非常短的程序中重现完全相同的问题(参见下面的
)。
当常量SHORT_STRING_SIZE_LIMIT设置为0时,该程序按预期工作,并输出字符串+。当它设置为7时,它输出垃圾
(非初始化存储区域的一部分)。
我们在SuSE Linux上使用gcc 3.3版。
#include < iostream>
使用命名空间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); <朋友ostream&运营商LT;< (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'';
返回;
}
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;
}否则{
m_uiCapa = m_uiSize;
p = new char [m_uiCapa + 1];
}
返回p;
}
ostream& ;运营商LT;< (ostream& os,const String& sStr)
{
os<< sStr.m_p;
返回操作系统;
}
//字符串结尾
类-------------------- -------------------------------------
struct Operators
{
字符串m_sOperator;
};
静态运算符运算符= {" +"};
int main()
{
cout<< operators.m_sOperator<< endl;
}
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;
}
这对我来说看起来不是编译错误。
你的运算符结构只是没有''使用你的String类的构造函数
providen。
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.
这篇关于struct的静态实例中的类类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!