This repository was archived by the owner on May 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cc
More file actions
84 lines (78 loc) · 2.19 KB
/
Copy pathmain.cc
File metadata and controls
84 lines (78 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*****************************************************************************/
// Author: Xuefeng Ding <xuefengd@princeton.edu>
// Institute: Department of Physics, Princeton University
// Date: 3/4/22
// Version: v1.0
// Description: GooStats, a statistical analysis toolkit that runs on GPU.
//
// All rights reserved. 2022 copyrighted.
/*****************************************************************************/
#include <iostream>
#include <cstring>
#include "C.h"
void testA() {
int *a = new int[3]{1, 2, 3};
std::cout << "ok: " << a[2] << std::endl;
delete[] a;
std::cout << "dangling pointer: " << a[2] << std::endl;// inspect will find it with static analysis
}
namespace B {
int global[3]{};
int *pa() {
int a[3];
return &a[0];
}
void testB(int argc) {
int *a[3] = {new int, new int, new int};
delete a[argc];
std::cout << argc << " dangling pointer" << *a[argc] << std::endl;// need dynamic analysis
std::cout << "memory leak" << std::endl;
int b[3] = {};
int c[3];
int *d = new int[3]{1, 3};
int *e = new int[3];
std::cout << "uninitialized var " << c[argc] << std::endl;
std::cout << "uninitialized var " << e[argc] << std::endl;
std::cout << "over shoot stack " << b[2 + argc] << std::endl;
std::cout << "over shoot heap " << d[2 + argc] << std::endl;
std::cout << "over shoot global " << global[2 + argc] << std::endl;
std::cout << "use after return" << pa()[2 + argc] << std::endl;
int *f;
{
int a[3];
f = a;
}
std::cout << "use after scope" << f[2 + argc] << std::endl;
int *p = static_cast<int*>(malloc(sizeof c));
memcpy(p,e,sizeof c);
std::cout<<p[1]<<" "<<sizeof c<<" "<<sizeof(c)<<" "<<sizeof e<<" "<<sizeof(e)<<std::endl;
}
}// namespace C
void testC() {
C::A a;
a.supp();
C::supp();
}
#include "D.h"
void testD() {
D::test();
E *e = new E{};
// D d(e);
E m { std::move(*e)};
E n { std::move(*e)};
E o { std::move(m)};
E p { std::move(m)};
E q;
E r { std::move(q)};
delete e;
// std::cout<<"move?"<<std::endl;
// D d2(std::move(d));
// std::cout<<"move."<<std::endl;
}
int main(int argc, char *[]) {
testA();
B::testB(argc);
testC();
testD();
return 0;
}