malloc() 和 free() 在哪里存储分配的大小和地址? [英] Where do malloc() and free() store allocated sizes and addresses?

查看:25
本文介绍了malloc() 和 free() 在哪里存储分配的大小和地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

malloc()free() 在哪里存储分配的地址及其大小(Linux GCC)?我已经读过一些实现将它们存储在实际分配的内存之前的某个地方,但我无法在我的测试中确认这一点.

Where do malloc() and free() store the allocated addresses and their sizes (Linux GCC)? I've read that some implementations store them somewhere before the actual allocated memory, but I could not confirm that in my tests.

背景,也许有人对此有另一个提示:

The background, maybe someone has another tip for this:

我正在尝试分析一个进程的堆内存,以确定另一个进程中字符串的当前值.访问进程堆内存并浏览它是没有问题的.但是,由于字符串的值发生变化,并且进程每次都会分配新的内存部分,因此字符串的地址也会发生变化.由于字符串具有固定格式,因此仍然很容易找到,但是经过一些更改后,旧版本的字符串仍在堆内存中(可能已释放,但仍未重用/覆盖),因此我无法分辨哪个字符串是当前字符串.

I'm experimenting a little bit with analyzing the heap memory of a process in order to determine the current value of a string in the other process. Accessing the process heap memory and strolling through it is no problem. However, because the value of the string changes and the process allocates a new part of the memory each time, the string's address changes. Because the string has a fixed format it's still easy to find, but after a few changes the old versions of the string are still in the heap memory (probably freed, but still not reused / overwritten) and thus I'm not able to tell which string is the current one.

因此,为了仍然找到当前的字符串,我想通过将其地址与地址 malloc()free 进行比较来检查我在内存中找到的字符串是否仍在使用() 了解一下.

So, in order to still find the current one I want to check if a string I find in the memory is still used by comparing its address against the addresses malloc() and free() know about.

咲,艾尔玛

推荐答案

malloc/free 可以通过多种方式存储内存区域的大小.例如,它可能存储在 malloc 返回的区域之前.或者它可能存储在其他地方的查找表中.或者它可能被隐式存储:某些区域可能会保留用于特定大小的分配.

There are lots of ways in which malloc/free can store the size of the memory area. For example, it might be stored just before the area returned by malloc. Or it might be stored in a lookup table elsewhere. Or it might be stored implicitly: some areas might be reserved for specific sizes of allocations.

要了解 Linux (glibc) 中的 C 库如何执行此操作,请从 http 获取源代码://ftp.gnu.org/gnu/glibc/ 并查看 malloc/malloc.c 文件.顶部有一些文档,它指的是 A Memory Allocator道格·利.

To find out how the C library in Linux (glibc) does this, get the source code from http://ftp.gnu.org/gnu/glibc/ and look at the malloc/malloc.c file. There is some documentation at the top, and it refers to A Memory Allocator by Doug Lea.

这篇关于malloc() 和 free() 在哪里存储分配的大小和地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆