取消引用 cout 指针时的 C++ SegFault

2022-01-12 00:00:00 pointers segmentation-fault c++ cout

我是 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;

相关文章