malloc到新的 [英] malloc to new

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

问题描述




我正在尝试在C ++中使用new / delete而不是malloc / free。

我有这个:


int g_TTable_size = 130000; //例如

struct hash_t

{

int深度;

int flags;

int eval;

};


这样可以正常工作:

hash_t * pt_htable =(hash_t *)malloc (g_TTable_size * sizeof(hash_t));

if(pt_htable!= NULL){free(pt_htable);}


但这不是: - (((

hash_t * pt_htable =(hash_t *)new char [g_TTable_size * sizeof(hash_t)];

both:

hash_t * pt_htable =( hash_t *)new hash_t [g_TTable_size];

这可能没问题,我还不知道:

if(pt_htable!= NULL){delete pt_htable; }


我做错了什么?


感谢您的帮助。

解决方案



" Jeff"< no **** @ nospam.com>写在留言中

news:41 ***** ****************** @ news.wanadoo.fr ...



我正在尝试在C ++中使用new / delete而不是malloc / free 。
我有这个:

int g_TTable_size = 130000; //例如
struct hash_t
{int int;
int flags;
int eval;
};

这很好用:
hash_t * pt_htable =(hash_t *)malloc(g_TTable_size * sizeof(hash_t));
if(pt_htable!= NULL){free(pt_htable);}
<但是这不是: - ((
hash_t * pt_htable =(hash_t *)new char [g_TTable_size * sizeof(hash_t)];
既不:
hash_t * pt_htable =(hash_t * )new hash_t [g_TTable_size];
这可能没问题,我还不知道:
if(pt_htable!= NULL){delete pt_htable;}

什么我做错了吗?




你没有使用可用的工具:


#include< vector>


std :: vector< hash_t> htable(g_TTable_size);


Jeff


Jeff写道:

我正在尝试在C ++中使用new / delete而不是malloc / free。
我有这个:

int g_T Table_size = 130000; //例如
struct hash_t
{int int;
int flags;
int eval;
};

这很好用:
hash_t * pt_htable =(hash_t *)malloc(g_TTable_size * sizeof(hash_t));
if(pt_htable!= NULL){free(pt_htable);}
<但是这不是: - ((
hash_t * pt_htable =(hash_t *)new char [g_TTable_size * sizeof(hash_t)];


a)没有必要投下结果。放弃演员。

b)大小错误。括号中的表达式不应该是''sizeof''部分。

both:
hash_t * pt_htable =(hash_t *)new hash_t [g_TTable_size ]。


a)这个尺寸更好。但仍然,放弃该死的演员。

这可能没问题,我还不知道:
if(pt_htable!= NULL){delete pt_htable;}


不,这不行。如果使用''new []''进行分配,则需要使用

delete []。此外,如果分配失败,则抛出异常,除非使用''nothrow''变体,否则''new''

不会返回NULL。只需说


删除[] pt_htable;

我做错了什么?




你你可能不会仔细阅读你的C ++书籍。


V


Jeff发布:



我正在尝试在C ++中使用new / delete而不是malloc / free。
我有这个:

int g_TTable_size = 130000; //例如
struct hash_t
{int int;
int flags;
int eval;
};

这很好用:
hash_t * pt_htable =(hash_t *)malloc(g_TTable_size * sizeof(hash_t));
if(pt_htable!= NULL){free(pt_htable);}
<但是这不是: - ((
hash_t * pt_htable =(hash_t *)new char [g_TTable_size * sizeof(hash_t)];
既不:
hash_t * pt_htable =(hash_t * )new hash_t [g_TTable_size];
这可能没问题,我还不知道:
if(pt_htable!= NULL){delete pt_htable;}



hash_t * pt_htablearray1 = new hash_t [g_TTable_size];


delete [] pt_htablearray;

hash_t * pt_htablearray2 = reinterpret_cast< hash_t * const>(new char

[g_TTable_size * sizeof(hash_t)]);


delete [] pt_htablearray2;

-JKop


Hi,

i''m trying to use new/delete in C++ instead of malloc/free.
I have this :

int g_TTable_size=130000; // for example
struct hash_t
{
int depth;
int flags;
int eval;
};

And this works fine :
hash_t *pt_htable = (hash_t *) malloc (g_TTable_size*sizeof(hash_t));
if(pt_htable!=NULL) {free(pt_htable);}

But this not :-((
hash_t *pt_htable = (hash_t *) new char[g_TTable_size*sizeof(hash_t)];
neither :
hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
And this may be ok, i don''t know yet :
if(pt_htable!=NULL) {delete pt_htable;}

What am i doing wrong ?

Thanks for your help.

解决方案


"Jeff" <no****@nospam.com> wrote in message
news:41***********************@news.wanadoo.fr...

Hi,

i''m trying to use new/delete in C++ instead of malloc/free.
I have this :

int g_TTable_size=130000; // for example
struct hash_t
{
int depth;
int flags;
int eval;
};

And this works fine :
hash_t *pt_htable = (hash_t *) malloc (g_TTable_size*sizeof(hash_t));
if(pt_htable!=NULL) {free(pt_htable);}

But this not :-((
hash_t *pt_htable = (hash_t *) new char[g_TTable_size*sizeof(hash_t)];
neither :
hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
And this may be ok, i don''t know yet :
if(pt_htable!=NULL) {delete pt_htable;}

What am i doing wrong ?



Your not using the available tools:

#include <vector>

std::vector<hash_t> htable( g_TTable_size );

Jeff


Jeff wrote:

i''m trying to use new/delete in C++ instead of malloc/free.
I have this :

int g_TTable_size=130000; // for example
struct hash_t
{
int depth;
int flags;
int eval;
};

And this works fine :
hash_t *pt_htable = (hash_t *) malloc (g_TTable_size*sizeof(hash_t));
if(pt_htable!=NULL) {free(pt_htable);}

But this not :-((
hash_t *pt_htable = (hash_t *) new char[g_TTable_size*sizeof(hash_t)];
a) There is no need to cast the result. Drop the cast.
b) The size if wrong. The expression in the brackets should not have
the ''sizeof'' part.
neither :
hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
a) This is better size-wise. But still, drop the damn cast.
And this may be ok, i don''t know yet :
if(pt_htable!=NULL) {delete pt_htable;}
No, this is not OK. If you allocate using ''new[]'', you need to use
delete[]. Also, if allocation fails, an exception is thrown, the ''new''
does not return NULL unless you use the ''nothrow'' variation. Just say

delete[] pt_htable;

What am i doing wrong ?



You''re probably not reading your C++ book as carefully as you should.

V


Jeff posted:

Hi,

i''m trying to use new/delete in C++ instead of malloc/free.
I have this :

int g_TTable_size=130000; // for example
struct hash_t
{
int depth;
int flags;
int eval;
};

And this works fine :
hash_t *pt_htable = (hash_t *) malloc (g_TTable_size*sizeof(hash_t));
if(pt_htable!=NULL) {free(pt_htable);}

But this not :-((
hash_t *pt_htable = (hash_t *) new char[g_TTable_size*sizeof(hash_t)];
neither :
hash_t *pt_htable = (hash_t *) new hash_t[g_TTable_size];
And this may be ok, i don''t know yet :
if(pt_htable!=NULL) {delete pt_htable;}


hash_t *pt_htablearray1 = new hash_t[g_TTable_size];

delete [] pt_htablearray;
hash_t *pt_htablearray2 = reinterpret_cast<hash_t* const>( new char
[g_TTable_size * sizeof(hash_t)] );

delete [] pt_htablearray2;
-JKop


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

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