当数组长度达到c中的0时,断言中止

当数组长度达到c中的0时,断言中止,c,arrays,struct,abort,assertion,C,Arrays,Struct,Abort,Assertion,此函数的目的是删除索引最高的元素,它基本上是数组的最后一个元素。我使用长度为5的数组运行了这段代码,它一直运行良好,直到到达数组长度为0的点。然后突然,它说由于ia->data的断言失败而中止。当它到达一个空数组时,为什么会给我一个断言错误 下面是我的代码的结构: typedef struct { int* data; unsigned int len; } intarr_t; 以下是删除索引最高的元素的函数: intarr_result_t intarr_pop( intarr_t*

此函数的目的是删除索引最高的元素,它基本上是数组的最后一个元素。我使用长度为5的数组运行了这段代码,它一直运行良好,直到到达数组长度为0的点。然后突然,它说由于ia->data的断言失败而中止。当它到达一个空数组时,为什么会给我一个断言错误

下面是我的代码的结构:

typedef struct {
  int* data;
  unsigned int len;
} intarr_t;
以下是删除索引最高的元素的函数:

intarr_result_t intarr_pop( intarr_t* ia, int* i )
{
    unsigned int len = ia->len;
    if (ia == NULL)
    {
        return INTARR_BADARRAY;
    }
    else
    {
        ia->data = realloc(ia->data, (sizeof(int)*(len-1)));
        assert (ia->data);
        if (ia->data != 0)
        {
            if (i != 0)
            {
                *i = ia->data[len-1]
                ia->len = len-1;
                return INTARR_OK;
            }
        }
        else
        {
            return INTARR_BADINDEX;
        }
    }
    return 0;
}

len
声明为
无符号int
。因此,如果
len
等于0,则该行

ia->data = realloc(ia->data, (sizeof(int)*(len-1)));   
                                           ^ (len-1) underflows if len == 0
将尝试在32位机器上分配4GB,或在64位机器上分配大量内存。无论哪种方式,如果分配失败并且
realloc
返回
NULL
,则
assert
将失败


为避免此问题,需要将
len==0
作为特例处理。

len
声明为
无符号int
。因此,如果
len
等于0,则该行

ia->data = realloc(ia->data, (sizeof(int)*(len-1)));   
                                           ^ (len-1) underflows if len == 0
per this link:
<https://www.securecoding.cert.org/confluence/display/seccode/MEM04-C.+Beware+of+zero-length+allocations>   

When the requested size is 0, 
the behavior of the memory allocation functions malloc(), calloc(), and realloc() 
is implementation-defined. 

Subclause 7.22.3 of the C Standard [ISO/IEC 9899:2011] states:

If the size of the space requested is zero, the behavior is implementation-defined: 
either a null pointer is returned, 
or the behavior is as if the size were some nonzero value, 
except that the returned pointer shall not be used to access an object.

it seems that your implementation returns NULL when the allocation size is 0
将尝试在32位机器上分配4GB,或在64位机器上分配大量内存。无论哪种方式,如果分配失败并且
realloc
返回
NULL
,则
assert
将失败


为避免此问题,需要将
len==0
作为特例处理。

len
声明为
无符号int
。因此,如果
len
等于0,则该行

ia->data = realloc(ia->data, (sizeof(int)*(len-1)));   
                                           ^ (len-1) underflows if len == 0
per this link:
<https://www.securecoding.cert.org/confluence/display/seccode/MEM04-C.+Beware+of+zero-length+allocations>   

When the requested size is 0, 
the behavior of the memory allocation functions malloc(), calloc(), and realloc() 
is implementation-defined. 

Subclause 7.22.3 of the C Standard [ISO/IEC 9899:2011] states:

If the size of the space requested is zero, the behavior is implementation-defined: 
either a null pointer is returned, 
or the behavior is as if the size were some nonzero value, 
except that the returned pointer shall not be used to access an object.

it seems that your implementation returns NULL when the allocation size is 0
将尝试在32位机器上分配4GB,或在64位机器上分配大量内存。无论哪种方式,如果分配失败并且
realloc
返回
NULL
,则
assert
将失败


为避免此问题,需要将
len==0
作为特例处理。

len
声明为
无符号int
。因此,如果
len
等于0,则该行

ia->data = realloc(ia->data, (sizeof(int)*(len-1)));   
                                           ^ (len-1) underflows if len == 0
per this link:
<https://www.securecoding.cert.org/confluence/display/seccode/MEM04-C.+Beware+of+zero-length+allocations>   

When the requested size is 0, 
the behavior of the memory allocation functions malloc(), calloc(), and realloc() 
is implementation-defined. 

Subclause 7.22.3 of the C Standard [ISO/IEC 9899:2011] states:

If the size of the space requested is zero, the behavior is implementation-defined: 
either a null pointer is returned, 
or the behavior is as if the size were some nonzero value, 
except that the returned pointer shall not be used to access an object.

it seems that your implementation returns NULL when the allocation size is 0
将尝试在32位机器上分配4GB,或在64位机器上分配大量内存。无论哪种方式,如果分配失败并且
realloc
返回
NULL
,则
assert
将失败

为避免此问题,您需要将
len==0
作为特殊情况处理。

通过此链接:
per this link:
<https://www.securecoding.cert.org/confluence/display/seccode/MEM04-C.+Beware+of+zero-length+allocations>   

When the requested size is 0, 
the behavior of the memory allocation functions malloc(), calloc(), and realloc() 
is implementation-defined. 

Subclause 7.22.3 of the C Standard [ISO/IEC 9899:2011] states:

If the size of the space requested is zero, the behavior is implementation-defined: 
either a null pointer is returned, 
or the behavior is as if the size were some nonzero value, 
except that the returned pointer shall not be used to access an object.

it seems that your implementation returns NULL when the allocation size is 0
当请求的大小为0时, 内存分配函数malloc()、calloc()和realloc()的行为 是否定义了实现。 C标准[ISO/IEC 9899:2011]第7.22.3款规定: 如果请求的空间大小为零,则行为由实现定义: 返回空指针, 或者行为就像大小是一些非零值, 但返回的指针不能用于访问对象。 当分配大小为0时,您的实现似乎返回NULL
通过此链接:
当请求的大小为0时,
内存分配函数malloc()、calloc()和realloc()的行为
是否定义了实现。
C标准[ISO/IEC 9899:2011]第7.22.3款规定:
如果请求的空间大小为零,则行为由实现定义:
返回空指针,
或者行为就像大小是一些非零值,
但返回的指针不能用于访问对象。
当分配大小为0时,您的实现似乎返回NULL
通过此链接:
当请求的大小为0时,
内存分配函数malloc()、calloc()和realloc()的行为
是否定义了实现。
C标准[ISO/IEC 9899:2011]第7.22.3款规定:
如果请求的空间大小为零,则行为由实现定义:
返回空指针,
或者行为就像大小是一些非零值,
但返回的指针不能用于访问对象。
当分配大小为0时,您的实现似乎返回NULL
通过此链接:
当请求的大小为0时,
内存分配函数malloc()、calloc()和realloc()的行为
是否定义了实现。
C标准[ISO/IEC 9899:2011]第7.22.3款规定:
如果请求的空间大小为零,则行为由实现定义:
返回空指针,
或者行为就像大小是一些非零值,
但返回的指针不能用于访问对象。
当分配大小为0时,您的实现似乎返回NULL

可能是因为当
len
0
时,
data
NULL
?您昨天已经问过这个问题,在第一条评论中,有人在检查
如果(ia==NULL)
之前告诉您
len=ia->len
是个坏主意。另外,用0(而不是
NULL
)调用
realloc
你期望得到什么?可能是因为
len
0
data
NULL
?你昨天已经问过这个问题,在第一条评论中,有人在检查
if>之前告诉你
len=ia->len(ia==NULL)
是个坏主意。另外,用0调用
realloc
会得到什么(除了
NULL
)?可能是因为当
len
0
时,
数据
NULL
?您昨天已经问过这个问题,在第一条评论中,有人在检查
如果(ia==NULL)
之前告诉您
len=ia->len
是一个坏主意。另外,用0调用
realloc
,您期望得到什么(除了
NULL
)?可能是因为当
len
0
时,
data
NULL
?您昨天已经问过这个问题,在第一条评论中,有人在检查
if(ia==NU>之前告诉您
len=ia->len