From a7b6b88677cd224e86e84d2e247c1aac7607d213 Mon Sep 17 00:00:00 2001 From: 185******05 <852159247@qq.com> Date: Wed, 19 Jul 2023 13:50:46 +0000 Subject: [PATCH] the difference between malloc and new Signed-off-by: 185******05 <852159247@qq.com> --- C++/malloc_error.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 C++/malloc_error.cpp diff --git a/C++/malloc_error.cpp b/C++/malloc_error.cpp new file mode 100644 index 0000000..1eb3532 --- /dev/null +++ b/C++/malloc_error.cpp @@ -0,0 +1,15 @@ +#include +#include +using namespace std; + +struct A { + vector v; +}; + +int main() { + // Init the struct with 'malloc' which has a vector + A* a = reinterpret_cast(malloc(sizeof(A))); + // unexpected action + a->v.push_back(100); + return 0; +} \ No newline at end of file -- Gitee