在 C 中使用 calloc 的二维数组 unsigned char 中的分段错误
Segmentation fault in 2D array unsigned char using calloc in C
我正在尝试使用 calloc:
分配一个 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);
}
}
for (i=0;i<width;i++)
{
for(j=0;j<width;j++)
{
newmatriz[i][j] = 0;
}
}
但是当我尝试访问 pos [i][j] 时出现分段错误
问题是否与使用 int 作为迭代器有关?
答案很简单 newmatriz 是一个指向 unsigned char* 的数组(也称为指针)* 您正在分配 unsigned char 只需更改顶行以分配正确大小的数组,在这种情况下您需要一个字节大小的数组 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);
}
}
此语句中有错字。而不是 sizeof( unsigned char )
你必须使用 sizeof( unsigned char * )
newmatriz = (unsigned char**) calloc(width, 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;
}
}
没有意义,因为 calloc
已经用零初始化分配的内存。
在第二个 calloc()
中缺少一些括号:
if (newmatriz[j] = calloc(witdh, sizeof(unsigned char)) == NULL){
应该是
if ((newmatriz[j] = calloc(witdh, sizeof(unsigned char))) == NULL){
否则,calloc()
s 的结果将与 NULL 进行比较,并且比较结果而不是指针存储在 newmatriz[j]
中
我正在尝试使用 calloc:
分配一个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);
}
}
for (i=0;i<width;i++)
{
for(j=0;j<width;j++)
{
newmatriz[i][j] = 0;
}
}
但是当我尝试访问 pos [i][j] 时出现分段错误
问题是否与使用 int 作为迭代器有关?
答案很简单 newmatriz 是一个指向 unsigned char* 的数组(也称为指针)* 您正在分配 unsigned char 只需更改顶行以分配正确大小的数组,在这种情况下您需要一个字节大小的数组 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);
}
}
此语句中有错字。而不是 sizeof( unsigned char )
你必须使用 sizeof( unsigned char * )
newmatriz = (unsigned char**) calloc(width, 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;
}
}
没有意义,因为 calloc
已经用零初始化分配的内存。
在第二个 calloc()
中缺少一些括号:
if (newmatriz[j] = calloc(witdh, sizeof(unsigned char)) == NULL){
应该是
if ((newmatriz[j] = calloc(witdh, sizeof(unsigned char))) == NULL){
否则,calloc()
s 的结果将与 NULL 进行比较,并且比较结果而不是指针存储在 newmatriz[j]