2012-10-14 51 views
4

我有一个结构如何应用结构偏移量?

typedef struct foo { 
    int lengthOfArray1; 
    int lengthOfArray2; 
    int* array1; 
    int* array2; 
} foo; 

我需要为整个结构和数组的内容分配足够的内存。因此,假设每个阵列有长度为5 ...

foo* bar = (foo*)malloc(sizeof(foo) + (sizeof(int) * 5) + (sizeof(int) * 5)); 

我现在有指向数组1和数组2到正确的位置在分配的缓冲区:

bar->array1 = (int*)(&bar->lengthOfArray2 + sizeof(int)); 
bar->array2 = (int*)(bar->array1 + lengthOfArray2); 

这是正确的吗?

编辑#1

只是为了澄清任何混淆:我试图保持内存在一个块,而不是三个。

编辑#2

我不能使用C99作为MSVC 2010编译器不支持它(http://stackoverflow.com/questions/6688895/does-microsoft-visual-studio-2010-supports -c99)。

+0

在一个步骤? NO,是不正确的,只有最后一个成员可以用这种方式分配使用灵活数组(C99),看看http://en.wikipedia.org/wiki/Data_padding –

+0

这可以很好地在一个步。请参阅下面的某些答案。 @DavidRF – alk

+0

我不能使用C99 –

回答

3

继OP的方法,这应该做的工作:

/* Defining these types allows to change the types without having the need to modify the code. */ 
typedef int Foo_ArrayElement1_t; 
typedef int Foo_ArrayElement2_t; 

typedef struct Foo_s { 
    size_t lengthOfArray1; /* 'size_t' is the type of choice for array/memory dimensions. */ 
    size_t lengthOfArray2; 
    Foo_ArrayElement1_t * array1; 
    Foo_ArrayElement2_t * array2; 
} Foo_t; 

/* 
* Allocates memory to hold a structure of type Foo_t including size for 
* 's1' elements referenced by 'array1' and 's2' elements by 'array2'. 
* The arrays' elements are set to 0. 
* 
* Returns a pointer to the freshly allocated memory or NULL if the memory could not 
* be allocated. 
*/ 
Foo_t * Foo_CreateAndInit(size_t s1, size_t s2) 
{ 
    /* At once allocate all 'Foo_t' (including the memory Foo_t's array pointers shall point to). */ 
    Foo_t * pfoo = calloc(1, 
     sizeof(*pfoo) + 
     s1 * sizeof(*(pfoo->array1) + 
     s2 * sizeof(*(pfoo->array2))); 
    if (pfoo) 
    { 
    pfoo->lengthOfArray1 = s1; 
    pfoo->lengthOfArray2 = s2; 

    /* The first array starts right after foo. */ 
    pfoo->array1 = (Foo_ArrayElement1_t *) (pfoo + 1); 

    /* The second array starts right after s1 elements of where the first array starts. */ 
    pfoo->array2 = (Foo_ArrayElement2_t *) (pfoo->array1 + s1); /* That casting here is not 
     necessaryas long as 'Foo_t.array1' and 'Foo_t.array2' point to the same type but makes 
     the code work even if those types were changed to be different. */ 
    } 

    return pfoo; 
} 

... 

Foo_t * foo = Foo_CreateAndInit(5, 5); 
+0

我抬起头size_t,它似乎被使用在我不能使用的C99中。 –

+0

从我的知识'size_t'在'C99'之前存在looong。无论如何,数组大小至少不需要“签名”。所以只要在你的平台上使用'malloc()'/'calloc()'的参数所使用的类型。 @StormKiernan – alk

+0

你打算:sizeof(* pfoo)?或sizeof(Foo_t)? –

4

您必须分配结构的大小。然后您必须分配具有各自大小的整数数组。

foo* bar = malloc(sizeof(foo)); 

/* somewhere in here the array lengths are set then... */ 

bar->array1 = malloc(sizeof(int) * bar->lengthOfArray1); 
bar->array2 = malloc(sizeof(int) * bar->lengthOfArray2); 
+2

这个答案的关键是有两个步骤,首先分配结构,然后分配结构指向的内存。您不能同时分配结构和数组内存。 –

+0

个人而言,我觉得最好写一下:bar-> arrayX = malloc(sizeof(* bar-> arrayX)* bar-> lengthOfArrayX);如果您应该决定将类型从int更改为其他内容。生成相同的代码。 – Pat

+1

遵循你的方法,你创建了三个独立的内存区域,这些内存区域可能会使逻辑结构'foo'的处理(复制,清除)包括其动态数组成员更加复杂。 – alk

1

随着一点点额外的内存(仅适用于C99):

#include <stdio.h> 
#include <stdlib.h> 

typedef struct foo { 
    int lengthOfArray1; 
    int lengthOfArray2; 
    int *array1; 
    int *array2; 
    int array[]; 
} foo; 

int main(void) 
{ 
    foo *bar = malloc(sizeof(foo) + (sizeof(int) * 10)); 

    bar->array1 = &bar->array[0]; 
    bar->array2 = &bar->array[5]; /* 5 or lengthOfArray1 */ 

    return 0; 
} 
+0

你可以通过使用'int array [1]'并且在'malloc'(旧的方法来做一个灵活的数组成员)中减1来完成这项工作。 – nneonneo

+0

@nneonneo,是的,但作为c-faq说,它不清楚它是合法的或便携式的http://c-faq.com/struct/structhack.html –

+0

我不能使用C99 –