删除Vs.删除[] [英] delete Vs. delete []

查看:82
本文介绍了删除Vs.删除[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有一个奇怪的问题。


我有这样的课......


class CMsg

{

public:

void * m_body;


~CMsg()

{

删除m_body;

}

}

在程序执行指针期间不同类型是typecasted

并分配给m_body。一些指针用于单个块(p = new

char),很少是指向数组的指针。 (内存由p = new

char [nSize]分配)。

通过调用
$ b在CMsg的析构函数中发生内存释放$ b删除。此解除分配仅适用于删除单个

内存块。因此,在尝试删除与指向

数组的指针相关的内存时,会抛出异常。


如何识别指向数组的指针?相应地取消分配

调用delete []?

解决方案

* fe ******* @ gmail.com

我的项目中有一个奇怪的问题。 />
我有这样的课......

班CMsg
{
公开:
void * m_body;

~CMsg()
{
删除m_body;
}
}

在程序执行期间,不同类型的指针被强制转换
并分配给m_body。一些指针用于单个块(p = new
char),很少是指向数组的指针。 (内存由p = new
char [nSize]分配。)

内存释放通过调用
delete在CMsg的析构函数中发生。此解除分配仅适用于删除单个内存块。因此,在尝试删除与指向
数组的内存相关的内存时,会抛出异常。

如何识别指向数组的指针?并通过调用delete []相应地取消分配?




最好的策略是使用


std :: string m_body;





std :: vector< char> m_body;


如果字符数组必须是连续的,例如用于连接到C.


如果这对你没有帮助,你可能只需跟踪一个

的额外成员变量, m_body目前是。

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么会这样这么糟糕吗?

A:热门帖子。

问:usenet和电子邮件中最烦人的是什么?


fe*******@gmail.com 写道:

我的项目中有一个奇怪的问题。

我有一个这样的课......

class CMsg
{<公开:
void * m_body;

~CMsg()
{
删除m_body;
}
}

在程序执行期间,不同类型的指针被转换为
并分配给m_body。一些指针用于单个块(p = new
char),很少是指向数组的指针。 (内存由p = new
char [nSize]分配。)

内存释放通过调用
delete在CMsg的析构函数中发生。此解除分配仅适用于删除单个内存块。因此,在尝试删除与指向
数组的内存相关的内存时,会抛出异常。

如何识别指向数组的指针?并通过调用delete []相应地取消分配?




class CMsg

{

public:

bool is_array;

void * m_body;


~CMsg()

{

if(is_array)delete [] m_body;

else delete m_body;

}

};


问候,

Ben


benben发布:

fe*******@gmail.com 写道:

我的项目有一个奇怪的问题。

我有一个这样的课......

class CMsg
{
公众:
void * m_body;

~CMsg()
{
删除m_body;
在程序执行期间,不同类型的指针是
typecasted并分配给m_body。一些指针用于单个块(p = new
char),很少是指向数组的指针。 (内存由p = new
char [nSize]分配。)

内存释放通过调用
delete在CMsg的析构函数中发生。此解除分配仅适用于删除单个内存块。因此,在尝试删除与指向
数组的内存相关的内存时,会抛出异常。

如何识别指向数组的指针?并通过调用delete []相应地解除分配?



类CMsg
{
公开:
bool is_array;
void * m_body;

~CMsg()
{
如果(is_array)删除[] m_body;
否则删除m_body;
}
}

问候,




您是否必须提供删除正确的类型?例如,如果你将
声明为int,那么使用new,然后删除不是类型的论证

" int *",而不是void *"

-Tomás


I have a strange problem in my project.

I have a class like this......

class CMsg
{
public:
void *m_body;

~CMsg()
{
delete m_body;
}
}
During the program execution pointers of different types are typecasted
and assigned to m_body. Some pointers meant for a single block (p= new
char) and few are pointers to array. (Memory was allocated by p = new
char[nSize]).

Memory deallocation happens in the destructor of CMsg by invoking
delete. This deallocation works only for deleting a single block of
memory. So, while trying to delete memory assosiated with pointer to
array, exception is thrown.

How to identify pointer to an array? and deallocate accordingly by
calling delete [] ?

解决方案

* fe*******@gmail.com:

I have a strange problem in my project.

I have a class like this......

class CMsg
{
public:
void *m_body;

~CMsg()
{
delete m_body;
}
}
During the program execution pointers of different types are typecasted
and assigned to m_body. Some pointers meant for a single block (p= new
char) and few are pointers to array. (Memory was allocated by p = new
char[nSize]).

Memory deallocation happens in the destructor of CMsg by invoking
delete. This deallocation works only for deleting a single block of
memory. So, while trying to delete memory assosiated with pointer to
array, exception is thrown.

How to identify pointer to an array? and deallocate accordingly by
calling delete [] ?



The best strategy is to use

std::string m_body;

or

std::vector<char> m_body;

if the array of chars must be contigous, e.g. for interfacing to C.

If that doesn''t help you, you must probably just to keep track, in an
extra member variable, of what m_body currently is.
--
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?


fe*******@gmail.com wrote:

I have a strange problem in my project.

I have a class like this......

class CMsg
{
public:
void *m_body;

~CMsg()
{
delete m_body;
}
}
During the program execution pointers of different types are typecasted
and assigned to m_body. Some pointers meant for a single block (p= new
char) and few are pointers to array. (Memory was allocated by p = new
char[nSize]).

Memory deallocation happens in the destructor of CMsg by invoking
delete. This deallocation works only for deleting a single block of
memory. So, while trying to delete memory assosiated with pointer to
array, exception is thrown.

How to identify pointer to an array? and deallocate accordingly by
calling delete [] ?



class CMsg
{
public:
bool is_array;
void* m_body;

~CMsg()
{
if (is_array) delete[] m_body;
else delete m_body;
}
};

Regards,
Ben


benben posted:

fe*******@gmail.com wrote:

I have a strange problem in my project.

I have a class like this......

class CMsg
{
public:
void *m_body;

~CMsg()
{
delete m_body; } }
During the program execution pointers of different types are typecasted and assigned to m_body. Some pointers meant for a single block (p= new
char) and few are pointers to array. (Memory was allocated by p = new
char[nSize]).

Memory deallocation happens in the destructor of CMsg by invoking
delete. This deallocation works only for deleting a single block of
memory. So, while trying to delete memory assosiated with pointer to
array, exception is thrown.

How to identify pointer to an array? and deallocate accordingly by
calling delete [] ?



class CMsg
{
public:
bool is_array;
void* m_body;

~CMsg()
{
if (is_array) delete[] m_body;
else delete m_body;
}
};

Regards,
Ben



Do you not have to give "delete" the correct type? For instance, if you
declared an "int" with "new", then "delete" wasn''t an argument of type
"int*", rather than a "void*"?
-Tomás


这篇关于删除Vs.删除[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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