Does the "auto" keyword in C++ have anything to do with storage class For example:
void foo() {
auto ptr = new int[9]
}
Does the pointer to int above have is automatic(stack), or dynamic(heap)?
Until c++11
, auto
was used to specify automatic
storage duration. But since c++11
its only meaning is that the type of the variable is automatically deduced. It has nothing to do wiith the storage-class of the variable itself.
In your case ptr
is a local variable (int * ptr
) pointing to a location on the heap. You can always get the same effect by explicitely writing the types of the variables as in the following:
void foo() {
int* ptr = new int[9];
}
Please take a look at this link for more details and on how the deduction process works.
auto
is not used in C++11 as a storage-class specifier.int *ptr = new int[9];
, But modern C++ doesn't like using (or abusing)new
anddelete
, you should rethink your app in terms ofvector
,array
,shared_ptr
,unique_ptr
,weak_ptr
.