第十四章 内存操作API

1.

  • 运行程序无事发生
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
    int *x = NULL;
    free(x);
    return 0;
}

2.

gdb输出

(gdb) run
Starting program: /home/acs/code/chapter14/null 
warning: Error disabling address space randomization: Operation not permitted
[Inferior 1 (process 66702) exited normally]

3.

valgrind输出,没有检测出什么错误

==66806== Memcheck, a memory error detector
==66806== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==66806== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==66806== Command: ./null
==66806== 
==66806== 
==66806== HEAP SUMMARY:
==66806==     in use at exit: 0 bytes in 0 blocks
==66806==   total heap usage: 1 allocs, 1 frees, 72,704 bytes allocated
==66806== 
==66806== All heap blocks were freed -- no leaks are possible
==66806== 
==66806== For lists of detected and suppressed errors, rerun with: -s
==66806== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

4.

  • 实验代码
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
    int *x = (int*)malloc(sizeof(int));
    return 0;
}
  • gdb输出
(gdb) run
Starting program: /home/acs/code/chapter14/code4 
warning: Error disabling address space randomization: Operation not permitted
[Inferior 1 (process 66903) exited normally]
  • valgrind输出
==66911== Memcheck, a memory error detector
==66911== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==66911== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==66911== Command: ./code4
==66911== 
==66911== 
==66911== HEAP SUMMARY:
==66911==     in use at exit: 4 bytes in 1 blocks
==66911==   total heap usage: 2 allocs, 1 frees, 72,708 bytes allocated
==66911== 
==66911== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==66911==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==66911==    by 0x10919E: main (code4.cpp:7)
==66911== 
==66911== LEAK SUMMARY:
==66911==    definitely lost: 4 bytes in 1 blocks
==66911==    indirectly lost: 0 bytes in 0 blocks
==66911==      possibly lost: 0 bytes in 0 blocks
==66911==    still reachable: 0 bytes in 0 blocks
==66911==         suppressed: 0 bytes in 0 blocks
==66911== 
==66911== For lists of detected and suppressed errors, rerun with: -s
==66911== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

5.

  • 实验代码,直接运行没有反应
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
    int *data = (int *)malloc(100 * sizeof(int));
    data[100] = 0;
    return 0;
}
  • valgrind输出两个错误
==67033== Memcheck, a memory error detector
==67033== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==67033== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==67033== Command: ./code5
==67033== 
==67033== Invalid write of size 4
==67033==    at 0x1091AD: main (code5.cpp:8)
==67033==  Address 0x4da6e10 is 0 bytes after a block of size 400 alloc'd
==67033==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==67033==    by 0x10919E: main (code5.cpp:7)
==67033== 
==67033== 
==67033== HEAP SUMMARY:
==67033==     in use at exit: 400 bytes in 1 blocks
==67033==   total heap usage: 2 allocs, 1 frees, 73,104 bytes allocated
==67033== 
==67033== 400 bytes in 1 blocks are definitely lost in loss record 1 of 1
==67033==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==67033==    by 0x10919E: main (code5.cpp:7)
==67033== 
==67033== LEAK SUMMARY:
==67033==    definitely lost: 400 bytes in 1 blocks
==67033==    indirectly lost: 0 bytes in 0 blocks
==67033==      possibly lost: 0 bytes in 0 blocks
==67033==    still reachable: 0 bytes in 0 blocks
==67033==         suppressed: 0 bytes in 0 blocks
==67033== 
==67033== For lists of detected and suppressed errors, rerun with: -s
==67033== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

6.

  • 实验代码
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
    int *data = (int *)malloc(100 * sizeof(int));
    free(data);
    cout << data[0];
    return 0;
}
  • valgrind输出
==67117== Memcheck, a memory error detector
==67117== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==67117== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==67117== Command: ./code6
==67117== 
==67117== Invalid read of size 4
==67117==    at 0x1091F3: main (code6.cpp:9)
==67117==  Address 0x4da6c80 is 0 bytes inside a block of size 400 free'd
==67117==    at 0x483CA3F: free (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==67117==    by 0x1091EE: main (code6.cpp:8)
==67117==  Block was alloc'd at
==67117==    at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==67117==    by 0x1091DE: main (code6.cpp:7)
==67117== 
==67117== 
==67117== HEAP SUMMARY:
==67117==     in use at exit: 0 bytes in 0 blocks
==67117==   total heap usage: 3 allocs, 3 frees, 74,128 bytes allocated
==67117== 
==67117== All heap blocks were freed -- no leaks are possible
==67117== 
==67117== For lists of detected and suppressed errors, rerun with: -s
==67117== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

7.

  • 实验代码
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
    int *data = (int *)malloc(100 * sizeof(int));
    free(data + 10);
    return 0;
}
  • 运行结果,这次不用调试工具了
free(): invalid pointer
Aborted (core dumped)

8.

  • 实验代码
#include <sys/types.h>
#include <unistd.h>
#include <iostream>
using namespace std;
int main()
{
    int *data = (int *)malloc(2 * sizeof(int));
    data = (int *)realloc(data, 11 * sizeof(int));
    data[10] = 100;
    free(data);
    return 0;
}
  • valgrind输出,没有报错
==67384== Memcheck, a memory error detector
==67384== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==67384== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==67384== Command: ./code8
==67384== 
==67384== 
==67384== HEAP SUMMARY:
==67384==     in use at exit: 0 bytes in 0 blocks
==67384==   total heap usage: 3 allocs, 3 frees, 72,756 bytes allocated
==67384== 
==67384== All heap blocks were freed -- no leaks are possible
==67384== 
==67384== For lists of detected and suppressed errors, rerun with: -s
==67384== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

posted @   穿过雾的阴霾  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示