Skip to content

Instantly share code, notes, and snippets.

@rinfz
Created March 6, 2019 22:06
Show Gist options
  • Save rinfz/370611cf24fb4ce3fddee0a171da2bc7 to your computer and use it in GitHub Desktop.
Save rinfz/370611cf24fb4ce3fddee0a171da2bc7 to your computer and use it in GitHub Desktop.
Pointers and stuff
#include <iostream>
#include <memory>
#include <cstdlib>
#include <vector>
void incr(float *arr) {
arr[5] += 1;
arr[7] += 1;
}
int main() {
float *xs = (float*) malloc(sizeof(float) * 10);
std::shared_ptr<float>(xs, free);
xs[0] = 5;
xs[9] = 15;
std::cout << xs[0] * xs[9] << '\n';
//auto ys = std::make_shared<float[10]>(new float[10]);
std::shared_ptr<float[]> ys(new float[10]);
ys[5] = 5;
ys[7] = 5;
std::cout << ys[5] * ys[7] << '\n';
incr(ys.get());
std::cout << ys[5] * ys[7] << '\n';
std::vector<float> zs(10, 0);
zs[5] = 6;
zs[7] = 6;
std::cout << zs[5] * zs[7] << '\n';
incr(zs.data());
std::cout << zs[5] * zs[7] << '\n';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment