它会泄漏吗? [英] will it leak?

查看:73
本文介绍了它会泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

偶然出现了一些疑问。当我用vc7.1编译它并在Windows上运行它时,字符串是bb。在类destructed中构造遵循

类,但是当我使用g ++ / gcc编译它并在ubuntu上运行时,

出现了问题。似乎是字符串 ;当

类破坏时仍然活着。

为什么?这里是代码:

#include< iostream>


使用命名空间std;


A级{

public:

int i;

string a;

bool b;

public:

void SetInt(int iV)

{

i = iV;

}

无效SetBool(bool bV)

{

b = bV;

}

void SetChar(const char * aV)

{

a = aV ;

}


A()

:i(0),

a(" ),

b(假)

{

}

~A()

{

// a.clear();

}

};


const char * msg;

char * cstr;

void foo(A * ca)

{

ca-> SetInt(10);

cstr = new char [14];

if(cstr)

strcpy(cstr," ;你好世界!");

printf(" CSTR:%p \ n&q uot;,cstr);

ca-> SetChar(cstr);

ca-> SetBool(true);

}


void test(const char * str)

{

msg = str;

}

int main()

{

A * ca = new A;

foo(ca);

printf(" INT:%d \ tSTRING:%s \ tBOOL:%s \ n",ca-> i,(ca-> a).data(),

ca-> b?" true":" false");


test((ca-> a).data()) ;

删除cstr;

printf(" DATA:%s \ n",msg);


删除ca ;


printf(" CSTR:%s \ n",cstr);

//删除cstr;

printf(" DATA:%s \ n",msg);

}

Some doubt appeared by chance.When i compiled it with vc7.1 and run it
on windows,the "string" constructed in class destructed follow the
class, but when i compiled it with g++/gcc and run it on ubuntu,
problems came out.It seems that the "string" is still alive when the
class destruct.
why?here is the code:
#include <iostream>

using namespace std;

class A {
public:
int i;
string a;
bool b;
public:
void SetInt(int iV)
{
i = iV;
}
void SetBool(bool bV)
{
b = bV;
}
void SetChar(const char* aV)
{
a = aV;
}

A()
:i(0),
a(""),
b(false)
{
}
~A()
{
// a.clear();
}
};

const char* msg;
char* cstr;
void foo(A* ca)
{
ca->SetInt(10);
cstr = new char[14];
if (cstr)
strcpy(cstr, "hello world!");
printf("CSTR:%p\n", cstr);
ca->SetChar(cstr);
ca->SetBool(true);
}

void test(const char* str)
{
msg = str;
}
int main()
{
A* ca = new A;
foo(ca);
printf("INT:%d\tSTRING:%s\tBOOL:%s\n", ca->i, (ca->a).data(),
ca->b?"true":"false");

test((ca->a).data());
delete cstr;
printf("DATA:%s\n", msg);

delete ca;

printf("CSTR:%s\n", cstr);
// delete cstr;
printf("DATA:%s\n", msg);
}

推荐答案

很晦涩。哪个字符串还活着,你怎么知道它还活着?


ABAI

lylone写道:
very obscure. which string still alive, how do you know it is alive?

ABAI
lylone wrote:

有些怀疑是偶然出现的。当我用vc7.1编译并在windows上运行它时,字符串和字符串在类destructed中构造遵循

类,但是当我使用g ++ / gcc编译它并在ubuntu上运行时,

出现了问题。似乎是字符串 ;当

类破坏时仍然活着。

为什么?这里是代码:

#include< iostream>


使用命名空间std;


A级{

public:

int i;

string a;

bool b;

public:

void SetInt(int iV)

{

i = iV;

}

无效SetBool(bool bV)

{

b = bV;

}

void SetChar(const char * aV)

{

a = aV ;

}


A()

:i(0),

a(" ),

b(假)

{

}

~A()

{

// a.clear();

}

};


const char * msg;

char * cstr;

void foo(A * ca)

{

ca-> SetInt(10);

cstr = new char [14];

if(cstr)

strcpy(cstr,hello world!);

printf(" CSTR:%p \ n",cstr);

ca - > SetChar(cstr);

ca-> SetBool(true);

}


void test(const char * str)

{

msg = str;

}

int main()

{

A * ca =新A;

foo(ca);

printf(" INT:%d \\ \\ tSTRING:%s \ tBOOL:%s \ n",ca-> i,(ca-> a).data(),

ca-> b?" true":" false");


test((ca-> a).data());

delete cstr;

printf(" DATA:%s \ n",msg);


删除ca;


printf( CSTR:%s \ nn,cstr);

//删除cstr;

printf(" DATA:%s \ n",msg);

}
Some doubt appeared by chance.When i compiled it with vc7.1 and run it
on windows,the "string" constructed in class destructed follow the
class, but when i compiled it with g++/gcc and run it on ubuntu,
problems came out.It seems that the "string" is still alive when the
class destruct.
why?here is the code:
#include <iostream>

using namespace std;

class A {
public:
int i;
string a;
bool b;
public:
void SetInt(int iV)
{
i = iV;
}
void SetBool(bool bV)
{
b = bV;
}
void SetChar(const char* aV)
{
a = aV;
}

A()
:i(0),
a(""),
b(false)
{
}
~A()
{
// a.clear();
}
};

const char* msg;
char* cstr;
void foo(A* ca)
{
ca->SetInt(10);
cstr = new char[14];
if (cstr)
strcpy(cstr, "hello world!");
printf("CSTR:%p\n", cstr);
ca->SetChar(cstr);
ca->SetBool(true);
}

void test(const char* str)
{
msg = str;
}
int main()
{
A* ca = new A;
foo(ca);
printf("INT:%d\tSTRING:%s\tBOOL:%s\n", ca->i, (ca->a).data(),
ca->b?"true":"false");

test((ca->a).data());
delete cstr;
printf("DATA:%s\n", msg);

delete ca;

printf("CSTR:%s\n", cstr);
// delete cstr;
printf("DATA:%s\n", msg);
}


对不起我的英文...这里是在Windows上运行的结果和

ubuntu。

当windows:

CSTR: 003708A0

INT:10 STRING:你好世界! BOOL:true

DATA:你好世界!

CSTR:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?o? oz

DATA:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?o?oè?oè?oè?oè?o?o?


当ubuntu:

CSTR:0x804a018

INT:10 STRING:你好世界! BOOL:true

DATA:你好世界!

CSTR:

DATA:你好世界!


在linux上编译并运行时,字符串a和在A级之后有效吗

被破坏,它可以正确打印,为什么?
Sorry for my english...here are the results running on windows and
ubuntu.
when windows:
CSTR:003708A0
INT:10 STRING:hello world! BOOL:true
DATA:hello world!
CSTR:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oz
DATA:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?o è?o?

when ubuntu:
CSTR:0x804a018
INT:10 STRING:hello world! BOOL:true
DATA:hello world!
CSTR:
DATA:hello world!

When compiled and run on linux, the string "a" was valid after class A
was destructed,it could be printed correctly,why?


" lylone" < ly ************ @ gmail.comwrote在

新闻:11 ******************* ***@i42g2000cwa.googlegr psps.com:
"lylone" <ly************@gmail.comwrote in
news:11**********************@i42g2000cwa.googlegr oups.com:

对不起我的英文...这里是在Windows上运行的结果和

ubuntu。

当windows:

CSTR:003708A0

INT:10 STRING:你好世界! BOOL:true

DATA:你好世界!

CSTR:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?o? oz

DATA:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?o?oè?oè?oè?oè?o?o?

当ubuntu:

CSTR:0x804a018

INT:10 STRING:你好世界! BOOL:true

DATA:你好世界!

CSTR:

DATA:你好世界!

编译时在Linux上运行,字符串a在A级后有效

被破坏,可以正确打印,为什么?
Sorry for my english...here are the results running on windows and
ubuntu.
when windows:
CSTR:003708A0
INT:10 STRING:hello world! BOOL:true
DATA:hello world!
CSTR:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oz
DATA:è?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?oè?o è?o?
when ubuntu:
CSTR:0x804a018
INT:10 STRING:hello world! BOOL:true
DATA:hello world!
CSTR:
DATA:hello world!
When compiled and run on linux, the string "a" was valid after class A
was destructed,it could be printed correctly,why?



一些事情:


1)未定义的行为:您使用new []分配给cstr,但使用delete <处理内存的


2)未定义的行为(假设你使用了删除[]):你使用cstr

你已经删除了内存之后的指针。


3)未定义的行为:在执行修改操作后,你使用了从字符串对象获得的指针

该字符串(具体来说:

销毁它)。

在所有三种情况下,字符串a都是(我认为你的意思是:ca-> a

ca被正确分配)在A级之后是_never_有效(好吧,那个类的A的

实例)指的是被毁坏了。只是

碰巧工作了在某些情况下。这是未定义的结果

行为:无论编译器做什么感觉。

A few things:

1) Undefined behaviour: You allocate to cstr with new[], but use delete
to dispose of the memory.

2) Undefined behaviour (assuming you used delete[]): You use the cstr
pointer after you have already deleted the memory.

3) Undefined behaviour: You used a pointer obtained from a string object
after a modifying operation was performed on that string (specifically:
destroying it).
In all three cases, the string "a" (and I presume you meant: ca->a when
ca was properly allocated) was _never_ valid after class A (well, the
instance of class A that ca was pointing to) was destructed. It just
happened to "work" in some cases. That''s the result of Undefined
Behaviour: whatever the compiler feels like doing.


这篇关于它会泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆