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
基础教程推荐
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- Windows Media Foundation 录制音频 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01