Skip to content

Commit cd07935

Browse files
committed
improve c++ quality
1 parent 437956c commit cd07935

File tree

13 files changed

+42
-39
lines changed

13 files changed

+42
-39
lines changed

app/allocate/interactive.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
#include <iostream>
22
#include <cstring>
33
#include <cstdlib>
4+
#include <vector>
45

56
#include "my_alloc.h"
67

78

89
int main(){
910

10-
size_t d1[1] = {1000000000};
11+
std::vector<size_t> d1 = {1000000000};
1112

1213
float *A1;
1314

1415

15-
falloc1(&A1, d1);
16+
falloc1(&A1, &d1.front());
1617
std::cout << "1D: allocated\n";
1718

1819
std::cout << "press enter to deallocate 1D\n";

src/sleep/sleep.c

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
#include <stdio.h>
22

3-
void c_sleep(int);
4-
53
#ifdef _MSC_VER
6-
74
#define WIN32_LEAN_AND_MEAN
85
#include <windows.h>
6+
#else
7+
#include <time.h>
8+
#include <errno.h>
9+
#endif
10+
11+
12+
void c_sleep(int);
913

1014
// https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-sleep
1115
void c_sleep(int milliseconds)
1216
{
13-
Sleep(milliseconds);
14-
}
1517

18+
#ifdef _MSC_VER
19+
Sleep(milliseconds);
1620
#else
17-
18-
#include <time.h>
19-
#include <errno.h>
2021
// https://linux.die.net/man/3/usleep
21-
void c_sleep(int milliseconds)
22-
{
23-
2422
if (milliseconds <= 0){
2523
fprintf(stderr, "ERROR:sleep: milliseconds must be strictly positive\n");
2624
return;
@@ -52,6 +50,6 @@ void c_sleep(int milliseconds)
5250
fprintf(stderr, "nanosleep() error\n");
5351
break;
5452
}
55-
}
56-
5753
#endif
54+
55+
}

src/vector/lib.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <stddef.h>
22

3-
void timestwo_c(int x[], int x2[], size_t N){
3+
#include "my_vector.h"
4+
5+
void timestwo_c(const int x[], int x2[], const size_t N){
46
for (size_t i=0; i<N; i++)
57
x2[i] = x[i] * 2;
68
}

src/vector/lib.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
// or whatever you name this library file.
55
#include <cstddef>
66

7-
extern "C" void timestwo_cpp(int x[], int x2[], std::size_t N){
8-
for (std::size_t i=0; i<N; i++)
7+
#include "my_vector.h"
8+
9+
void timestwo_cpp(const int x[], int x2[], const size_t N){
10+
for (size_t i=0; i<N; i++)
911
x2[i] = x[i] * 2;
1012
}

src/vector/mathv.f90

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module mathv
99
pure subroutine timestwo_f(z, z2, N) bind(c)
1010
! elemental is not allowed with BIND(C)
1111

12-
integer(C_SIZE_T), intent(in) :: N
12+
integer(C_SIZE_T), intent(in), value :: N
1313
integer(c_int),intent(in) :: z(N)
1414
integer(c_int),intent(out) :: z2(N)
1515

src/vector/my_vector.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
extern "C" {
33
#endif
44

5-
void timestwo_f(int [], int [], size_t*);
6-
void timestwo_c(int [], int [], size_t*);
7-
void timestwo_cpp(int [], int [], size_t*);
5+
void timestwo_f(const int [], int [], const size_t);
6+
void timestwo_c(const int [], int [], const size_t);
7+
void timestwo_cpp(const int [], int [], const size_t);
88

99
#ifdef __cplusplus
1010
}

test/array/array.f90

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ program array
88
interface
99
subroutine timestwo_c(v, N) bind(C)
1010
import
11-
integer(C_SIZE_T), intent(in) :: N
11+
integer(C_SIZE_T), intent(in), value :: N
1212
integer(C_INT), intent(inout) :: v(N)
1313
end subroutine
1414

1515
subroutine timestwo_cpp(v, N) bind(C)
1616
import
17-
integer(C_SIZE_T), intent(in) :: N
17+
integer(C_SIZE_T), intent(in), value :: N
1818
integer(C_INT), intent(inout) :: v(N)
1919
end subroutine
2020
end interface

test/array/lib.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#include <stddef.h>
22

3-
void timestwo_c(int*, size_t*);
3+
void timestwo_c(int*, size_t);
44

5-
void timestwo_c(int* A, size_t* N)
5+
void timestwo_c(int* A, size_t N)
66
{
7-
for (size_t i=0; i < *N; i++){
7+
for (size_t i=0; i < N; i++){
88
A[i] = 2*A[i];
99
}
1010
}

test/array/lib.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <cstddef>
22

3-
extern "C" void timestwo_cpp(int* A, size_t* N)
3+
extern "C" void timestwo_cpp(int* A, const size_t N)
44
{
5-
for (auto i=0u; i < *N; i++){
5+
for (auto i=0u; i < N; i++){
66
A[i] = 2*A[i];
77
}
88
}

test/array/main.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
int main()
88
{
9-
const std::size_t Nc = 3;
10-
std::size_t N = Nc;
9+
const size_t Nc = 3;
10+
const size_t N = Nc;
1111

12-
std::array<int, Nc> x = {0, 1, 2};
12+
const std::array<int, Nc> x = {0, 1, 2};
1313
std::array<int, Nc> x2;
1414

15-
timestwo_f(&x.front(), &x2.front(), &N);
15+
timestwo_f(&x.front(), &x2.front(), N);
1616

1717
for (auto i=0u; i < x2.size(); i++){
1818
if (x2[i] != 2*x[i]){

test/malloc/main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ int main(void)
1414
x[i] = i+1;
1515
}
1616

17-
timestwo_f(&x[0], &x2[0], &N);
17+
timestwo_f(&x[0], &x2[0], N);
1818

1919
for (size_t i=0; i < N; ++i){
2020
if (x2[i] != 2*x[i]){

test/malloc/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77

88
int main()
99
{
10-
std::size_t N = 3;
10+
constexpr size_t N = 3;
1111

1212
std::vector<int> x(N);
1313
std::vector<int> x2(N);
1414

1515
for (size_t i = 0; i < N; ++i)
1616
x[i] = i+1;
1717

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

2020
for (auto i=0u; i < N; i++){
2121
if (x2[i] != 2 * x[i]){

test/vector/main.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
int main()
88
{
99

10-
std::vector<int> x = {0, 1, 2};
11-
auto N = x.size();
10+
const std::vector<int> x = {0, 1, 2};
11+
const size_t N = x.size();
1212

1313
std::vector<int> x2(N);
1414

15-
timestwo_f(&x.front(), &x2.front(), &N);
15+
timestwo_f(&x.front(), &x2.front(), N);
1616

1717
for (auto i=0u; i < x2.size(); i++){
1818
std::cout << x2[i] << "\n";

0 commit comments

Comments
 (0)