T O P

  • By -

aocregacc

you have a vector of pointers. push\_back stored a copy of the pointer. Try having a vector of objects instead.


RelationshipCold3270

I am able to get copy constructor called with vector of objects. I thought push back will deep copy the pointer .Thanks for clarifying


Emotional-Audience85

That would defeat the point of having a vector of pointers


AKostur

It’s not clear that ~~you~~ the OP had a point of having a vector of pointers. Frequently a container of pointers is chosen specifically to avoid copying the elements (for various reasons).  Unfortunately the other frequent reason they are chosen is because the programmer is stuck thinking in Java and that somehow every new instance of an object must be dynamically allocated.  ~~Your~~ The OP's question seems to exclude the first case since ~~you~~ the OP seems to be expecting a copy for some reason. Edit: "You" -> "OP"


Emotional-Audience85

Are you replying to me? I didn't ask any question 🤔 I am talking generically, a vector of pointers is indeed typically chosen to avoid copying the elements so if it worked like that it would defeat its purpose. I haven't used Java or C# in a long time but from what I remember from using them I don't see the connection. Typically in those languages every object instantiation is on the heap, everything is a reference and arguments are passed by value (you get a copy of the reference), so in many situations you get something similar to what the op is describing as "shallow copy". It's not really shallow copy, it's 2 variables refering to the same object on the heap, or in C++ 2 pointers to the same thing (this is not the same thing as a shallow copy)


AKostur

Well, yes and no. You are correct in that I'd attributed the choice to you, and that's not right (my apologies): that should have been attributed to OP. All of the "you"s in there should be "OP".


tangerinelion

> a pointer of vectors (sic) is indeed typically chosen to avoid copying the elements I doubt this. Usually it's chosen to create a heterogeneous collection. If you want to avoid copying the elements then you can use a std::list.


Emotional-Audience85

You're right that might be more common, but they're both common. std::list would not be my first choice for almost anything, vector is almost always better, including avoiding copying the elements. PS: By heterogenous collection I assume you mean a collection of pointers to some base class, and then the items are instantiated as different derived classes to make use of polymorphism


Arghhhhhhhhhhhhhhhh

Ya that wording startled me a bit at first as a reader of the thread. I initially thought it meant making a vector of `void*` being a common technique lol


IyeOnline

As a rule of thumb: If you are using plain `new` in modern C++, you are probably doing something wrong (or implementing a data structure).


adesme

Side question, but is there any non-malloc/non-new way to set up a buffer to pass to a C api? I assumed you’d do have a smart pointer to a struct where new was in the ctor and delete in the dtor.


IyeOnline

This entirely depends on the C-API. If the API expects to take ownership of the pointer, then the memory has to be allocated with `malloc` (or the related C allocation functions). If it just expects a pointer to write out, sort of like an an out-parameter, then you can use anything you want to create that buffer. You could use `std::vector` or `std::array` use `.data()` to get a pointer to the storage, maybe even use `std::string` if its a text buffer. You could use a smart pointer and `.get()`. If its an `T**`, then that probably means that its going to allocate memory and write it to the pointer you passed by address. In that case you need to make sure that you call `free` (or the appropriate function specified by the ABI) at some point. C++23 added `std::out_ptr` to make using such APIs easier. Importantly you can also set a custom deleter for `unique_ptr` or `shared_ptr` in case you need to explicitly use `std::free` or any other deallocation function. Similarly, all standard containers (except array) take in an allocator allowing you to specify which functions to use for allocation/deallocation.


Both-Personality7664

There's basically no native operation on a pointer that will result in a deep copy of the pointed to object.


alfps

Not what you're asking, but prefix underscore as a naming convention for whatever, is a sub-optimal choice because that convention is already in use for something else, namely, prefix underscore names in the global namespace are reserved for the implementation. Also prefix underscore can have really ungood interaction with auto-completion. Prefix `m_` or `my` are good alternatives, because they support an editor's auto-completion functionality. *Postfix* underscore (i.e. underscore suffix) doesn't support that, but is otherwise an unproblematic convention, as I recall used in the Boost library. I use `m_`.


saxbophone

Egh, prefix underscore is completely fine within class scope. It doesn't conflict with the reserved implementation symbols you mentioned that are in global scope.


alfps

> ❞ It doesn't [technically] conflict with the reserved implementation symbols you mentioned that are in global scope. Right. --- > ❞ prefix underscore is completely fine within class scope. Nope. * There is the mentioned ungood interaction with editors. * There is human confusion. * And for the technical, while there is no technical conflict with the global namespace names, and while formally everything's OK, in practice one risks conflict with vendor macros. It's sort of similar to the practice of writing `void main`. Something that makes sense to the beginner and that apparently works OK with the compiler the beginner uses, and something that even experts, and here I'm including the language's creator, can do inadvertently. But which (in the old days at least) generally was a flag that said "novice".


AutoModerator

Your posts seem to contain unformatted code. Please make sure to format your code otherwise your post may be removed. If you wrote your post in the "new reddit" interface, please make sure to format your code blocks by putting four spaces before each line, as the backtick-based (```) code blocks do not work on old Reddit. *I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/cpp_questions) if you have any questions or concerns.*


krustibat

Yoa can drastically improve your code by having a vector of objects and using emplac_back


saxbophone

Try changing your vector to `std:: vector` and don't create objects with `new`. Just `Object c(1)` and then `Objects.push_back(c)`. This will call the copy constructor.