Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中使用calloc的2D数组无符号字符分段错误_C_Arrays_Char_Unsigned_Calloc - Fatal编程技术网

C中使用calloc的2D数组无符号字符分段错误

C中使用calloc的2D数组无符号字符分段错误,c,arrays,char,unsigned,calloc,C,Arrays,Char,Unsigned,Calloc,我正在尝试使用calloc分配一个无符号字符**: newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char)); for (j=0 ; j < width; j++) { if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){ printf("Memory allocation error

我正在尝试使用calloc分配一个
无符号字符**

newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char));

for (j=0 ; j < width; j++)
{
    if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){
        printf("Memory allocation error. Exit program\n");
        exit(1);
    }
}

for (i=0;i<width;i++)
{
    for(j=0;j<width;j++)
    {
        newmatriz[i][j] = 0;
    }
}
newmatriz=(无符号字符**)calloc(宽度,sizeof(无符号字符));
对于(j=0;j对于(i=0;i答案是简单的newmatriz是一个指向无符号字符*的数组(又称指针)。您正在分配无符号字符,只需更改顶行以分配大小正确的数组,在这种情况下,您需要的数组是字节大小
width*sizeof(unsigned char*)
而不是当前的
width*sizeof(unsigned char)

newmatriz = (unsigned char**) calloc(width, sizeof(unsigned char*));
for (j=0 ; j < width; j++){
    if (newmatriz[j]=(unsigned char*)calloc(witdh, sizeof(unsigned char)) == NULL){
       printf("Memory allocation error. Exit program\n");
        exit(1);
    }
}
newmatriz=(无符号字符**)calloc(宽度,sizeof(无符号字符*);
对于(j=0;j
此语句中有一个输入错误。您必须使用
sizeof(unsigned char*)而不是
sizeof(unsigned char*)

此外,此if语句不正确

if ( newmatriz[j] = calloc(witdh, sizeof(unsigned char) ) == NULL){
在此语句中,
newmatriz[j]
被设置为1或0,具体取决于内存分配是否成功

我想你是说

if ( ( newmatriz[j] = calloc(witdh, sizeof(unsigned char) ) ) == NULL){
这些循环

for (i=0;i<width;i++)
{
    for(j=0;j<width;j++)
    {
        newmatriz[i][j] = 0;
    }
}
for(i=0;i在第二个
calloc()
中缺少一些括号:

if (newmatriz[j] = calloc(witdh, sizeof(unsigned char)) == NULL){
应该是

if ((newmatriz[j] = calloc(witdh, sizeof(unsigned char))) == NULL){

如果没有这一点,
calloc()
s结果将与NULL进行比较,比较结果而不是指针存储在
newmatriz[j]

应该是
newmatriz=calloc(width,sizeof(unsigned char*)
不要强制转换
calloc()
朋友返回的
void*
。另外:不要使用
sizeof(char)
(有符号/无符号相同)。这永远不会与1不同,因为标准明确定义为1。代码缺少星号(请参阅BLUEPIXY的注释)。投票以键入错误结束。我从calloc中删除了强制转换,但仍然得到相同的错误。事实上,我在编译时收到此警告:filterDefinitivo.c:298:29:警告:赋值从整数生成指针,但没有强制转换[默认启用]如果(newmatriz[j]=calloc(width,sizeof(unsigned char))=NULL){@user3055867,这是不显式强制转换
malloc
函数族的返回值的一个很好的理由。您通过使用显式强制转换隐藏了一个问题。我以前和现在也尝试过,但仍然不起作用。只要您没有耗尽正在测试的内存,应该可以工作。您好,指针正在修复循环只是为了检查对矩阵的访问,但我发现了分段错误。@user3055867请参阅我更新的帖子。下次请尝试复制并粘贴至少一个已编译的示例来演示问题。谢谢!!这就是错误!!现在它编译时没有警告,似乎可以工作了!@user3055867您应该始终注意警告。
if ((newmatriz[j] = calloc(witdh, sizeof(unsigned char))) == NULL){