**struct 的重新分配问题

发布于 2024-07-26 04:56:07 字数 1577 浏览 3 评论 0原文

我在 C 中的 realloc 函数上遇到问题。我向您传递了一段代码:

typedef struct _Pool Pool;
typedef struct _Item Item;

struct _Pool {
    Item ** items;
    unsigned int itemsCount;
    unsigned int poolSize;
    unsigned int poolStep;
};

struct _Item {
    char * file; 
    unsigned int lenOfFilePath;
    unsigned int x;
};

void Pool_insert(Pool ** pool, Item * item)
{       
    if ((*pool)->itemsCount + 1 == (*pool)->poolSize)
    {
        (*pool)->items = realloc((*pool)->items, sizeof(Item *)*((*pool)->poolSize + (*pool)->poolStep));;
        if (!(*pool)->items) return;
        (*pool)->poolSize += (*pool)->poolStep;
    }

    (*pool)->items[(*pool)->itemsCount++] = item;
}

我没有发送第一个分配代码,但如果我增加在那里使用的项目数量,一切都会正常。 当我调用 realloc 时,我收到这样的错误:

malloc:*** 对象 0x19dba8 错误: 没有重新分配指针 已分配

您能帮我解决我的问题吗?


这是池的创建方法:

void Pool_create(Pool ** pool, unsigned int poolSize)
{   
    unsigned int poolMemSize = sizeof(Pool) + sizeof(Item *)*poolSize;
    *pool = malloc(poolMemSize);
    if (!*pool) return;
    memset(*pool,0,poolMemSize);

    (*pool)->itemsCount = 0;
    (*pool)->poolSize = poolSize;
    (*pool)->poolStep = POOL_STEP;

    (*pool)->items = (Item **)((char*)(*pool) + sizeof(Pool));
    if(!(*pool)->items) return;
    memset((*pool)->items, 0, sizeof(Item *) * poolSize);
}

就像我所说的,如果我想要插入 1000 个项目并通过创建函数分配内存,那么如果我声明 100 个元素的起始池大小,然后我想重新分配项目,那么一切都会正常工作,我得到错误。

非常感谢您如此快速的答复。

I have a problem with realloc function in C. I'm passing you a code bellow:

typedef struct _Pool Pool;
typedef struct _Item Item;

struct _Pool {
    Item ** items;
    unsigned int itemsCount;
    unsigned int poolSize;
    unsigned int poolStep;
};

struct _Item {
    char * file; 
    unsigned int lenOfFilePath;
    unsigned int x;
};

void Pool_insert(Pool ** pool, Item * item)
{       
    if ((*pool)->itemsCount + 1 == (*pool)->poolSize)
    {
        (*pool)->items = realloc((*pool)->items, sizeof(Item *)*((*pool)->poolSize + (*pool)->poolStep));;
        if (!(*pool)->items) return;
        (*pool)->poolSize += (*pool)->poolStep;
    }

    (*pool)->items[(*pool)->itemsCount++] = item;
}

I'm not sending the first allocation code but if I increase the number of items that I'm using there everything works fine. When I call realloc I'm getting such error:

malloc: *** error for object 0x19dba8:
pointer being reallocated was not
allocated

Could you please help me to solve my problem?


Here is the creation method for Pool:

void Pool_create(Pool ** pool, unsigned int poolSize)
{   
    unsigned int poolMemSize = sizeof(Pool) + sizeof(Item *)*poolSize;
    *pool = malloc(poolMemSize);
    if (!*pool) return;
    memset(*pool,0,poolMemSize);

    (*pool)->itemsCount = 0;
    (*pool)->poolSize = poolSize;
    (*pool)->poolStep = POOL_STEP;

    (*pool)->items = (Item **)((char*)(*pool) + sizeof(Pool));
    if(!(*pool)->items) return;
    memset((*pool)->items, 0, sizeof(Item *) * poolSize);
}

Like I told, if I want for example insert 1000 Items and allocate the memory by create function then everything works fine if I declare start Pool size for 100 elements and then I want to realloc Items I get the error.

Thank you very much for such quick answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜点 2024-08-02 04:56:07

但是 (*pool)->items 从未分配! 您只分配了(*pool) - 整个内存片。 在这种情况下,您应该使用两个 malloc - 一个用于控制结构(池),另一个用于 items* 数组。

澄清:您不能重新分配已分配内存的部分 - 只能重新分配整个 malloc 块。
另请注意,在用编写良好的代码替换原始指针之前,您应该首先测试 realloc 的结果 - 否则,如果 realloc 失败,您将丢失先前分配的内存块。

But (*pool)->items was never allocated! You only allocated (*pool) - the whole slice of memory. You should have used two mallocs in this case - one for the control structure (Pool) and one for items* array.

Clarification: you cannot realloc part of allocated memory - only the whole malloc'ed chunk.
Also note that you should test the result of realloc first, before replacing the original pointer in well-written code - otherwise if realloc fails you lose the previously allocated memory block.

青巷忧颜 2024-08-02 04:56:07
struct _Pool {
    Item ** items;
    unsigned int itemsCount;
    unsigned int poolSize;
    unsigned int poolStep;
};

malloc:*** 对象 0x19dba8 错误:
没有重新分配指针
已分配

您是否使用 malloc 为项目分配内存? 该消息似乎告诉您,您不是。 Realloc 应该用于使用类似 malloc 的函数分配的内存。

(*pool)->items = realloc((*pool)->items, sizeof(Item *)*((*pool)->poolSize + (*pool)->poolStep));;

如需进一步帮助,请向我们展示如何初始化项目。

struct _Pool {
    Item ** items;
    unsigned int itemsCount;
    unsigned int poolSize;
    unsigned int poolStep;
};

malloc: *** error for object 0x19dba8:
pointer being reallocated was not
allocated

Are you allocating memory for items with malloc ? That message seems to be telling that you are not. Realloc should be used for memory allocated with malloc - like functions.

(*pool)->items = realloc((*pool)->items, sizeof(Item *)*((*pool)->poolSize + (*pool)->poolStep));;

For further help, show us how you initialize items.

你是年少的欢喜 2024-08-02 04:56:07

您可能忘记初始化结构中的 *items 并且正在追逐野指针。

You probably forgot to initialize *items in the structure and are chasing a wild pointer.

原野 2024-08-02 04:56:07

这是池的创建方法:

void Pool_create(Pool ** pool, unsigned int poolSize)
{   
    unsigned int poolMemSize = sizeof(Pool) + sizeof(Item *)*poolSize;
    *pool = malloc(poolMemSize);
    if (!*pool) return;
    memset(*pool,0,poolMemSize);

    (*pool)->itemsCount = 0;
    (*pool)->poolSize = poolSize;
    (*pool)->poolStep = POOL_STEP;

    (*pool)->items = (Item **)((char*)(*pool) + sizeof(Pool));
    if(!(*pool)->items) return;
    memset((*pool)->items, 0, sizeof(Item *) * poolSize);
}

就像我所说的,如果我想要插入 1000 个项目并通过创建函数分配内存,那么如果我声明 100 个元素的起始池大小,然后我想重新分配项目,那么一切都会正常工作,我得到错误。

非常感谢您如此快速的答复。

Here is the creation method for Pool:

void Pool_create(Pool ** pool, unsigned int poolSize)
{   
    unsigned int poolMemSize = sizeof(Pool) + sizeof(Item *)*poolSize;
    *pool = malloc(poolMemSize);
    if (!*pool) return;
    memset(*pool,0,poolMemSize);

    (*pool)->itemsCount = 0;
    (*pool)->poolSize = poolSize;
    (*pool)->poolStep = POOL_STEP;

    (*pool)->items = (Item **)((char*)(*pool) + sizeof(Pool));
    if(!(*pool)->items) return;
    memset((*pool)->items, 0, sizeof(Item *) * poolSize);
}

Like I told, if I want for example insert 1000 Items and allocate the memory by create function then everything works fine if I declare start Pool size for 100 elements and then I want to realloc Items I get the error.

Thank you very much for such quick answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文