对于结构指针数组的内存分配 [英] Memory allocation for array of structure pointers

查看:141
本文介绍了对于结构指针数组的内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧...我将试图解释这是尽我所能。
我试图说多少内存他要用和内存最小的块大小可以提供用户开始建立C的内存分配器。

Okay... I will try to explain this as best I can. I'm trying to build a memory allocator in C. The user starts by saying how much memory he wants to use, and the smallest block size of memory that can be made available.

所以,举例来说,假设用户请求与1024B 8B的最小的块大小。
这意味着可能块大小将是1024,512,256,128,64,32,16,和8个

So, for example, let's say the user requests 1024B with the smallest block size of 8B. That means the possible block sizes would be 1024, 512, 256, 128, 64, 32, 16, and 8.

要跟踪的内存空闲块的,我有一个指针数组结构。这些结构被称为报头,并且阵列被称为的FreeList。
我的意思是,的FreeList [0]将包含一个指针在存储器中的空间,其中有8的FreeList [1]将包含一个指针在存储器中的空间,其中有存储器大小16的块存储器尺寸的块。等等。

To keep track of the free blocks of memory, I have an array of pointers to structures. These structures are called Header, and the array is called FreeList. What I mean is that FreeList[0] would contain a pointer to the space in memory where there is a block of memory size 8. FreeList[1] would contain a pointer to the space in memory where there is a block of memory size 16. etc.

typedef void * Addr;
struct Header
{
    Addr next;
    int order;
};

struct Header *FreeList[];

我想分配内存此空闲列表具有以下code使用方法:

I'm trying to allocate memory for this free list to use with the following code:

FreeList = malloc(Order*sizeof(struct Header));

在哪里顺序不同块大小的数量,你可以有。

Where Order is the number of different block sizes you can have.

我得到的编译错误'的FreeList'有一个不完整的类型。

I'm getting the compile error 'FreeList' has an incomplete type.

我不希望这些指针指向任何地方还没有,我只是想分配空间的数据。

I don't want these pointers to point anywhere yet, I just want to allocate the space for the data.

任何援助将大大AP preciated。 =)

Any assistance would be much appreciated. =)

推荐答案

在C语言

struct Header *FreeList[];

是未知大小(不完全型)的静态数组暂定定义。这个数组应稍后以已知的编译时的大小来定义。问题的关键是,它是一个的静态的数组。这是不是分配的由的malloc

is a tentative definition for a static array of unknown size (incomplete type). This array should be defined later with known compile-time size. The point is that it is a static array. It is not "allocatable" by malloc.

如果您需要,可以在运行时被分配的指针数组的malloc ,你要声明一个指针到指针变量

If you need an array of pointers that can be allocated at run-time by malloc, you have to declare a pointer-to-pointer variable

struct Header **FreeList;

这是后者与适当大小分配

which is latter allocated with proper size

FreeList = malloc(Order * sizeof *FreeList);

请注意,在这种情况下,你分配的​​指针的数组,就像你想要的。和的sizeof 在上面的配置等同于的sizeof(结构*头)。即一个指针的大小(相对于不正确的的sizeof(结构标题)在你原来的code)。

Note that in this case you are allocating an array of pointers, just like you wanted. And the sizeof in the above allocation is equivalent to sizeof(struct Header *). i.e. size of a pointer (as opposed to the incorrect sizeof(struct Header) in your original code).

这再次分配未初始化的指针数组。这是你的责任,以初始化这些指针,即让他们一点,无论你想它们指向。如果需要的话,你就必须为实际的头分配内存。

This, again, allocates an array of uninitialized pointers. It is your responsibility to initialize these pointers, i.e. to make them point wherever you want them to point to. If necessary, you'll have to allocate memory for the actual headers as well.

然而,这是不是从你贴什么真正清楚是否真的需要的指针的数组为标题或,也许,的实际头的数组的。你的解释是混乱的,有时,自相矛盾。如果你需要实际的头的数组,然后将指针声明和分配将如下所示

However, it is not really clear from what you posted whether you really need an array of pointers to headers or, maybe, an array of actual headers. Your explanation is confusing and, at times, self-contradictory. If you need an array of actual headers, then the pointer declaration and allocation will look as follows

struct Header *FreeList;
...
FreeList = malloc(Order * sizeof *FreeList);

在这种情况下,的sizeof 上方的前pression等同于的sizeof(结构标题),如我们原来的例子。但要记住所分配的头阵仍没有初始化。

In this case sizeof expression above is equivalent to sizeof(struct Header), as in our original example. Remember though that the allocated header array is still not initialized.

这篇关于对于结构指针数组的内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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