如果传入空指针,以下代码是否安全?
if(ptr && *ptr == value)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
检查的顺序是否重要?如果我改成它,它会起作用吗?
if(*ptr == value && ptr)
{
//do something
}
Run Code Online (Sandbox Code Playgroud) 我正在阅读一篇关于nullptrC++ 中一些特性的文章,一个特定的例子在我的理解中引起了一些混乱。
考虑(来自上述帖子的简化示例):
struct A {
void non_static_mem_fn() {}
static void static_mem_fn() {}
};
A* p{nullptr};
/*1*/ *p;
/*6*/ p->non_static_mem_fn();
/*7*/ p->static_mem_fn();
Run Code Online (Sandbox Code Playgroud)
根据作者的说法/*1*/,取消引用 the 的表达式nullptr本身不会导致未定义的行为。与/*7*/使用nullptr-object 调用静态函数的表达式相同。
理由基于C++ Standard Core Language Closed Issues, Revision 100中的issue 315
...
*pis not an error whenpis null 除非左值被转换为右值(7.1 [conv.lval]),它不在这里。
从而区分/*6*/和/*7*/。
因此,对实际的间接引用nullptr 不是未定义行为(在SO答案,在C ++标准的问题232讨论,...)。因此,/*1*/在这个假设下, 的有效性是可以理解的。
但是,如何 …
c++ static-methods null-pointer undefined-behavior language-lawyer
如果我实际上没有访问解除引用的"对象",那么取消引用仍然未定义的空指针?
int* p = 0;
int& r = *p; // undefined?
int* q = &*p; // undefined?
Run Code Online (Sandbox Code Playgroud)
一个稍微更实际的例子:我可以取消引用空指针来区分重载吗?
void foo(Bar&);
void foo(Baz&);
foo(*(Bar*)0); // undefined?
Run Code Online (Sandbox Code Playgroud)
好的,根据标准,参考示例肯定是未定义的行为:
在一个定义良好的程序中不能存在空引用,因为创建这样一个引用的唯一方法是将它绑定到通过解引用空指针获得的"对象",这会导致未定义的行为.
不幸的是,强调的部分是模棱两可的.它是导致未定义行为的绑定部分,还是取消引用部分足够?
#include <iostream>
int main()
{
int* i = 0;
int x = (*i);
std::cout << x;
}
Run Code Online (Sandbox Code Playgroud)
当我编译并运行它时,上面的程序将崩溃Visual Studio 2010,我知道它崩溃,因为我将指针设置为0.
我想知道的是,访问null pointer标准中定义的C++,还是未定义的,我很幸运,因为我的编译器/计算机/操作系统,我的程序崩溃了
如果定义了,当我尝试访问空指针时C++会保证什么?
在C/C++中,不允许访问地址0处的数据.
但是,物理内存从0开始编号.在DOS时代,中断向量表位于物理地址0处.第一个中断向量是除零异常的处理程序.
我的问题是:
在什么情况下允许访问物理地址0?
我很好奇解除引用NULL ptr的哪一部分会导致不良行为.例:
// #1
someObj * a;
a = NULL;
(*a).somefunc(); // crash, dereferenced a null ptr and called one of its function
// same as a->somefunc();
// #2
someObj * b;
anotherObj * c;
b = NULL;
c->anotherfunc(*b); // dereferenced the ptr, but didn't call one of it's functions
Run Code Online (Sandbox Code Playgroud)
在这里我们在#2中看到我实际上并没有尝试从b中访问数据或函数,所以如果*b刚解析为NULL并且我们将NULL传递给anotherfunc(),这仍会导致意外行为吗?
空指针取消引用通常会导致运行时错误,程序立即崩溃.为什么不使这些情况特殊,抛出异常并允许程序员处理它运行时并保持程序运行?
我想测试我为嵌入式系统编写的异常处理程序函数,并想编写一个测试代码来注入对禁止的内存的访问。
void Test_Mem_exception
{
__asm(
"LDR R0, =0xA0100000\n\t"
"MOV R1, 0x77777777\n\t"
"STR R1, [R0,#0]"
);
Run Code Online (Sandbox Code Playgroud)
这是我想在 0xA010000 处写入访问内存位置的代码。不知何故,这对我来说似乎不是一个通用的测试代码。
是否有用 C 或 C++ 编写此类测试代码的标准方法。通用我的意思是一个独立于它运行的系统的内存映射的代码。
我编写了应该导致访问冲突的示例代码,但事实并非如此.我认为异常应该发生在GetSession1(),GetSession2()函数for return *m_pObj,但它没有.这是为什么?
头文件
class CSession
{
public:
CSession() {};
~CSession() {};
CSession(const CSession& rhs) {};
private:
long m_lUSN;
};
class CTest
{
public:
CSession* m_pObj;
CSession& GetSesstion1() { m_pObj = NULL; return *m_pObj; }
CSession GetSesstion2(); { m_pObj = NULL; return *m_pObj; }
};
Run Code Online (Sandbox Code Playgroud)
Cpp文件
int _tmain(int argc, _TCHAR* argv[])
{
CTest test;
CSession Session2 = test.GetSesstion1();
CSession Session3 = test.GetSesstion2();
return 0;
};
Run Code Online (Sandbox Code Playgroud) 灵感来自这个问题.
码:
#include <stdio.h>
int main()
{
char arr[] = "Hello";
char *ptr = arr + 5;
printf("%s\n",ptr);
}
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我访问了以null结尾的字符.
那么,在文字字符串中访问空终止字符时实际发生了什么?它是未定义的行为吗?