When creating a new object, i can't decide to use "static allocation" or "dynamic allocation".
Ex:

class A
{...};

int main()
{
    // A theObject;
   // or A theObject = new A();  ?
}

In some more complex cases, i don't know what i should do.
Ex:
Use this:

vector<vector<Student>> a;

Or this:

vector<vector<Student *>> a;

accompanied by

a.resize(someSize);
for(i = 0; i < (int)a.size(); ++i)
   a[i] = new Student();

Please explain to me how to use them properly. Thanks thousands.

Dani AI

Generated

A few clarifications and actionable rules of thumb that address the answers above and the original confusion from . The word "static" is often used informally to mean "stack/automatic", but in C++ it has a precise meaning (static storage duration). The three storage categories to keep in mind are automatic (local variables), static (globals or static variables) and dynamic/free-store (what new uses). See storage durations (automatic, static, dynamic) for the formal definitions.

Concrete guidance: prefer value semantics — create objects directly and store them in containers by value when you can. That gives a single allocation for a std::vector, good cache locality, and automatic cleanup. Use heap allocation (and therefore pointers) when you need one of these: polymorphism (avoid slicing), objects with non-copyable semantics, stable object addresses independent of container reallocation, shared ownership, or truly huge objects that you cannot put on the stack. This follows and expands on ’s lifetime point: heap objects outlive the creating function until explicitly released.

In modern C++ avoid raw new/delete. Express ownership with RAII and smart pointers. Example pattern for polymorphic or owning pointers:

std::vector<std::unique_ptr<Base>> items;
items.reserve(N);
items.emplace_back(std::make_unique<Derived>(/*args*/));

Use std::unique_ptr for exclusive ownership, std::shared_ptr only when ownership must be shared (watch for cycles and use std::weak_ptr), and std::optional<T> for "maybe present" values. See std::unique_ptr and std::vector.

Quick checklist: (1) choose values unless you need pointers for one of the reasons above, (2) prefer smart pointers over raw owning pointers, (3) prefer returning objects by value (RVO/moves) instead of returning raw heap pointers — see copy elision / RVO.

Recommended Answers

All 3 Replies

use static allocation unless (1) your os and compiler have very limited stack space, or (2) the objects are huge. Declaring STL containers like vector dynamicall is just a waste of time/effort because they only consume about 20 bytes of memory anyway.

Additionally, are there any cases we use this?
vector<int> *a = new vector<int>;

I still don't understand when i should use 'static' or 'dynamic' T_T

It's a matter of where you want to store the object, and whether you wish it to remain in existence after the function in which it was created has returned.

What you are calling "static" allocation (more properly "automatic") saves the data on the stack. Dynamic allocation stores the data in the free store (a.k.a., the heap).

When data is stored on the stack, it disappears when the function that allocated it returns. By storing the data in the free store, you can return it from a function by returning its address, which will still be valid after the function's stack space is freed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.