简体   繁体   English

C如何修改其他结构中的结构的内存

[英]C how to modify memory of structs that are inside other structs

If I have two structs: 如果我有两个结构:

typedef struct{
    unsigned int time;
    double rate;

}quote;

typedef struct{

    unsigned int freeSlots;
    unsigned int end;
    unsigned int start;
    unsigned int currSize;
    unsigned int maxSize;
    unsigned int startAt;
    //unsigned int currIndex;
    quote quoteBuffer[1];

}cbuf;

And I wanted to make a function that would modify the size of the quoteBuffer array inside cbuf, how exactly would I go about doing that? 我想创建一个函数来修改cbuf中的quoteBuffer数组的大小,我究竟会怎么做呢? I have tried a few approaches but none have worked so far. 我尝试了一些方法,但到目前为止还没有。 I keep returning to the same format of: 我一直回到相同的格式:

quote *newQuoteBuffer = malloc(sizeof(quote) * newSize);

And if I already have an existing cbuf somewhere (for example, we will call it "a" where a is the pointer to the cbuf): 如果我已经有一个现有的cbuf(例如,我们将其称为“a”,其中a是指向cbuf的指针):

a->quoteBuffer = newQuoteBuffer;

But obviously this doesn't work. 但显然这不起作用。 Any hints? 任何提示?

This: 这个:

quote quoteBuffer[1];

should be: 应该:

quote *quoteBuffer;

Then the assignment will work. 然后分配将起作用。

Dereferencing quote looks like this: 取消引用quote如下所示:

a->quoteBuffer->time;

If you later have multiple elements of quote allocated with malloc() you can access them like this: 如果您以后有多个使用malloc()分配的引用元素,您可以像这样访问它们:

a->quoteBuffer[i].time;

If you are not sure of how many elements will go into the quoteBuffer, maintain a linked list of the same. 如果您不确定quoteBuffer中将包含多少元素,请维护相同的链接列表。 For that 为了那个原因

quote *quoteBuffer;

And keep adding or removing the elements to/from the buffer as required. 并根据需要继续向缓冲区添加元素或从缓冲区中删除元素。

I think you're missing the point of why someone would have the last element of a struct as a single element array. 我认为你错过了为什么某人将结构的最后一个元素作为单个元素数组的观点。 This is a trick that's used in old C code as a way to make the struct size variable length. 这是一种在旧C代码中用作使结构大小可变长度的方法。

You can write code such as this: 你可以编写如下代码:

Bitmapset *p = malloc(offsetof(Bitmapset, quoteBuffer) + n * sizeof(quote));

Then you write code like this: 然后你编写这样的代码:

p->quoteBuffer[0]

up to: 取决于:

p->quoteBuffer[n-1]

You do not want to assign a pointer directly to quoteBuffer, as you guessed. 正如您猜测的那样,您不希望将指针直接指定给quoteBuffer。

So, why would you want to declare quoteBuffer as: quote quoteBuffer[1]; 那么,为什么要将quoteBuffer声明为:quote quoteBuffer [1]; instead of quote* quoteBuffer; 而不是引用* quoteBuffer; ?

It's because you do not wanna to have a separate allocation for quoteBuffer. 这是因为你不想为quoteBuffer单独分配。 A single allocation can be used for the entire cbuf, including the inline quote array. 单个分配可用于整个cbuf,包括内联引用数组。

There are two approaches. 有两种方法。 One is to use a pointer in cbuf, as others have mentioned, by changing 一种是在cbuf中使用指针,正如其他人提到的那样,通过改变

quote quoteBuffer[1];

to

quote* quoteBuffer;

The other is to resize the cbuf: 另一种是调整cbuf的大小:

#include <stddef.h> // for offsetof

struct cbuf* realloc_cbuf(struct cbuf* cbufp, size_t nquotes)
{
    struct cbuf* new_cbufp = realloc(cbufp, offsetof(struct cbuf, quoteBuffer) + nquotes * sizeof *cbufp->quoteBuffer);
    if (!new_cbufp)
    {
        // handle out of memory here. cbufp is still intact so free it if you don't need it.
    }
    return new_cbufp;
}

void elsewhere(void)
{
    struct cbuf* acbuf = NULL;
    acbuf = realloc_cbuf(1);
    acbuf = realloc_cbuf(10);
    // etc. 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM