gpt4 book ai didi

c - 有一些我没用过的奇怪的 malloc 函数

转载 作者:太空宇宙 更新时间:2023-11-04 02:06:21 29 4
gpt4 key购买 nike

我使用 pin 来分析我的简单程序。

它有 4 个 malloc 函数,但是,当我将 pin 与名为 malloctrace 的 pintool 一起使用时,它显示超过 4 个 malloc。

这些 malloc 函数是什么?

我的操作系统是 Ubuntu 12.04 64 位。以下项目是我制作的代码和 pintool 的结果。

#include <stdio.h>
#define SIZE 100

int main()
{
int *test1 = (int*)malloc(SIZE* sizeof(int));
int *test2 = (int*)malloc(SIZE* sizeof(int));
int i, j;
int *test3 = (int*)malloc(16*sizeof(int));
int *test4 = (char*)malloc(SIZE* sizeof(int));

test1[0] = 2;
test1[2] = 3;
test2[0] = 5;

printf("test1's addr : %p , test1's val = %d \n", test1, test1[0]);
printf("test1's addr : %p , test1's val = %d \n", &test1[1], test1[1]);
printf("test2's addr : %p , test2's val = %d \n", &test1[9], test1[9]);


return 0;
}

       name           size          start_addr           malloc_IP          access_cnt
malloc 0x589 0 0x7ffff7de557e 0
malloc 0x489 0 0x7ffff7de557e 0
malloc 0xf 0 0x7ffff7ddd29e 0
malloc 0x4b 0 0x7ffff7df01b2 0
malloc 0x28 0 0x7ffff7de1fe7 0
malloc 0x14a0 0 0x7ffff7de202c 0
malloc 0x28 0 0x7ffff7de22ad 0
malloc 0x48 0 0x7ffff7ddf9d3 0
malloc 0x5c 0 0x7ffff7ddf9d3 0
malloc 0x5c 0 0x7ffff7ddf9d3 0
malloc 0x39 0 0x7ffff7ddf9d3 0
malloc 0x20 0 0x7ffff7de294e 0
malloc 0x492 0 0x7ffff7de557e 0
malloc 0x20 0 0x7ffff7de57ed 0
malloc 0x28 0 0x7ffff7de776f 0
malloc 0x38 0 0x7ffff7de7900 0
malloc 0x48 0 0x7ffff7deab5a 0
malloc 0x48 0 0x7ffff7deab5a 0
malloc 0x228 0 0x7ffff7deab5a 0
malloc 0x90 0 0x7ffff7deab5a 0
malloc 0x410 0 0x7ffff7ddaf22 0
malloc 0x110 0 0x7ffff7debd52 0
malloc 0x190 0 0x4013d2 0
malloc 0x190 0x603010 0x4013d2 0
malloc 0x190 0x6031b0 0x4013e0 0
malloc 0x40 0x603350 0x4013ee 0
malloc 0x190 0x6033a0 0x4013fc 0
free 0 0 0x401688 0
free 0 0 0x401688 0
free 0 0 0x401688 0
free 0 0 0x401688 0
free 0 0 0x4016b0 0
free 0 0 0x4016b0 0
free 0 0 0x4016d7 0
free 0 0 0x4016d7 0
free 0 0 0x4016d7 0
free 0 0 0x4016d7 0
free 1 0 0x4016e8 0
free 0 0 0x4016e8 0
free 0 0 0x401718 0
free 0 0 0x401718 0

最佳答案

很可能其他东西可能正在调用 malloc在幕后,包括 C 运行时代码(例如,用于 strtok 等事物使用的线程特定数据)甚至您的分析工具本身。

如果你检查所有这些内存块的起始地址,你会注意到除了你做的那些之外,所有的都是0。 (100 个 4 字节 int 变量占用 4000x190 字节,其中 16 个占用 640x40 字节)。

可能在这里是相关的,尽管它也可能是你是唯一一个没有清理自己的人:-)


顺便说一下,你不应该转换 malloc 的返回值在 C 中,因为它可以隐藏某些细微的错误,例如当您的 int和指针大小是不同的宽度,你忘记包括 stdlib.h ,如果您在 64 位环境中运行,这两者在这里都是可能的。

C 完全能够隐式转换 void *malloc 返回转换成任何其他指针类型。

关于c - 有一些我没用过的奇怪的 malloc 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236321/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com