为什么 Visual Studio 在我想检查结构分配时警告我 "Derefencing NULL pointer"?
Why Visual Studio is warning me of "Derefencing NULL pointer" when I want to check a struct allocation?
假设我有一个随机结构,例如国际象棋位置。
typedef char chessPos[2];
我有国际象棋位置的链接列表。
typedef struct _chessPosCell
{
chessPos position;
struct _chessPosCell* next;
} chessPosCell;
现在,我想创建一个指向列表的新指针。所以我使用以下内容:
chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell));
现在,我想检查内存是否分配正确。
所以我想创建一个特定的函数来检查我代码中的每个分配。
void checkAllocation(void* ptr)
{
if (ptr == NULL)
{
printf("Memory allocation failure. \n");
exit(1);
}
}
我的问题是,如何发送新分配的内存?
1.
checkAllocation(&answer);
checkAllocation(answer);
(区别只是'&')
我问这个是因为我一直在和一个朋友讨论。我正在使用选项 1,因为选项 2 给我 Visual Studio 警告“解引用 NULL 指针”答案。
朋友说我需要使用选项 2,因为我想检查“答案”分配,而不是地址分配。但是,选项 2 给了我上面提到的警告!
所以我有点困惑。任何人都可以向我解释这部分吗?
提前致谢!
- 如果您使用 Visual Studio 并用 C 语言(不是默认的 C++)编写,请更改项目属性使用的语言:
那么你将不需要(也不应该)投指针。
(The difference is just the '&')
&
给出对象的引用(地址)。
int *pointer = malloc(sizeof(*pointer));
定义类型为 pointer to int
的变量 pointer
并使用函数 malloc 返回的值对其进行初始化。
如果你想在存储值为 NULL 的情况下破解你想使用这个变量
if(pointer == NULL)
当您添加 &
时,您得到的是变量 pointer
的引用(地址),而不是存储在此变量中的值。
其他类型也一样。
int i = 5;
if(i == 5)
&i
将给出变量 i
的引用,而不是存储在此变量中的值。
使用checkAllocation(answer);
.
定义chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell));
调用malloc
保留一些内存。 malloc
returns 该内存的地址(或空指针)。该定义将 answer
的值初始化为具有该地址。
checkAllocation(&answer);
会将名为 answer
的对象的地址传递给 checkAllocation
。 chessAllocation(answer)
将名为 answer
的对象的值传递给 checkAllocation
。 answer
的值是malloc
的地址return,这就是你要检查的。
顺便说一下,定义最好写成chessPosCell *answer = malloc(sizeof *answer));
:
- 通过使用
sizeof *answer
而不是 sizeof(chessPosCell)
,您可以获得所指向类型的大小。即使稍后在代码编辑中更改 answer
的类型,这也将保持正确。
- C 自动从
void *
转换为其他指向对象的指针类型,因此不需要强制转换为 (chessPosCell*)
。有时使用强制转换可以抑制有关错误的警告消息,因为编译器假定强制转换是有意的,它们表明作者知道他们在做什么。
假设我有一个随机结构,例如国际象棋位置。
typedef char chessPos[2];
我有国际象棋位置的链接列表。
typedef struct _chessPosCell
{
chessPos position;
struct _chessPosCell* next;
} chessPosCell;
现在,我想创建一个指向列表的新指针。所以我使用以下内容:
chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell));
现在,我想检查内存是否分配正确。 所以我想创建一个特定的函数来检查我代码中的每个分配。
void checkAllocation(void* ptr)
{
if (ptr == NULL)
{
printf("Memory allocation failure. \n");
exit(1);
}
}
我的问题是,如何发送新分配的内存?
1.
checkAllocation(&answer);
checkAllocation(answer);
(区别只是'&')
我问这个是因为我一直在和一个朋友讨论。我正在使用选项 1,因为选项 2 给我 Visual Studio 警告“解引用 NULL 指针”答案。 朋友说我需要使用选项 2,因为我想检查“答案”分配,而不是地址分配。但是,选项 2 给了我上面提到的警告!
所以我有点困惑。任何人都可以向我解释这部分吗? 提前致谢!
- 如果您使用 Visual Studio 并用 C 语言(不是默认的 C++)编写,请更改项目属性使用的语言:
那么你将不需要(也不应该)投指针。
(The difference is just the '&')
&
给出对象的引用(地址)。
int *pointer = malloc(sizeof(*pointer));
定义类型为 pointer to int
的变量 pointer
并使用函数 malloc 返回的值对其进行初始化。
如果你想在存储值为 NULL 的情况下破解你想使用这个变量
if(pointer == NULL)
当您添加 &
时,您得到的是变量 pointer
的引用(地址),而不是存储在此变量中的值。
其他类型也一样。
int i = 5;
if(i == 5)
&i
将给出变量 i
的引用,而不是存储在此变量中的值。
使用checkAllocation(answer);
.
定义chessPosCell* answer = (chessPosCell*)malloc(sizeof(chessPosCell));
调用malloc
保留一些内存。 malloc
returns 该内存的地址(或空指针)。该定义将 answer
的值初始化为具有该地址。
checkAllocation(&answer);
会将名为 answer
的对象的地址传递给 checkAllocation
。 chessAllocation(answer)
将名为 answer
的对象的值传递给 checkAllocation
。 answer
的值是malloc
的地址return,这就是你要检查的。
顺便说一下,定义最好写成chessPosCell *answer = malloc(sizeof *answer));
:
- 通过使用
sizeof *answer
而不是sizeof(chessPosCell)
,您可以获得所指向类型的大小。即使稍后在代码编辑中更改answer
的类型,这也将保持正确。 - C 自动从
void *
转换为其他指向对象的指针类型,因此不需要强制转换为(chessPosCell*)
。有时使用强制转换可以抑制有关错误的警告消息,因为编译器假定强制转换是有意的,它们表明作者知道他们在做什么。