Thread: using malloc in C++

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    using malloc in C++

    I was wondering if there is anything wrong with using malloc in a C++ program. I also remember reading a thread some time ago about making a "renew" operator similiar to realloc. If you are using a block of memory and want to change the size frequently without using a temporary block to copy data, I think using malloc is better because you can then use realloc. But is realloc really any faster? Maybe it just does the copying behind the scenes, especially if the block is not able to fit.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are dozens of different ways to solve this particular problem. C++ std::vector does indeed solve this particular problem, but it does in effect do the same thing as realloc(), and I doubt that it's MORE efficient than using realloc in the cases where realloc and vector solves the same problem.

    Another solution would be to allocate (with whatever method) larger blocks in for example a linked list. If you know that you will have thousands of entries, but it may grow to lots more, you may allocate blocks of for example 4000 (or just under 4096, to allow the block to be a multiple of 4096 bytes with room for a header block with links and other admin) entries, and then link them together.

    But the initial thought should probably be to use vector.

    --
    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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    When using vectors, reserve() can be used if you know in advance the exact maximum memory capacity needed, and the "swap trick" can be used to trim the capacity to the actual size of the vector, if you don't know the final size in advance. Together these give the same control over memory use as malloc()/realloc().

    http://www.informit.com/guides/conte...lus&seqNum=268

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MacNilly View Post
    I was wondering if there is anything wrong with using malloc in a C++ program. I also remember reading a thread some time ago about making a "renew" operator similiar to realloc. If you are using a block of memory and want to change the size frequently without using a temporary block to copy data, I think using malloc is better because you can then use realloc. But is realloc really any faster? Maybe it just does the copying behind the scenes, especially if the block is not able to fit.
    Technically there's nothing wrong with using malloc() in C++, as long as you only use it on PODs. Once you get into objects, you have to use new, otherwise the constructor won't be called. I've seen C++ code that uses malloc() for char* strings and new for objects. I consider it very bad code though, because when it comes time to delete the pointers, you'll be in a whole lot of trouble if you accidentally call delete with malloc()'ed memory or free() with new'ed memory. That sort of code comes from people who think they're smarter than their compilers and are trying to optimized code that probably doesn't need to be optimized to begin with.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The use of malloc in C++ certainly isn't inheritably bad, but it is discouraged. I really see no need to use malloc at all in C++.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    The use of malloc in C++ certainly isn't inheritably bad, but it is discouraged. I really see no need to use malloc at all in C++.
    You mean beside a trivial implementation of a new() operator? ;-)

    --
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you override new, you probably want to use something else rather than malloc seeing as it does the same thing as new... so no If I override new, I'm looking for something more like VirtualAlloc or something.
    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.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Unless the purpose of overriding new is not to change the function, but to track allocations and deallocation, of course.

    Malloc can also be used for the explicit purpose of using realloc, in the rare cases that such an optimization may be worthwhile.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #10
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    thanks for all the replies; the swap() trick was particularly helpful.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by King Mir View Post
    Malloc can also be used for the explicit purpose of using realloc, in the rare cases that such an optimization may be worthwhile.
    But then you probably wouldn't override new but make your own allocation/deallocation function...
    Though you may be right. One small plus for malloc inside C++.
    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.

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    But then you probably wouldn't override new but make your own allocation/deallocation function...
    Though you may be right. One small plus for malloc inside C++.
    If you're doing that with objects, malloc()/realloc() probably isn't a good idea because realloc() will move memory if there isn't enough space. Moving objects without destructing & reconstructing them is probably a bad thing. In my earlier posts on the same subject I found a Windows specific way using HeapAlloc() & HeapReAlloc(). HeapReAlloc() has an option to expand or fail rather than expand or move the memory - realloc() doesn't have that option.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM