Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Choosing between new or malloc

Reply
Thread Tools

Choosing between new or malloc

 
 
Henrik J
Guest
Posts: n/a
 
      11-11-2003
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
 
Reply With Quote
 
 
 
 
Humphrey Clerx
Guest
Posts: n/a
 
      11-11-2003
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      11-11-2003

"Henrik J" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Stephen M. Webb
Guest
Posts: n/a
 
      11-11-2003
(E-Mail Removed) (Henrik J) wrote in message news:<(E-Mail Removed). 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
(E-Mail Removed)
 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      11-11-2003
Henrik J <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
Henrik J
Guest
Posts: n/a
 
      11-12-2003
Thanks alot to all of you..!
It's working now...

Regards Henrik
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
to malloc or not to malloc?? Johs32 C Programming 4 03-30-2006 10:03 AM
porting non-malloc code to malloc micromysore@gmail.com C Programming 3 02-19-2005 05:39 AM
Malloc/Free - freeing memory allocated by malloc Peter C Programming 34 10-22-2004 10:23 AM
free'ing malloc'd structure with malloc'd members John C Programming 13 08-02-2004 11:45 AM
Re: free'ing malloc'd structure with malloc'd members ravi C Programming 0 07-30-2004 12:42 PM



Advertisments