Thread: Memory allocation question using malloc

  1. #1
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654

    Memory allocation question using malloc

    So I was wondering, if one were to use malloc or some malloc-styled allocation (i.e. not using) new, and then using placement new on that memory, would calling delete on that memory then be defined according to the standard?

    This may seem silly. Why would anyone want to do this? But sometimes there are libraries written in C that you just have to use.

    Example:
    Code:
    auto* p = malloc(sizeof(std::vector<int>));
    auto* v = new (v) std::vector<int>();
    // Use v
    delete v;
    v = p = nullptr;
    Last edited by Elysia; 11-22-2014 at 03:58 PM.
    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.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    But sometimes there are libraries written in C that you just have to use.
    So why would you need to use malloc() in a C++ program just to use some C library?


    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The point is that the library written in C uses some kind of malloc-style allocation.
    Code:
    auto * p = allocate_me_some_memory(numa_core, sizeof(std::vector<int>));
    auto * myvec = new (p) std::vector<int>();
    // Use myvec
    delete myvec;
    myvec = p = nullptr;
    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
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    If the library is using malloc() you should be using free().

    Jim

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That won't call destructors (BAD, BAD BAD!!!), so I was curious if it's undefined or not.
    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
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    The point is that the library written in C uses some kind of malloc-style allocation.
    Code:
    auto * p = allocate_me_some_memory(numa_core, sizeof(std::vector<int>));
    auto * myvec = new (p) std::vector<int>();
    // Use myvec
    delete myvec;
    myvec = p = nullptr;
    The behaviour of your example is undefined, since you need to use the corresponding placement delete.

    If you have modified/overloaded a variant of operator new() to use malloc(), the default expectation would be to use free() in the corresponding operator delete(). And code which uses operator new (in an expression) needs to use the corresponding operator delete.

    If you are using malloc() to allocate memory that is then passed to placement new, the expectation is that you would use free() after using the corresponding placement delete.
    Last edited by grumpy; 11-22-2014 at 04:18 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    So what? That C library doesn't call constructors or even know anything about C++ classes, so there are no classes to worry about, hence no destructors need to be called.

    Jim

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Now thinking about it, it was probably a silly question. You're right. We need to explicitly call the destructor and then free.
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jimblumberg View Post
    So what? That C library doesn't call constructors or even know anything about C++ classes, so there are no classes to worry about, hence no destructors need to be called.

    Jim
    Come on. We can't allocate standard library containers with malloc. So we need to use placement new. And because of that we need to call destructors.
    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.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Yes. That would be undefined behavior.

    One solution, my preferred, is using an adapter to disguise allocation/deallocation details and custom deleter with a smart pointer.

    Code:
    template<typename F> kill(F & f){f->~F();}
    // ...
    {
    auto sWhatever = allocate_and_construct_me_some_object<std::vector<int>>(adapter, &kill, /* constructor arguments */);
    sWhatever->push_back(0);
    // sWhatever is destroyed so ~std::vector<int>() and `free' are called 
    }
    Soma
    Last edited by phantomotap; 11-22-2014 at 04:28 PM.
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The behaviour of your example is undefined, since you need to use the corresponding placement delete.
    O_o

    You can't call the corresponding placement `delete' manually; those functions are only called automatically when an exception is thrown from the associated `new'.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  12. #12
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    I realize the code was just an example, but I wanted to make one more point.

    The code `allocate_me_some_memory(numa_core, sizeof(std::vector<int>));' allocates memory for a container, apparently, on some NUMA architecture. That is fine, but the memory allocated by the container will live wherever the standard allocator places that memory. If you want the memory allocated by the container to live on the same place as the container, you'll have to deal with a custom allocator.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Good point. I hadn't thought about that. Guess it's time to look into custom allocators, then.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc memory allocation
    By jamorp in forum C Programming
    Replies: 3
    Last Post: 10-17-2014, 06:57 AM
  2. warnings- dynamic memory allocation - malloc
    By raviraj123 in forum C Programming
    Replies: 6
    Last Post: 06-05-2013, 01:28 PM
  3. Memory allocation with malloc
    By duif in forum C Programming
    Replies: 6
    Last Post: 11-28-2012, 09:05 AM
  4. Dynamic Memory Allocation (malloc vs calloc)
    By lostandconfused in forum C Programming
    Replies: 10
    Last Post: 09-01-2010, 01:56 PM
  5. Memory allocation without malloc
    By messi89 in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 07:27 AM