English 中文(简体)
内装着一股斯特鲁茨的阵列, 内装有阵列( 和阵列) 内装着阵列。
原标题:A Struct with an Array of Structs with Arrays inside Them (and an Array) inside It: How would I malloc this?

我目前没有代码,因为我根本不知道该如何做到这一点。我能否自己计算一下每个低层支架需要多少字节,然后用它来进行折叠?这是非常糟糕的编码,不是吗?这就是我试图拼凑起来的两个字节:

struct property {
    int d;
    char name [111]; // I just malloc this like I would a normal array, right?
    char descr [1025]; // Ditto.
}

struct category {
    int d [413]; // The d s of all the child structs sorted highest to lowest.
    char name [111];
    struct property property [413]; // This. How do I allocate this?
}</code>

我是否必须做 < code> struct property* property * property = (struct property *) cloc (( struct property) * 413); ? 数组内的圆球是否完好无损? 阵列中的圆球一般如何表现?

最佳回答

You don t have a pointer member inside your structure property so you don t need to malloc any of your structure members.
When you malloc for the structure it will give you enough memory to hold all the structure members including arrays, exceptions are pointer structure members(You don t have any).

问题回答

您的malloc 没有投影会很好。 它会分配整个阵列的毗连内存。 阵列内的所有阵列都与它一同分配, 它们是合适的阵列, 而不是指针 。

大小会给您提供您整个结构的大小。 它会正确计算数组和结构的大小 。

然而,413件物品似乎具有任意性。 一个可变规模的结构对你们有用吗?

在此情况下, 提前计算时空大小以避免中圆点是一个良好的性能设想 。 Malloc 速度缓慢, 需要锁, 而堆块会随着时间的流逝而碎裂 。 此示例显示您如何在结构末端用指针而不是数组或变长数组来制造“ 变长” 结构 :

struct category
{
  int              cItems;  // need this if handling variable # of items now.
  int             *d;  // ptr instead of array
  char            *name;  // ptr again
  struct property  property[0];  // var length array
}


int cItems = 413; // or whatever
// this is a nifty trick to get the size of a variable length struct:
int cbCategory = (size_t)(&((struct category*)0)->property[cItems]);
int cbD = sizeof(int)*cItems;
int cbName = sizeof(char)*cItems;
struct category *pCategory  = (struct category*)malloc(cbCategory + cbD + cbName);
// wire up d:
pCategory->d = (int*)((char*)pCategory + cbCategory);
// or wire d up this way:
pCategory->d = (int*)&pCategory->property[cItems];
// wire up name
pCategory->name = (char*)pCategory->d + cbD;
// or wire up name this way
pCategory->name = (char*)&pCategory->d[cItems];
// set items
pCategory->cItems = cItems;

注意,我假设d有413个元素。我本可以很容易地把它留成一个阵列。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签