C++ SegFault when dereferencing a pointer for cout(取消引用 cout 指针时的 C++ SegFault)
问题描述
我是 C++ 新手,只是想掌握它.通常看起来还不错,但我偶然发现了这种奇怪/病态的段错误行为:
I'm new to C++ and just trying to get a hang of it. It generally seems not too bad, but I stumbled upon this weird/pathological segfaulting behavior:
int main () {
int* b;
*b = 27;
int c = *b;
cout << "c points to " << c << endl; //OK
printf( "b points to %d
", *b); //OK
// cout << "b points to " << (*b) << endl; - Not OK: segfaults!
return 0;
}
这个程序,如给定的,产生你所期望的:
This program, as given, produces what you'd expect:
c points to 27
b points to 27
另一方面,如果您取消对倒数第二行的注释,您会得到一个在运行时崩溃(seg-fault)的程序.为什么?这是一个有效的指针.
On the other hand, if you uncomment the second-to-last line, you get a program that crashes (seg-fault) in runtime. Why? This is a valid pointer.
推荐答案
int* b 指向一个未知的内存地址,因为它没有被初始化.如果您将它初始化为编译器存在的任何空指针值(0 直到 C++11,nullptr 在 C++11 和更高版本中),您肯定会早点得到一个段错误.问题在于您为指针分配了空间,但没有为它指向的数据分配空间.如果您改为这样做:
int* b points to an unknown memory address because it wasn't initialized. If you initialized it to whatever null pointer value exists for your compiler (0 until C++11, nullptr in C++11 and newer), you'd most certainly get a segfault earlier. The problem lies in the fact that you allocated space for the pointer but not the data it points to. If you instead did this:
int c = 27;
int* b = &c;
cout << "c points to " << c << endl;
printf ("b points to %d
", *b);
cout << "b points to " << (*b) << endl;
因为 int* b 指的是您的程序可以访问的内存位置(因为内存实际上是您程序的一部分),所以事情会起作用.
Things would work because int* b refers to a memory location that is accessible by your program (since the memory is actually a part of your program).
如果您未初始化指针或为其分配空值,则在它指向您知道可以访问的内存地址之前,您无法使用它.例如,使用带有 new 运算符的动态分配将为您的数据保留内存:
If you leave a pointer uninitialized or assign a null value to it, you can't use it until it points to a memory address that you KNOW you can access. For example, using dynamic allocation with the new operator will reserve memory for the data for you:
int* b = new int();
*b = 27;
int c = *b;
//output
delete b;
这篇关于取消引用 cout 指针时的 C++ SegFault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:取消引用 cout 指针时的 C++ SegFault
基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
