Thread: Container allocators based in malloc and realloc

  1. #1
    Registered User
    Join Date
    Apr 2020
    Posts
    20

    Question Container allocators based in malloc and realloc

    Hi,

    I've found several container classes out there that use malloc() internally as their allocator, and realloc() for growing/shrinking. Of course, they use placement new.

    They claim the only requirement is that these container classes cannot be used for objects "whose copy constructor has side effects". Is that always the case? I mean, is having a "copy constructor without side effects" the only requirement for being it safe to reallocate C++ objects to a different memory address?

    I make this question because one of the first things I learnt about C++ is that objects are not supposed to move from one memory address to another, so I'm wondering if this kind of container classes violate some rule from the standard.

    Said this, is there any succinct and complete definition of what's a "copy constructor without side effects"? Typically I hear the explanation that they don't do any other thing but copying the members, but I find that explanation quite vague and imprecise.

    By the way, is there any keyword in the C++ language that lets you tag a copy constructor as either having side effects or not?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,214
    Quote Originally Posted by ccafe
    I've found several container classes out there that use malloc() internally as their allocator, and realloc() for growing/shrinking. Of course, they use placement new.

    They claim the only requirement is that these container classes cannot be used for objects "whose copy constructor has side effects". Is that always the case? I mean, is having a "copy constructor without side effects" the only requirement for being it safe to reallocate C++ objects to a different memory address?

    I make this question because one of the first things I learnt about C++ is that objects are not supposed to move from one memory address to another, so I'm wondering if this kind of container classes violate some rule from the standard.
    It would be good for you to provide an example of such a container class and its associated documentation for readers here to examine since it is possible that you may have misinterpreted the claim, or someone attempting to answer your questions may misinterpret your paraphrasing of the claim.

    In general though, C++ programmers tend to be conditioned not to rely on copy constructors having side effects since copy constructors can be elided.

    Quote Originally Posted by ccafe
    Said this, is there any succinct and complete definition of what's a "copy constructor without side effects"? Typically I hear the explanation that they don't do any other thing but copying the members, but I find that explanation quite vague and imprecise.
    I think it is used in the general computer science sense of "modifying outside the local environment context" rather than something explicitly defined by the standard, that's why the explanations you hear are "quite vague and imprecise".

    Quote Originally Posted by ccafe
    By the way, is there any keyword in the C++ language that lets you tag a copy constructor as either having side effects or not?
    Not that I know of, unless you count comments of the structured type that ends up in generated documentation.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2020
    Posts
    20
    Quote Originally Posted by laserlight View Post
    It would be good for you to provide an example of such a container class and its associated documentation for readers here to examine since it is possible that you may have misinterpreted the claim, or someone attempting to answer your questions may misinterpret your paraphrasing of the claim.
    I found quite a complete thread on this topic in this other forum: Making "new" out of "malloc" - C++ Forum and it includes examples about container classes in the fashion I was talking about. It also includes an ancient quote by Stroustrup saying that you can realloc() only if your objects have default copy constructors (which makes sense to me, as a realloc just copies every byte when the buffer needs to be moved). A very good read, I found that thread really helpful for understanding my questions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Range-based looping over container of references
    By milli-961227 in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2015, 06:24 AM
  2. Use of malloc and realloc
    By mrbains in forum C Programming
    Replies: 5
    Last Post: 11-11-2010, 02:53 AM
  3. Differences allocators malloc-new
    By Leite33 in forum C++ Programming
    Replies: 1
    Last Post: 10-09-2006, 03:34 PM
  4. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM

Tags for this Thread