Zi 字媒體
2017-07-25T20:27:27+00:00
C/C++動態配置記憶體_記憶體洩漏介紹
資料來源: https://mp.weixin.qq.com/s/WvAsjLhgGlRJsxFPLAI3lw
C code
/*
https://mp.weixin.qq.com/s/WvAsjLhgGlRJsxFPLAI3lw
https://www.tutorialspoint.com/compile_c_online.php
*/
#include
int main()
{
char arr_char[1024*1000000];
arr_char[0] = '0';
}
/*
$gcc -o main *.c
$main
/usr/bin/timeout: the monitored command dumped core
sh: line 1: 182320 Segmentation fault /usr/bin/timeout 10s main
*/
/*
https://mp.weixin.qq.com/s/WvAsjLhgGlRJsxFPLAI3lw
https://www.tutorialspoint.com/compile_c_online.php
*/
#include
#include
#include
int main()
{
char *p1 = (char *)malloc(1024*1000000);
strcpy(p1, "这里是堆区");
printf("%s\n", p1);
free(p1);
}
/*
$gcc -o main *.c
$main
这里是堆区
*/
© 2021 GitHub, Inc.
C++ code
//https://mp.weixin.qq.com/s/WvAsjLhgGlRJsxFPLAI3lw
//https://www.tutorialspoint.com/compile_cpp_online.php
#include
using namespace std;
class Babe
{
public:
Babe()
{
cout << "Create a Babe to talk with me" << endl;
}
~Babe()
{
cout << "Babe don\'t Go away,listen to me" << endl;
}
};
int main()
{
int *a = new int[10];
for(int i=0;i<10;i++)
{
a[i]=i;
cout<< a[i]< delete pbabe;
pbabe = new Babe[3];
delete[] pbabe;
return 0;
}
/*
$g++ -o main *.cpp
$main
0 1 2 3 4 5 6 7 8 9
Create a Babe to talk with me
Create a Babe to talk with me
Create a Babe to talk with me
Babe don't Go away,listen to me
Babe don't Go away,listen to me
Babe don't Go away,listen to me
Create a Babe to talk with me
Create a Babe to talk with me
Create a Babe to talk with me
Babe don't Go away,listen to me
Babe don't Go away,listen to me
Babe don't Go away,listen to me
*/
© 2021 GitHub, Inc.
圖文版完整內容:
寫了
5860316篇文章,獲得
23313次喜歡