Skip to content

Instantly share code, notes, and snippets.

@alchem0x2A
Created April 20, 2018 16:07
Show Gist options
  • Save alchem0x2A/a244b20b74efafe47b1e10fdd414b186 to your computer and use it in GitHub Desktop.
Save alchem0x2A/a244b20b74efafe47b1e10fdd414b186 to your computer and use it in GitHub Desktop.
Memory copy of pointers in c++
#include <iostream>
#include <cstring> //memcpy implemented here
// Copy pointer y from x
void copy_pointer(double *y, const double *x, size_t n){
// The size in memcpy is byte length!
std::memcpy(y, x, n * sizeof(double));
}
int main() {
double y[5];
double x[5] = {1.0, 2.0, 3.0, 4.0, 5.0};
copy_pointer(y, x, 5);
for (size_t i=0; i<5; ++i)
std::cout << x[i] << " "
<< y[i] << " " << std::endl;
// std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment