search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

C/C++動態配置記憶體_記憶體洩漏介紹 – jashliao部落格

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.

圖文版完整內容:



熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦