C++ and m/c/realloc with Native Types

This is a discussion on C++ and m/c/realloc with Native Types within the C++ Programming forums, part of the General Programming Boards category; Is it safe to use malloc and it's derivatives with native C++ types? As sort of a learning experience I ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    7

    C++ and m/c/realloc with Native Types

    Is it safe to use malloc and it's derivatives with native C++ types? As sort of a learning experience I want to write my own stack class, first with integers, and I can't imagine how i'd write a dynamically sized array without using these functions. Are there, however, some alternative functions in C++?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    malloc = new. free = delete. New will call a class's constructor, which malloc won't.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Always use new and not malloc.
    There is no substitution for realloc, however. You will have to manually allocate more memory and copy over the old.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As stated, it's "better" to use new/delete as those are the clean C++ way to do it.

    However, the C++ standard allows (essentially) C89/C90 standard C, so you CAN use malloc/realloc/calloc if you really have a good reason (such as, you have 2000 lines of old C code that does EXACTLY what you need, and does it without any known problems - there's no reason to rewrite the memory allocation there to use new/delete).

    Note also that mixing [as in, "allocate" with new and free using free(), or allocate with malloc and free with delete] the two forms is very likely to land you into serious trouble.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Malloc is left there for backwards compatibility, so if you are compiling or writing C code, of course you can / should use malloc. But for any C++ project, new/delete is the way to go.
    That, I think, is what we want to point out.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by StapleGun View Post
    I can't imagine how i'd write a dynamically sized array without using these functions. Are there, however, some alternative functions in C++?
    You'd do it the same way the STL does.
    i.e. Allocate a new array that's bigger than the old one (twice as big is probably a good idea); then copy everything to the new array; then delete the old array...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  7. #7
    Kung Fu Kitty Angus's Avatar
    Join Date
    Oct 2008
    Location
    Montreal, Canada
    Posts
    115
    Quote Originally Posted by Elysia View Post
    Always use new and not malloc.
    There is no substitution for realloc, however. You will have to manually allocate more memory and copy over the old.
    This sounds like a very good reason to use malloc() over new. If I'm working with a primitive type, and I am going to have a use for realloc() then I'll use malloc(). If not, then the only benefit of using new on primitive types is that you can call delete on a NULL, which you can't do with free().

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    I remember a thread from a while ago (maybe a year or two) where using malloc/realloc for dynamically sizing an array in C++ was discussed. I seem to recall there might have been other reasons given for not using realloc in this way, but I cannot remember them. You might consider searching for that thread to see if there is any more insight.

    Edit: variable array size is the one I'm thinking of. Looking through it reminded me, are you trying to use malloc/realloc as a premature optimization, or have you identified a place in your code where new causes a slowdown?
    Last edited by Daved; 11-04-2008 at 11:22 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by Angus
    If not, then the only benefit of using new on primitive types is that you can call delete on a NULL, which you can't do with free().
    Actually, it is safe to call free() on a null pointer too.

    In this case, there is no benefit to using malloc/realloc/free since the next step would be to turn the class into a class template, upon which one would have to use new[] and delete[] anyway in order to accomodate non-POD types.

    Then again, it should be possible to continue to use malloc/(realloc?)/free but with placement new and its associated weirdness hidden beneath the stack class template interface, but that may be yet a further step that the OP should not be concerned with at this point.

    Also, consider implementing a vector class template, and then using that to implement a stack class template. This would be akin to how std::stack uses an underlying container.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Quote Originally Posted by Angus View Post
    This sounds like a very good reason to use malloc() over new. If I'm working with a primitive type, and I am going to have a use for realloc() then I'll use malloc(). If not, then the only benefit of using new on primitive types is that you can call delete on a NULL, which you can't do with free().
    Realloc many times does the same thing. Allocates new size. Copies over old data. Deletes old data.
    But in C++, this is even further abstracted by the fact that typically we use std::vector and nor new/delete/new/delete.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I guess you could create a template container and specialize the primitive types to use realloc() and use new/delete for everything else, but I doubt you'd notice much performance benefit from doing it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I got bored after seeing the conversation switching back and forth between senior members. I hope someone brought up the placement new operator at some point.

    Example:
    Code:
    myClass *x = new(malloc(sizeof(*x))) myClass;

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    I got bored after seeing the conversation switching back and forth between senior members. I hope someone brought up the placement new operator at some point.

    Example:
    Code:
    myClass *x = new(malloc(sizeof(*x))) myClass;
    Yes, but that is REALLY not a good idea except in very special cases.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by matsp View Post
    Yes, but that is REALLY not a good idea except in very special cases.

    --
    Mats
    Agreed 100%. Particularly when considering that there is a golden rule of C++ "Never call a destructor explicitly." And placement new is the only exception to that rule. You have to explicitely call the destructor or else the destructor will not be called upon freeing the block of memory.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    And yet you post the code that goes against exactly that rule... Bored? Or just having fun?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21