Choosing between new or malloc

H

Henrik J

Hello group..!
I have this struct:

struct FooData
{
unsigned short *foolist;
int NoOfFoos;
};

I want to dynamicly allocate X numbers of my FooData struct.
I'm using the new command..!
But what about my *foolist?
If I want to read data into my *foolist I have to use malloc or what...?

I know that dedicated users of c++ doesn't use malloc.

Regards Henrik Tomra
 
H

Humphrey Clerx

I have this struct:

struct FooData
{
unsigned short *foolist;
int NoOfFoos;
};

I want to dynamicly allocate X numbers of my FooData struct.
I'm using the new command..!
But what about my *foolist?
If I want to read data into my *foolist I have to use malloc or what...?

I know that dedicated users of c++ doesn't use malloc.

Why not use a std::vector<unsigned short> instead?

Then you can use it as follows:

std::vector<unsigned short> FooData;

int X = ...;
FooData.reserve(X);
for (int i = 0; i < X; ++i)
{
unsigned short my_data = ...;
FooData.push_back(my_data);
}

int NoOfFoos = FooData.size();

No need for new or malloc at all.

Greetings,
Humphrey.

--
----------------------------------------------------------------
In the mountains of truth, you never climb in vain - Nietzsche
#------+--------------------------------------------------------
\_O | Humphrey Clerx Eurocontrol Maastricht UAC
,__/> | Horsterweg 11
<" | tel: +31-(0)43-3661379 NL-6199 AC Maastricht Airport
' | fax: +31-(0)43-3661300 The Netherlands
 
C

Chris Theis

Henrik J said:
Hello group..!
I have this struct:

struct FooData
{
unsigned short *foolist;
int NoOfFoos;
};

I want to dynamicly allocate X numbers of my FooData struct.
I'm using the new command..!
But what about my *foolist?
If I want to read data into my *foolist I have to use malloc or what...?

I know that dedicated users of c++ doesn't use malloc.

Regards Henrik Tomra

As already explained in another thread you could use a vector instead and
your problems are solved. However, I'll try to shed some more light to that
new & malloc business. Malloc is an "old" C function that doesn't know about
classes and therefore has no clue about object construction. It just
allocates raw memory. If you want to allocate an object on the heap using
C++ it's not just sufficient to allocate the memory but you also need to put
that object into a valid state. This is done by the execution of the
constructor. That's why it's necessary to use new in C++ if you want to
create objects on the heap. For plain old data types like int or whatever
you could of course still use malloc but it's simply not a good idea to
start mixing. Another word of caution - when you use malloc then you must(!)
use free. In opposition to this the "new" statement requires a "delete"
statement to deallocate memory. Never ever mix them like using malloc and
delete or vice versa!!

HTH
Chris
 
S

Stephen M. Webb

Hello group..!
I have this struct:

struct FooData
{
unsigned short *foolist;
int NoOfFoos;
};

I want to dynamicly allocate X numbers of my FooData struct.
I'm using the new command..!
But what about my *foolist?
If I want to read data into my *foolist I have to use malloc or what...?

You may choose to use std::malloc() if you wish, but I would recommend
the use of a new expression for a number of reasons.

First, if you use new expressions consistently, you never have to
remember if you used new or called malloc() when the time comes to
release the resource.

Second, while calling malloc() will work without problem for POD types
(plain old data, that is a structure (class or struct) that has only
compiler-supplied contructors and destructors), as soon as you add
non-trivial construction or destruction you will either have to search
for and change the malloc()/free() calls to new and delete expressions
or cope with bug reports.

Third, if you centralize allocation of a particular resource through
one mechanism, it is easier to instrument that single mech for
debugging or to tweak it for performance if necessary. The only
reasonable numbers are zero, one, and infinity.

Finally, a new expression will throw an exception on failure, whereas
calling the malloc() function requires explicitly checking the
returned pointer for an out-of-band value and propagating failure
notice appropriately. Since this tends to be inconvenient, it often
doesn't get done. Again with the bug reports.

You are free to choose, but choose wisely.
 
D

David White

Henrik J said:
Hello group..!
I have this struct:

struct FooData
{
unsigned short *foolist;
int NoOfFoos;
};

I want to dynamicly allocate X numbers of my FooData struct.
I'm using the new command..!
But what about my *foolist?
If I want to read data into my *foolist I have to use malloc or what...?

I know that dedicated users of c++ doesn't use malloc.

FooData fd;
fd.NoOfFoos = 10;
fd.foolist = new unsigned short[fd.NoOfFoos];

To deallocate them later, write:
delete[] fd.foolist;

Note that you use 'delete[]' if you had allocated an array, and plain
'delete' for single allocations e.g.,
unsigned short *p = new unsigned short;
// ...
delete p;

I also recommend a vector, as Humphrey suggested. One of the big advantages
of C++ is that it allows you to concentrate on higher level code while the
standard library does most of the mundane memory management required to
maintain collections of objects.

DW
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top