Choosing between new or malloc

Discussion in 'C++' started by Henrik J, Nov 11, 2003.

  1. Henrik J

    Henrik J Guest

    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
     
    Henrik J, Nov 11, 2003
    #1
    1. Advertising

  2. In article <>,
    (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
     
    Humphrey Clerx, Nov 11, 2003
    #2
    1. Advertising

  3. Henrik J

    Chris Theis Guest

    "Henrik J" <> wrote in message
    news:...
    > 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
     
    Chris Theis, Nov 11, 2003
    #3
  4. (Henrik J) wrote in message news:<>...
    > 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
     
    Stephen M. Webb, Nov 11, 2003
    #4
  5. Henrik J

    David White Guest

    Henrik J <> wrote in message
    news:...
    > 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
     
    David White, Nov 11, 2003
    #5
  6. Henrik J

    Henrik J Guest

    Thanks alot to all of you..!
    It's working now...

    Regards Henrik
     
    Henrik J, Nov 12, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    721
  2. ravi
    Replies:
    0
    Views:
    469
  3. Peter
    Replies:
    34
    Views:
    2,019
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    492
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    334
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page