多少内存的malloc保持跟踪已分配 [英] keeping track of how much memory malloc has allocated

查看:142
本文介绍了多少内存的malloc保持跟踪已分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对SO相关问题快速扫描后,我推断,有没有功能,将检查的malloc已经分配给一个指针的内存量。我试图复制一些使用C中简单的char *的标准::字符串的基本功能(主要是动态的大小),并且不希望调用的realloc所有的时间。我想我会需要跟踪的多少内存已被分配。为了做到这一点,我正在考虑建立一个typedef将包含字符串本身和一个整数与当前分配的内存量,是这样的:

After a quick scan of related questions on SO, I have deduced that there's no function that would check the amount of memory that malloc has allocated to a pointer. I'm trying to replicate some of std::string basic functionality (mainly dynamic size) using simple char*'s in C and don't want to call realloc all the time. I guess I'll need to keep track of how much memory has been allocated. In order to do that, I'm considering creating a typedef that will contain the string itself and an integer with the amount of memory currently allocated, something like this:

typedef struct {
    char * str;
    int mem;
} my_string_t;

是一个最佳的解决方案,或者你可以建议一些能够承受更好的结果?在此先感谢您的帮助。

Is that an optimal solution, or perhaps you can suggest something that will bear better results? Thanks in advance for your help.

推荐答案

您将要分配空间的长度和在相同的内存块中的字符串。这可能是你打算用什么样的结构,但已保留空间只有一个指向字符串。

You will want to allocate the space for both the length and the string in the same block of memory. This may be what you intended with your struct, but you have reserved space for only a pointer to the string.

必须有分配给包含该字符串的字符的空间。

例如:


typedef struct
{
    int num_chars;
    char string[];
} my_string_t;

my_string_t * alloc_my_string(char *src)
{
    my_string_t * p = NULL;
    int N_chars = strlen(src) + 1;

    p = malloc( N_chars + sizeof(my_string_t));
    if (p)
    {
         p->num_chars = N_chars;
         strcpy(p->string, src);
    }
    return p;
}

在我的例子,来访问指向您的字符串,你解决字符串的成员 my_string_t

In my example, to access the pointer to your string, you address the string member of the my_string_t:


my_string_t * p = alloc_my_string("hello free store.");
printf("String of %d bytes is '%s'\n", p->num_chars, p->string);

小心意识到你正在获得指针字符串作为分配的空间来存储字符的后果。你是分配资源的人物存储,所获得的指针来分配存储的参考。

Be careful to realize that you are obtaining the pointer for the string as a consequence of allocating space to store the characters. The resource you are allocating is the storage for the characters, the pointer obtained is a reference to the allocated storage.

在我的例子中,分配的内存布局顺序如下:

In my example, the memory allocated is laid out sequentially as follows:


+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
| 00 | 00 | 00 | 11 | 'h'| 'e'| 'l'| 'l'| 'o'| 20 | 'f'| 'r'| 'e'| 'e'| 20 | 's'| 't'| 'o'| 'r'| 'e'| '.'| 00 |
+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
^^                   ^
||                   |
p|                   |
 p->num_chars        p->string

注意对 - >串的值没有被存储在所分配的存储器,它是从所分配的存储器的开始的四个字节,立即到随后的( presumed 32位,四字节)整数

Notice that the value of p->string is not stored in the allocated memory, it is four bytes from the beginning of the allocated memory, immediately subsequent to the (presumed 32-bit, four-byte) integer.

您的编译器可能需要您声明灵活的数组c为:

Your compiler may require that you declare the flexible C array as:


typedef struct
{
    int num_chars;
    char string[0];
} my_string_t;

但缺乏零点版本是C99兼容据说。

but the version lacking the zero is supposedly C99-compliant.

您可以完成,没有数组成员等价的东西如下:

You can accomplish the equivalent thing with no array member as follows:


typedef struct
{
    int num_chars;
} mystr2;

char * str_of_mystr2(mystr2 * ms)
{
    return (char *)(ms + 1);
}

mystr2 * alloc_mystr2(char *src)
{
    mystr2* p = NULL;
    size_t N_chars = strlen(src) + 1;

    if (N_chars num_chars = (int)N_chars;
         strcpy(str_of_mystr2(p), src);
    }
    return p;
} 

printf("String of %d bytes is '%s'\n", p->num_chars, str_of_mystr2 (p));

在第二个例子中,等同于 P->字符串值 str_of_mystr2计算()。这将有大致相同的值作为第一实例,这取决于如何结构的端部是由编译器设置包装

In this second example, the value equivalent to p->string is calculated by str_of_mystr2(). It will have approximately the same value as the first example, depending on how the end of structs are packed by your compiler settings.

虽然有些人会建议跟踪长度在为size_t 我想看看了,为什么我不同意一些旧道博博士的文章。支持值比INT_MAX更大的是值得怀疑的价值,你的程序的正确性。通过使用一个int,你可以写断言(P-> NUM_CHARS> = 0); ,并有测试的东西。一个无符号,你会写的等效试验类似断言(对GT; NUM_CHARS< UINT_MAX / 2); 只要你写code,其中包含在运行时的数据检查,使用符号的类型可以是有用的。

While some would suggest tracking the length in a size_t I would look up some old Dr. Dobb's article on why I disagree. Supporting values greater than INT_MAX is of doubtful value to your program's correctness. By using an int, you can write assert(p->num_chars >= 0); and have that test something. With an unsigned, you would write the equivalent test something like assert(p->num_chars < UINT_MAX / 2); As long as you write code which contains checks on run-time data, using a signed type can be useful.

在另一方面,如果你正在写它处理字符串超过UINT_MAX / 2个字符库,我向你致敬。

On the other hand, if you are writing a library which handles strings in excess of UINT_MAX / 2 characters, I salute you.

这篇关于多少内存的malloc保持跟踪已分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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