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

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

问题描述

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

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

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

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

ciao, 埃尔玛

解决方案

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

要了解Linux中的C库(glibc)如何做到这一点,请从 http ://ftp.gnu.org/gnu/glibc/并查看malloc/malloc.c文件.顶部有一些文档,它引用了内存分配器道格·李(Doug Lea)

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.

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.

ciao, Elmar

解决方案

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.

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天全站免登陆