C 内存分配的调试断言错误

C 内存分配的调试断言错误,c,debugging,memory,assertion,C,Debugging,Memory,Assertion,当这段代码编译时,我得到一个“调试断言错误”,表达式为_CrtIsValidHeapPointer(block),有人能解释为什么会发生这种情况,以及如何解决吗 typedef struct _POOL { int size; void* memory; } Pool; Pool * allocatePool(int n) { int *p = (int*)malloc(sizeof(int) * n); Pool pool = { sizeof(*p),

当这段代码编译时,我得到一个“调试断言错误”,表达式为_CrtIsValidHeapPointer(block),有人能解释为什么会发生这种情况,以及如何解决吗

typedef struct _POOL
{
    int size;
    void* memory;

} Pool;

Pool * allocatePool(int n)
{

    int *p = (int*)malloc(sizeof(int) * n);

    Pool pool = { sizeof(*p), *p };

    return &pool;

}

void freePool(Pool* pool)
{
    free(pool);
}
你需要这个:

Pool * allocatePool(int n)
{
    int *p = malloc(sizeof(int) * n);
    Pool *pool = malloc(sizeof(Pool));
    pool->memory = p;
    pool->size = n;
    return pool;
}

顺便说一句:
malloc
的类型转换是不必要的。

您的方法中有两个错误:

  • 返回指向具有自动存储的变量的指针
  • 在不指向动态内存的指针上调用
    free()
  • 这意味着您还应该为
    对象动态分配空间。在
    freePool
    中,必须释放
    void*memory
    指向的内存和
    Pool*
    本身

    Pool *allocatePool(int n)
    {
        Pool *pool = malloc(sizeof *pool);
    
        if (!pool) {
            /* malloc failed, bail out */
            return NULL;
        }
    
        /* Caller is responsible for passing non negative value as n.
           Negative values will have suprising effects. */
        int *p = malloc(sizeof *p * n);
    
        /* Failed malloc, or n was 0 and current implementation returns
           NULL. */
        if (!p) {
            /* Clean up already allocated memory */
            free(pool);
            /* Many dislike multiple return points in a function, but
               I think for this little example it's ok. That, or gotos. */
            return NULL;
        }
    
        /* Storing the size of single item seems dubious. Size of the whole
           allocated area in objects is much more useful. */
        pool->size = n;
        pool->memory = p;
        return pool;    
    }
    

    这两个功能

    Pool * allocatePool(int n)
    {
    
        int *p = (int*)malloc(sizeof(int) * n);
    
        Pool pool = { sizeof(*p), *p };
    
        return &pool;
    
    }
    
    void freePool(Pool* pool)
    {
        free(pool);
    }
    
    没有道理

    例如,第一个函数返回指向
    Pool
    类型的本地对象的指针,该对象在退出函数后将不活动。此外,此初始化无效

    Pool pool = { sizeof(*p), *p };
    
    我想你的意思至少是

    Pool pool = { n, p };
    
    或许

    Pool pool = { n * sizeof( int ), p };
    
    这些函数可以按以下方式显示

    Pool allocatePool( int n )
    {
        Pool pool = { 0, NULL };
        void *p;
    
        if ( n > 0 && ( p = malloc( n * sizeof( int ) ) ) != NULL )
        {
            pool.size = n;
            pool.memory = p;
        }
    
        return pool;
    }
    
    void freePool( Pool *pool )
    {
        free( pool->memory );
        pool->size = 0;
        pool->memory = NULL;
    }
    

    戴维他们需要的是C++,而不是C。
    Pool allocatePool( int n )
    {
        Pool pool = { 0, NULL };
        void *p;
    
        if ( n > 0 && ( p = malloc( n * sizeof( int ) ) ) != NULL )
        {
            pool.size = n;
            pool.memory = p;
        }
    
        return pool;
    }
    
    void freePool( Pool *pool )
    {
        free( pool->memory );
        pool->size = 0;
        pool->memory = NULL;
    }