Skip to content

Commit 0e4a1bc

Browse files
committed
use std::vector instead of make_unique
1 parent 17badad commit 0e4a1bc

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

test/contiguous/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ float* p = malloc(N * sizeof(float));
1616
p[0] = a[2];
1717
p[1] = a[1];
1818
p[2] = a[0];
19+
free(a);
1920

2021
asub(&p[0], &N);
2122

22-
free(a);
2323
free(p);
2424

2525
printf("OK: C contiguous array\n");

test/contiguous/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// std::vector is contiguous since C++17
2+
// https://en.cppreference.com/w/cpp/named_req/ContiguousContainer
3+
// https://en.cppreference.com/w/cpp/container/vector
4+
15
#include <cstdlib>
2-
#include <memory>
6+
#include <vector>
37
#include <iostream>
48

59
#include "contiguous.h"
@@ -8,12 +12,12 @@ int main(){
812

913
size_t dims[1] = {3};
1014

11-
auto a = std::make_unique<float[]>(3);
15+
std::vector<float> a(3);
1216

1317
for (size_t i = 0; i < 3; ++i)
14-
a.get()[i] = i+1;
18+
a[i] = i+1;
1519

16-
asub(&a.get()[0], dims);
20+
asub(&a[0], dims);
1721

1822
std::cout << "OK: C++ contiguous array\n";
1923

test/malloc/main.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <iostream>
2-
#include <memory>
2+
#include <vector>
33
#include <cstdlib>
44

55
#include "my_vector.h"
@@ -9,22 +9,22 @@ int main()
99
{
1010
std::size_t N = 3;
1111

12-
auto x = std::make_unique<int[]>(N);
13-
auto x2 = std::make_unique<int[]>(N);
12+
std::vector<int> x(N);
13+
std::vector<int> x2(N);
1414

1515
for (size_t i = 0; i < N; ++i)
16-
x.get()[i] = i+1;
16+
x[i] = i+1;
1717

18-
timestwo_f(&x.get()[0], &x2.get()[0], &N);
18+
timestwo_f(&x[0], &x2[0], &N);
1919

2020
for (auto i=0u; i < N; i++){
21-
if (x2.get()[i] != 2*x.get()[i]){
22-
std::cerr << "value " << x2.get()[i] << "!=" << x.get()[i] << std::endl;
21+
if (x2[i] != 2 * x[i]){
22+
std::cerr << "value " << x2[i] << "!=" << x[i] << std::endl;
2323
return EXIT_FAILURE;
2424
}
2525
}
2626

27-
std::cout << "OK: C++ make_unique\n";
27+
std::cout << "OK: C++ std::vector pre-allocated\n";
2828

2929
return EXIT_SUCCESS;
3030
}

test/pointer/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <iostream>
2-
#include <memory>
2+
#include <vector>
33
#include <cstdlib>
44

55
#include "my_pointer.h"
@@ -8,16 +8,16 @@
88
int main()
99
{
1010
size_t N = 3;
11-
auto a = std::make_unique<float[]>(N);
12-
auto b = std::make_unique<float[]>(N-1);
11+
std::vector<float> a(N);
12+
std::vector<float> b(N-1);
1313

1414
for (size_t i = 0; i < 3; ++i)
15-
a.get()[i] = i+1;
15+
a[i] = i+1;
1616

17-
point23(&a.get()[0], &b.get()[0], &N);
17+
point23(&a[0], &b[0], &N);
1818

19-
if (b.get()[0] != a.get()[1] || b.get()[1] != a.get()[2]){
20-
std::cerr << "value " << b.get()[0] << "!=" << a.get()[1] << " or " << b.get()[1] << "!=" << a.get()[2] << std::endl;
19+
if (b[0] != a[1] || b[1] != a[2]){
20+
std::cerr << "value " << b[0] << "!=" << a[1] << " or " << b[1] << "!=" << a[2] << std::endl;
2121
return EXIT_FAILURE;
2222
}
2323

test/vector/main.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,20 @@ int main()
99

1010
std::vector<int> x = {0, 1, 2};
1111
auto N = x.size();
12+
1213
std::vector<int> x2(N);
1314

1415
timestwo_f(&x.front(), &x2.front(), &N);
1516

1617
for (auto i=0u; i < x2.size(); i++){
18+
std::cout << x2[i] << "\n";
1719
if (x2[i] != 2*x[i]){
1820
std::cerr << "value " << x2[i] << "!=" << x[i] << std::endl;
1921
return EXIT_FAILURE;
2022
}
2123
}
2224

23-
std::cout << "OK: vector\n";
25+
std::cout << "OK: C++ std::vector\n";
2426

2527
return EXIT_SUCCESS;
2628
}

0 commit comments

Comments
 (0)