正在做 (ptr && *ptr) 检查是否有任何值分配给 ptr 的好方法?我的一直崩溃
is doing (ptr && *ptr) good way to check whether any value assigned to the ptr? mine just keep crashing
我不太确定使用 ptr&&(*ptr)
起初,我不确定这是为了检查 NULL ptr 还是赋值的存在。
但是现在在 运行 下面的代码之后我发现它在这两种方式中都不起作用:发生运行时错误!
有人可以解释一下里面发生了什么讨厌的事情吗?
海湾合作委员会 5.3.0
#include <stdio.h>
int main(void){
int* ptr=NULL;
// ptr=NULL;
int res = (ptr && *ptr);
//assigning NULL to *ptr or ptr address results in runtime error. What's happening?
//compiled with gcc.exe (GCC) 5.3.0 on windows
printf("address: %d \n val: %d \n",ptr,*ptr);
printf("isNullptr? ptr&& *ptr %d",res);
return;
}
下一行完全没问题,因为短路可以防止 ptr
在等于 NULL
.
时被取消引用
int res = (ptr && *ptr);
但是下面的代码无条件地取消引用它,导致崩溃。
int* ptr=NULL;
// ...
printf("address: %p \n val: %d \n",(void *)ptr,*ptr);
// ^~~~
此外,请注意打印指针的正确方法是使用 %p
而不是 %d
,并且指针必须转换为 void *
。我已经相应地更改了你的printf()
。
你认为 *ptr
(在 printf 中)是做什么的?它取消引用在初始化中已分配给 NULL
的指针。如果您将 NULL 传递给需要非 NULL 的函数,您的程序将调用 未定义的行为。打印地址的正确方法是使用 %p
说明符和 ptr-to-void:
if (ptr != NULL)
printf ("Pointer ptr points to address %p\n", (void *)ptr);
else
printf ("ptr is NULL\n");
从技术上讲,您有 undefined behaviour. It is a very bad thing。
(请记住,编程语言是在某些报告中编写的规范 - 而不是软件。对于 C11, read n1570)
实际上,你会得到 segmentation fault. You are dereferencing -in your first call to printf
- a (null) pointer which is always outside of your virtual address space。
您应该使用所有警告和调试信息进行编译,例如使用 gcc -Wall -g
如果使用 GCC. Then use a debugger (gdb
) and a memory leak detector like valgrind...
你需要多读书。仔细阅读几本关于编程的书籍(也许从 SICP), and several books on C programming. You might also want to read about operating systems.
开始
printf("address: %d \n val: %d \n",ptr,*ptr);
您试图解除对 NULL 指针的引用。这是未定义的行为。
来自 n1256 - §6.5.3.3 :
Among the invalid values for dereferencing a pointer by the unary *
operator are a null pointer, an address inappropriately aligned for
the type of object pointed to, and the address of an object after the
end of its lifetime.
正在做
int * ptr = NULL
...
printf("address: %d \n val: %d \n",ptr,*ptr);
结果:
首先 ptr
使用空指针常量进行初始化。为此,C 标准保证 ptr
不指向任何有效对象。 (*1)
C11标准(草案)6.3.2.3/3:
If a null pointer constant is converted to a
pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
然后在第二个语句中做 *ptr
被取消引用,也就是说它试图访问它指向的内容,这是无效的(见上面的 1.),导致Undefined Behaviour,从此以后什么事都有可能发生,包括程序崩溃。
C11标准(草案)6.5.3.2/4:
If an invalid value has been assigned to the pointer, the behavior of the unary *
operator is undefined.
我不太确定使用 ptr&&(*ptr)
起初,我不确定这是为了检查 NULL ptr 还是赋值的存在。
但是现在在 运行 下面的代码之后我发现它在这两种方式中都不起作用:发生运行时错误!
有人可以解释一下里面发生了什么讨厌的事情吗?
海湾合作委员会 5.3.0
#include <stdio.h>
int main(void){
int* ptr=NULL;
// ptr=NULL;
int res = (ptr && *ptr);
//assigning NULL to *ptr or ptr address results in runtime error. What's happening?
//compiled with gcc.exe (GCC) 5.3.0 on windows
printf("address: %d \n val: %d \n",ptr,*ptr);
printf("isNullptr? ptr&& *ptr %d",res);
return;
}
下一行完全没问题,因为短路可以防止 ptr
在等于 NULL
.
int res = (ptr && *ptr);
但是下面的代码无条件地取消引用它,导致崩溃。
int* ptr=NULL;
// ...
printf("address: %p \n val: %d \n",(void *)ptr,*ptr);
// ^~~~
此外,请注意打印指针的正确方法是使用 %p
而不是 %d
,并且指针必须转换为 void *
。我已经相应地更改了你的printf()
。
你认为 *ptr
(在 printf 中)是做什么的?它取消引用在初始化中已分配给 NULL
的指针。如果您将 NULL 传递给需要非 NULL 的函数,您的程序将调用 未定义的行为。打印地址的正确方法是使用 %p
说明符和 ptr-to-void:
if (ptr != NULL)
printf ("Pointer ptr points to address %p\n", (void *)ptr);
else
printf ("ptr is NULL\n");
从技术上讲,您有 undefined behaviour. It is a very bad thing。
(请记住,编程语言是在某些报告中编写的规范 - 而不是软件。对于 C11, read n1570)
实际上,你会得到 segmentation fault. You are dereferencing -in your first call to printf
- a (null) pointer which is always outside of your virtual address space。
您应该使用所有警告和调试信息进行编译,例如使用 gcc -Wall -g
如果使用 GCC. Then use a debugger (gdb
) and a memory leak detector like valgrind...
你需要多读书。仔细阅读几本关于编程的书籍(也许从 SICP), and several books on C programming. You might also want to read about operating systems.
开始printf("address: %d \n val: %d \n",ptr,*ptr);
您试图解除对 NULL 指针的引用。这是未定义的行为。
来自 n1256 - §6.5.3.3 :
Among the invalid values for dereferencing a pointer by the unary * operator are a null pointer, an address inappropriately aligned for the type of object pointed to, and the address of an object after the end of its lifetime.
正在做
int * ptr = NULL
...
printf("address: %d \n val: %d \n",ptr,*ptr);
结果:
首先
ptr
使用空指针常量进行初始化。为此,C 标准保证ptr
不指向任何有效对象。 (*1)C11标准(草案)6.3.2.3/3:
If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
然后在第二个语句中做
*ptr
被取消引用,也就是说它试图访问它指向的内容,这是无效的(见上面的 1.),导致Undefined Behaviour,从此以后什么事都有可能发生,包括程序崩溃。C11标准(草案)6.5.3.2/4:
If an invalid value has been assigned to the pointer, the behavior of the unary
*
operator is undefined.