in C++, your code would not work; either you'd have to have something like:
#include <iostream>
#include <algorithm>
int main() {
int b[] = { 4, 5, 6 };
int a[3];
std::copy_n(std::begin(b), std::end(a)-std::begin(a), std::begin(a));
// the above is safer than just
// std::copy(std::begin(b), std::end(b), std::begin(a))
// in the case that the size of b is greater than the size of a...
a[2] = 9;
std::cout << b[2] << ' ' << a[2] << '\n';
}
(in which case the memory for both a and b are on the stack) or its newer C++11 equivalent:
#include <iostream>
#include <memory>
int main() {
std::array<int, 3> b { 4, 5, 6 };
auto a = b; // or std::array<int, 3> a = b;
a[2] = 9;
std::cout << b[2] << ' ' << a[2] << '\n';
}
or, if you want resizable arrays like java has, you'd have to use vector:
#include <iostream>
#include <vector>
int main() {
std::vector<int> b { 4, 5, 6 };
auto a = b;
a[2] = 9;
std::cout << b[2] << ' ' << a[2] << '\n';
}
In all cases, as you can see if you run the examples, the array is copied item by item (all three examples print 6 9), so the storage for each array would occupy the space for three int variables (or 24 bytes, in 64-bit machines) -- plus the overhead for vector in the last case...
Answering your question: in the first two cases, as the 24 bytes are allocated on the stack, it's deallocated automagically at the return. In the last case, the destructor for std::vector takes care of deallocating the memory on the heap, too.
EDIT: I forgot to put an example where, as in Java, the a is just a copy of the reference to the same underlying storage as is b:
#include <iostream>
#include <memory>
int main() {
std::array<int, 3> b { 4, 5, 6 };
auto& a = b; // or std::array<int, 3>& a = b;
a[2] = 9;
std::cout << b[2] << ' ' << a[2] << '\n';
}
(this would work with vector also, and it prints 9 9 when run)
double b[] = { ... }and second,a = bis invalid.a(about 48 bytes, BTW) will be garbage collected.