473,382 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

malloc to new

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.
Jul 22 '05 #1
21 1525

"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
Jul 22 '05 #2
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
Jul 22 '05 #3
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
Jul 22 '05 #4

"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 ?


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
Jul 22 '05 #5
* 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.

--
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?
Jul 22 '05 #6
Jeff wrote:
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 ?

Thanks for your help.

Jul 22 '05 #7
Jeff wrote:

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++')

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #8
You don't need to check for NULL because operator 'delete' does that
for you.

.. . .inefficient?

-JKop
Jul 22 '05 #9

"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];
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
Jul 22 '05 #10
* JKop:
You don't need to check for NULL because operator 'delete' does that
for you.

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

--
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?
Jul 22 '05 #11
>> 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.

Jul 22 '05 #12
Alf P. Steinbach wrote:
* 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.


Why ? Isn't it pointless to dynamically allocate const sized arrays ?
Jul 22 '05 #13

"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


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
Jul 22 '05 #14
It's all right.
Thanks you all.
Jul 22 '05 #15
JKop wrote:
You don't need to check for NULL because operator 'delete' does that
for you.


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #16
In message <fV****************@newsread1.dllstx09.us.to.verio .net>,
Victor Bazarov <v.********@comAcast.net> writes
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.

--
Richard Herring
Jul 22 '05 #17
Richard Herring wrote:
In message <fV****************@newsread1.dllstx09.us.to.verio .net>,
Victor Bazarov <v.********@comAcast.net> writes
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.


You're right. I just didn't look! Damn...
Jul 22 '05 #18
Jeff wrote:
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.
Jul 22 '05 #19
Jeff wrote:

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.
Jul 22 '05 #20
JKop wrote:
You don't need to check for NULL because operator 'delete' does that
for you.


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

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #21
* Gianni Mariani:
Alf P. Steinbach wrote:
* 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.


Why ?


It's not a good idea to have global variables.

Isn't it pointless to dynamically allocate const sized arrays ?


In general, not necessarily (lifetime, size, number of them).

In the OP's case I don't know.

No further information is given.

--
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?
Jul 22 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: john smith | last post by:
Can someone please explain to me what is happening when I do a malloc(0). This is what I did. int* p = (int*)malloc(0); Then I printed the value of p and of course it was non-null. But...
34
by: Richard Hunt | last post by:
I'm sorry for asking such a silly question, but I can't quite get my head around malloc. Using gcc I have always programmed in a lax C/C++ hybrid (which I suppose is actually c++). But I have...
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
20
by: spasmous | last post by:
main() { float * f; initialize_f(f); // ...use f for processing free(f); }
15
by: Martin Jørgensen | last post by:
Hi, I have a (bigger) program with about 15-30 malloc's in it (too big to post it here)... The last thing I tried today was to add yet another malloc **two_dimensional_data. But I found out that...
68
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting;...
40
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd...
71
by: desktop | last post by:
I have read in Bjarne Stroustrup that using malloc and free should be avoided in C++ because they deal with uninitialized memory and one should instead use new and delete. But why is that a...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.