Reputation: 3366
Basically, the question is about how the memory is managed by the compiler.
double[] b = {1.0, 2.0, 3.0};
double[] a = new double[3]; //for Java
double a[3]; //for C++
a = b;
When array a
is declared, 24 bytes memory space are assigned to array a
.
a=b
simply copies the value of the reference of array b
to a
.
What about the 24 bytes memory block? It will be deallocated immediately, or reclaimed by the end of the function/method?
Upvotes: 0
Views: 125
Reputation: 3366
Now I understand.
In Java
double[] b = {1.0, 2.0, 3.0};
double[] a = new double[3];
a = b;
The declaration of a
will not result in a 24 bytes memory block allocation. Only a reference variable is created.
In C++, the code should be
void myArray(double a[], int size_a){
double c = 4.0;
a = &c;
cout << *a;
}
int main(){
double b[] = {1.0, 2.0, 3.0};
myArray(b, 3);
return 0;
}
In this case, a
is a pointer. In stead of passing the entire array to the function, only the starting address of the array is copied to a
.
In a nutshell, in both languages only one array is created in the memory. The program will not become cumbersome after the above operations.
Upvotes: 0
Reputation: 8972
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)
Upvotes: 0
Reputation: 7118
In C++, the following code snippet will provide error as invalid array assignment while doing
a = b;
Code snippet used:
double b[] = {1.0, 2.0, 3.0};
double a[3]; //for C++
a = b; // error: invalid array assignment
instead you have to do:
memcpy(a, b, sizeof(a));
In C++, an array has a fixed starting (base) address, which cannot be re-assigned. In Java, array is similar as pointer in C++ (called reference in Java), so can be assigned.
Upvotes: 0
Reputation: 310980
When array a is declared, 24 bytes memory space are assigned to array a.
In C++. In Java only a reference is allocated on the declaration, and the array itself is allocated in the initializer.
a=b simply copies the value of the reference of array b to a.
In Java. In C++ it isn't legal code.
What about the 24 bytes memory block? It will be deallocated immediately, or reclaimed by the end of the function/method?
In C++ it disappears with the enclosing scope or more probably the enclosing method. In Java it becomes eligible for garbage collection when the last reference to it disappears, which in this case is the end of the method.
Your question is really too mixed up to really make much more sense out of than that. Try thinking in one language at a time.
Upvotes: 1
Reputation: 664
Java runs a garbage collector when a variable goes out of scope. In this case, if the last method in the function was a = b
and there were no other references to a
or b
in the code, Java would deallocate the memory used for the arrays.
Upvotes: 0