Is a Java array of primitives stored in stack or heap?

As gurukulki said, it’s stored on the heap. However, your post suggested a misunderstanding probably due to some well-intentioned person propagating the myth that “primitives always live on the stack”. This is untrue. Local variables have their values on the stack, but not all primitive variables are local… For example, consider this: public class Foo … Read more

When vectors are allocated, do they use memory on the heap or the stack?

vector<Type> vect; will allocate the vector, i.e. the header info, on the stack, but the elements on the free store (“heap”). vector<Type> *vect = new vector<Type>; allocates everything on the free store. vector<Type*> vect; will allocate the vector on the stack and a bunch of pointers on the free store, but where these point is … Read more

Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management?

Automatic tells me something about the lifetime of an object: specifically that it is bound automatically to the enclosing scope, and will be destroyed automatically when that scope exits. Dynamic tells me that the lifetime of an object is not controlled automatically by the compiler, but is under my direct control. Stack is an overloaded … Read more