malloc to new

J

Jeff

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.
 
J

Jeff Flinn

Jeff said:
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
 
V

Victor Bazarov

Jeff said:
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
 
J

JKop

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
 
J

John Harrison

Jeff said:
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 ?

This is correct

hash_t *pt_htable = new hash_t[g_TTable_size];

delete[] pt_htable;

If you use new[], then you use delete[]. You don't need to test for NULL
before doing delete or delete[].

john
 
A

Alf P. Steinbach

* Jeff:
i'm trying to use new/delete in C++ instead of malloc/free.

I have this :

int g_TTable_size=130000; // for example

Should be const.

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)];

'new' isn't supposed to be used that way.

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

This should be OK, except for the C style cast which you should remove.

Note that if the allocation fails you will get a std::bad_alloc exception
(with a standard-conforming C++ implementation).

And this may be ok, i don't know yet :
if(pt_htable!=NULL) {delete pt_htable;}

You don't need to check for NULL because operator 'delete' does that for
you.


What am i doing wrong ?

Technically it's impossible to say since the second attempt at 'new' should
work.

Generally, it's coding at a too low abstraction level, using C++ as C.

Remove all the casts and stuff, and also think about removing direct pointer
handling, e.g. by using std::vector as suggested by Jeff Flinn.
 
G

Gianni Mariani

Jeff said:
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)];

Very very ugly but it should work on most platforms for your struct -
but it's a very bad habbit to get into.

a much nicer way is :
hash_t *pt_htable = new hash_t[g_TTable_size];

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

new is type safe, you don't (MUST NOT) type cast the result.

So what is the error ?
And this may be ok, i don't know yet :
if(pt_htable!=NULL) {delete pt_htable;}

use :
delete[] pt_htable;

// or if you did the ugly thing
delete[] (char*) pt_htable;

When deleting an array allocated with new T[N], you must use delete[].

Also, you don't need to check for NULL. delete check for that for you.
What am i doing wrong ?

Other than the nasty cast thing and type casting the return from new ?
 
K

Karl Heinz Buchegger

Jeff said:
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 ?

Not reading a text book?

hash_t *pt_htable = new hash_t [ g_TTable_size ];

....

delete [] pt_htable;

But the preferred solution would be:

#include <vector>

....

std::vector< hash_t > HTable;
HTable.resize( g_TTable_size );

since you no longer have to deal with freeing the memory.

Do yourself a favour and get a C++ book and work through it.
C++ is much to complex to be larned by trial and error.
See eg. the FAW for recommendations. There are even free ones available.

http://ma.rtij.nl/acllc-c++.FAQ.html
http://www.parashift.com/c++-faq-lite/
http://www.mindview.net/Books (Download a free copy of 'Thinking in C++')
 
H

Howard

Jeff said:
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];

What didn't work with the above?

With new, there's no reason to cast. Just use:

hash_t *pt_htable = new hash_t[g_TTable_size];

And this may be ok, i don't know yet :
if(pt_htable!=NULL) {delete pt_htable;}

To delete when using the form new[], you need to use the matching form
delete[]. like this:

delete [] pt_htable;

(No need to check if it's NULL or not. For one thing, calling delete[] on a
NULL pointer is a no-op. Also, if new fails, you should get an exception.
Some compilers will return NULL, but as I said, that's safe to call delete[]
on.)

-Howard
 
A

Alf P. Steinbach

* JKop:
. . .inefficient?

In terms of programmer time, yes (in terms of execution time it doesn't
matter). And there should be a '[]' in there. Forgot to mention that.
 
J

Jeff

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

Thanks but i'm quite newbie, i don't undersand.
Ok about "#include <vector>" but what is "htable" ?
How to convert
hash_t *pt_htable = (hash_t *) malloc (g_TTable_size*sizeof(hash_t));
With "new" ?
Regards.
 
R

Ron Natalie

Jeff said:
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_htable = new hash_t[g_TTable_size];
delete [] pt_htable;

should work fine. Your second version is equivelent, but the
cast is unnecessary (and should be avoided). Make sure you are
using [ ] and not ( ) around the array size.

To answer your last question.

1. pt_htable will NEVER be zero (unless you set it to zero).
new signals errors by throwing an exception not by returning
null (Unless you're using a broken compiler like visual C++).

2. The test above is a no op anyhow. Delete is defined to do
nothing with a null value (as does free, by the way).

3. Your delete expression is WRONG. Things allocated with the
array form of new must be deleted with delete[].

Further, I'd avoid the whole mess if possible:

vector<hash_t> hash_t(g_TTable_size);

would be a lot more convenient.
 
J

Jeff Flinn

Jeff said:
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

Thanks but i'm quite newbie, i don't undersand.
Ok about "#include <vector>" but what is "htable" ?

You'll achieve post-newbie-ness much quicker if you read a book on C++
standard containers, and forego manual memory management.

htable is an instance of a vector of 130000 hash_t struct instances
effectively initialized to zero(IIRC). std::vector manages all of the memory
in an exception safe manner.

void somefunc()
{
std::vector<int> someints( 3 ); // someints equivalent to { 0, 0, 0 }

someints[0] = 1; // assign values
someints[1] = 3;
someints[2] = 5;

someints.push_back( 7 ); // size is now 4

legacyfunction( &someints[0] ); // passes address of 'array'

} // someints memory now 'freed'
How to convert
hash_t *pt_htable = (hash_t *) malloc (g_TTable_size*sizeof(hash_t));
With "new" ?

Patient: My head hurts after I bang it against the wall.
Doctor: Then don't bang it against the wall.

Jeff F
 
K

Karl Heinz Buchegger

JKop said:
. . .inefficient?

Not really.
Most of the time you won't delete 0 pointers.
And that single additional assembler instruction
Return_if_zero
in the code of delete doesn't hurt much but ensures
that you don't need to write

if( pPtr != NULL )
delete pPtr;

all the time.
 
K

Karl Heinz Buchegger

JKop said:
. . .inefficient?

Not really.
Most of the time you won't delete 0 pointers.
And that single additional assembler instruction
Return_if_zero
in the code of delete doesn't hurt much but ensures
that you don't need to write

if( pPtr != NULL )
delete pPtr;

all the time.
 
R

Ron Natalie

Jeff said:
Thanks but i'm quite newbie, i don't undersand.
Ok about "#include <vector>" but what is "htable" ?

htalbe is the variable name of the declared vector.
 
R

Richard Herring

In message said:
Jeff wrote:
[...]
hash_t *pt_htable = (hash_t *) new char[g_TTable_size*sizeof(hash_t)];

a) There is no need to cast the result.

As he wrote it, there is :-( He's allocating chars, not hash_t's.
Drop the cast.
b) The size if wrong. The expression in the brackets should not have
the 'sizeof' part.

Likewise.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top