0
SomeClass* a = new SomeClass;

Is straightforward, but I have heard that std::vector is preferable to new. But,

std::vector<SomeClass> a(1);

also feels weird, because when I use a[0], it's not clear that I'm using it as a variable and not an array. Is there nothing wrong with that, or is there a better way to do this?

Edit: What I specifically want to do is to create a class instance in a function and return it without copying it.

Edit: Changed int to SomeClass.

ssamtkwon
  • 93
  • 12
  • Counter question: Why would one ever want or need to dynamically allocate a single variable? YAGNI is the only correct answer. – πάντα ῥεῖ Sep 13 '20 at 09:26
  • Okay, I should have been more specific - how do I dynamically allocate a single class? – ssamtkwon Sep 13 '20 at 09:28
  • 1
    std::make_unique? – JVApen Sep 13 '20 at 09:28
  • 2
    `std::unique_ptr` you dont need to new/free yourself this way. – Raildex Sep 13 '20 at 09:28
  • If you consider `std::vector a(1);` to be acceptable (if a bit weird) then what is wrong with `int a;`? That's not weird at all and seems to do everything you want. And exactly the same applies if you are actually talking about classes and not ints. Until you explain why you need to dynamically allocate anything at all it's going to be hard to advise. – john Sep 13 '20 at 09:34
  • 1
    Use `std::unique_ptr` or `std::shared_ptr`. – HolyBlackCat Sep 13 '20 at 09:36
  • Well `std::vector a(1);` would not achieve the editted goal. Two options really, move semantics (advanced topic) or `std::unique_ptr`. – john Sep 13 '20 at 09:39
  • @ssamtkwon Please [edit] your question to give a better example than just `int`, Asking about class or struct variables is a completely different case. – πάντα ῥεῖ Sep 13 '20 at 09:50

2 Answers2

4

If you want to dynamically allocate an instance of a class then use a smart pointer: std::unique_ptr or std::shared_ptr which are constructible via make_unique and make_shared respectively.

There are also libraries with other smart pointers you could use.

Incase of allocating an int... I mean there could be reasons but normally you should just keep something this small on the stack.

ALX23z
  • 4,456
  • 1
  • 11
  • 18
0

or is there a better way to do this?

Sure, there is a better way, as long we talk about primitive types, or small classes / structs: Just don't.

You can always do that on the stack, I can't see any reason why you ever would need to do that with dynamic memory allocation (unless some esoteric reasons, which would just indicate some serious design flaws).


As for the OP's comment regarding classes / structs:

  1. If a class grows that big memberwise, that it could be problematic to allocate it on the stack, it's probably better to change the internal class design to do the dynamic allocations internally (e.g. change a large std::array<T,HUGEVAL> member variable to a std::vector<T>).
  2. Alternatively if you really decide not to, or can't change the class prefer to use a std::vector. Using new and delete manually is too error prone and should be avoided by all means. There's practically no overhead using a std::vector, and if your HW is so limited it can't bear that small overhead, dynamic memory allocation won't be available in most cases anyways.
  3. As the OP pointed out in comments they want to know about the case for class or struct variables: The c++ standard provides std::unique_ptr and std::shared_ptr that can be used to hold and manage a single dynamically allocated type instance, without paying for the std::vector overhead.

As for the OPs edit (emphasis mine):

What I specifically want to do is to create a class instance in a function and return it without copying it.

There<'s no need to worry about copies made on return from a function. Modern c++ compilers support RVO (return value optimization), which effectively moves the returned value (see also: Is RVO (Return Value Optimization) applicable for all objects?).

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Maybe I want to return a class in a function, and don't want to copy the whole thing? If I misunderstood something, please correct me. – ssamtkwon Sep 13 '20 at 09:36
  • 1
    @ssamtkwon Regarding the return case, you usually don't need to worry. We have RVO (return value optimization), which will effectively move the value, instead of copying it. – πάντα ῥεῖ Sep 13 '20 at 09:54