By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 811 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Choosing between new or malloc

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In article <d1**************************@posting.google.com >,
do******@sol.dk (Henrik J) writes:
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
Jul 19 '05 #2

P: n/a

"Henrik J" <do******@sol.dk> wrote in message
news:d1**************************@posting.google.c om...
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
Jul 19 '05 #3

P: n/a
do******@sol.dk (Henrik J) wrote in message news:<d1**************************@posting.google. com>...
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.

--
Stephen M. Webb
st**********@bregmasoft.com
Jul 19 '05 #4

P: n/a
Henrik J <do******@sol.dk> wrote in message
news:d1**************************@posting.google.c om...
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

Jul 19 '05 #5

P: n/a
Thanks alot to all of you..!
It's working now...

Regards Henrik
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.