[英]Dynamically allocate C struct?
我想动态分配一个C结构:
typedef struct {
short *offset;
char *values;
} swc;
“偏移”和“值”都应该是数组,但是直到运行时它们的大小都是未知的。
如何为结构和结构的数组动态分配内存?
swc *a = (swc*)malloc(sizeof(swc));
a->offset = (short*)malloc(sizeof(short)*n);
a->values = (char*)malloc(sizeof(char)*n);
其中,n =每个数组中的项目数,a是新分配的数据结构的地址。 不要忘记在free()之前使用free()偏移量和值。
在C中:
swc *s = malloc(sizeof *s); // assuming you're creating a single instance of swc
if (s)
{
s->offset = malloc(sizeof *(s->offset) * number_of_offset_elements);
s->values = malloc(sizeof *(s->values) * number_of_value_elements);
}
在C ++中:
try
{
swc *s = new swc;
s->offset = new short[number_of_offset_elements];
s->values = new char[number_of_value_elements];
}
catch(...)
{
...
}
请注意,在C ++中,相对于动态分配的缓冲区,使用向量可能会更好:
struct swc
{
std::vector<short> offset;
std::vector<char> values;
};
swc *a = new swc;
问题:值应该是单个字符数组还是字符串数组? 那会改变一点。
编辑
我思考的越多,对C ++答案的满意度就越低; 在C ++中执行此类操作的正确方法(假设您需要动态分配的缓冲区而不是向量,而您可能不需要向量)是在struct类型中作为构造函数的一部分对offset和values进行内存分配,并且在销毁struct实例时(通过delete
或超出范围)让析构函数释放这些元素。
struct swc
{
swc(size_t numOffset = SOME_DEFAULT_VALUE,
size_t numValues = SOME_OTHER_DEFAULT_VALUE)
{
m_offset = new short[numOffset];
m_values = new char[numValues];
}
~swc()
{
delete[] m_offset;
delete[] m_values;
}
short *m_offset;
char *m_values;
};
void foo(void)
{
swc *a = new swc(10,20); // m_offset and m_values allocated as
// part of the constructor
swc b; // uses default sizes for m_offset and m_values
...
a->m_offset[0] = 1;
a->m_values[0] = 'a';
b.m_offset[0] = 2;
b.m_values[0] = 'b';
...
delete a; // handles freeing m_offset and m_values
// b's members are deallocated when it goes out of scope
}
您必须单独进行操作。 首先分配结构,然后分配数组的内存。
在C中:
swc *pSwc = malloc(sizeof(swc));
pSwc->offset = malloc(sizeof(short)*offsetArrayLength);
pSwc->values = malloc(valuesArrayLength);
在C ++中,您不应该那样做。
在C中:
typedef struct
{
short *offset;
char *values;
} swc;
/// Pre-Condition: None
/// Post-Condition: On failure will return NULL.
/// On Success a valid pointer is returned where
/// offset[0-n) and values[0-n) are legally de-refrancable.
/// Ownership of this memory is returned to the caller who
/// is responsible for destroying it via destroy_swc()
swc *create_swc(unsigned int size)
{
swc *data = (swc*) malloc(sizeof(swc));
if (data)
{
data->offset = (short*)malloc(sizeof(short)*n);
data->values = (char*) malloc(sizeof(char) *n);
}
if ((data != NULL) && (size != 0) && ((data->offset == NULL) || (data->values == NULL)))
{
// Partially created object is dangerous and of no use.
destroy_swc(data);
data = NULL;
}
return data;
}
void destroy_swc(swc* data)
{
free(data->offset);
free(data->values);
free(data);
}
在C ++中
struct swc
{
std::vector<short> offset;
std::vector<char> values;
swc(unsigned int size)
:offset(size)
,values(size)
{}
};
您将需要一个函数来执行此操作。 有点像(我的C / C ++生锈)
swc* makeStruct(int offsetCount, int valuesCount) {
swc *ans = new swc();
ans->offset = new short[offsetCount];
ans->values = new char[valuesCount];
return ans;
}
myNewStruct = makeStruct(4, 20);
语法可能会有些偏离,但这通常是您需要的。 如果您使用的是C ++,则可能需要一个带有2个args而不是makeStruct的构造函数的类,但是这样做非常相似。
在这里为许多正确答案添加一件事:您可以 malloc
分配一个超大的结构以在最后一个成员中容纳一个可变大小的数组。
struct foo {
short* offset;
char values[0]
};
然后
struct *foo foo1 = malloc(sizeof(struct foo)+30); // takes advantage of sizeof(char)==1
为values
数组中的30个对象腾出空间。 您仍然需要做
foo1->offsets = malloc(30*sizeof(short));
如果您希望他们使用相同大小的数组。
我通常不会实际执行此操作(如果需要扩展结构,则需要进行维护噩梦),但这是工具包中的工具。
[代码在c。 您需要在c ++中强制转换malloc
的(或更好地使用new
和RAII习惯用法)
swc* a = malloc(sizeof(*a));
a->offset = calloc(n, sizeof(*(a->offset)));
a->values = calloc(n, sizeof(*(a->values)));
您不应该在c中强制使用void *。
使用malloc函数或calloc动态分配内存。 并在Google上搜索以获取示例。
The calloc function initializes allocated memory to zero.
大多数答案是正确的。 我想添加一些您没有明确要求但可能也很重要的内容。
C / C ++数组不会在内存中存储自己的大小 。 因此,除非您希望offset
和values
具有编译时定义的值(在这种情况下,最好使用固定大小的数组),否则您可能希望将两个数组的大小都存储在struct
。
typedef struct tagswc {
short *offset;
char *values;
// EDIT: Changed int to size_t, thanks Chris Lutz!
size_t offset_count;
size_t values_count; // You don't need this one if values is a C string.
} swc;
免责声明 :我可能是错的。 例如,如果所有swc
实例的所有offset
都具有相同的大小,则最好将offset_count
存储为全局成员,而不是struct
的成员。 关于values
和values_count
也可以这样说。 另外,如果values
是C字符串,则无需存储其大小,但要提防画家之类的Schlemiel问题。
由于还没有人提及它,所以有时最好在一次分配中抢占这块内存,因此您只需要对一件事调用free():
swc* AllocSWC(int items)
{
int size = sizeof(swc); // for the struct itself
size += (items * sizeof(short)); // for the array of shorts
size += (items * sizeof(char)); // for the array of chars
swc* p = (swc*)malloc(size);
memset(p, 0, size);
p->offset = (short*)((char*)swc + sizeof(swc)); // array of shorts begins immediately after the struct
p->values = (char*)((char*)swc + sizeof(swc) + items * sizeof(short)); // array of chars begins immediately after the array of shorts
return p;
}
当然,这很难读取和维护(特别是如果在首次分配数组后动态调整数组的大小)。 我见过在许多地方使用过的另一种方法。
除了上述内容外,我还想添加释放的内存,如下所示。
typedef struct {
short *offset;
char *values;
} swc;
swc* createStructure(int Count1, int Count2) {
swc *s1 = new swc();
s1->offset = new short[Count1];
s1->values = new char[Count2];
return s1;
}
int _tmain(int argc, _TCHAR* argv[])
{
swc *mystruct;
mystruct = createStructure(11, 11);
delete[] mystruct->offset;
delete[] mystruct->values;
delete mystruct;
return 0;
}
**如果**您将不调整数组的大小,则只需调用malloc()
。
swc *new_swc (int m, int n) {
swc *p;
p = malloc (sizeof (*p) + m * sizeof (p->offset[0]) + n * sizeof (p->values[0]);
p->offset = (short *) &p[1];
p->values = (char *) &p->offset[m];
return p;
}
然后,您可以通过一次调用free()
来free()
它。
(通常,有一些对齐注意事项要考虑,但是对于由短裤组成的数组,然后是字符组成的数组,则可以。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.