'Using std::copy to deep copy 2D array in C++ got Segmentation fault
I want to use the following method to deep copy two-dimensional array a to two-dimensional array b, but when running to std::copy, Segmentation fault is reported.
int a_l = 1000;
int b_l = 513;
double ** a;
double ** b;
// Allocate memory
a = new double *[a_l];
for (int i = 0; i < a_l; ++i) {
a[i] = new double[b_l];
}
// Get data for array a
GetData(a);
// Copy a -> b
b = new double *[a_l];
for (int i = 0; i < a_l; ++i) {
b[i] = new double[b_l];
}
std::copy(&a[0][0], &a[0][0] + a_l * b_l, &b[0][0]);
Solution 1:[1]
Just use std::array or std::vector and you can simply use a = b; to copy.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Goswin von Brederlow |
