为什么我得到一个神秘的崩溃或QUOT;分段故障"当I /扫描数据复​​制到一个未初始化的指针? [英] Why do I get a mysterious crash or "segmentation fault" when I copy/scan data to an uninitialized pointer?

查看:140
本文介绍了为什么我得到一个神秘的崩溃或QUOT;分段故障"当I /扫描数据复​​制到一个未初始化的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是为了用作性质的所有常见问题参考:

为什么我收到一个神秘的崩溃或段错误时,I /扫描数据复​​制到一个未初始化的指针的地址?

例如:

 的char * PTR;
的strcpy(PTR,世界你好); //崩溃了!

 的char * PTR;
scanf函数(%S,PTR); //崩溃了!


解决方案

一个指针是一种特殊类型的变量,它可以只包含另一个变量的地址。它不能包含任何数据。你不能复制/存储数据到指针 - 没有任何意义。你只能设置一个指向别处分配的数据点。

这意味着,为了对一个指针有意义,它必须始终在一个有效的内存位置指向。例如,它可以在栈上分配的内存点:

  {
  int数据= 0;
  为int * PTR =放大器;数据;
  ...
}

或者在堆上动态分配的内存:

 为int * PTR =的malloc(sizeof的(INT));

它始终是使用指针已初始化之前的一个错误。这还没有在有效的记忆点。

这些例子都可能导致程序崩溃或其他类型的意外行为,如分段错误:

  / ***不正确使用指针的例子*** /// 1。
INT *坏;
*坏= 42;// 2。
字符*坏;
的strcpy(坏,你好);

相反,你必须确保在(足够的)分配的内存指针指向:

  / ***正确使用指针的例子*** /// 1。
INT无功;
为int *好=放大器; VAR;
*好= 42;// 2。
字符*好=的malloc(5 + 1); //分配内存为5个字符和1终结者
的strcpy(好,你好);


请注意,您也可以设置一个指针指向一个明确定义的无门点,通过让它指向 NULL 。这使得它的空指针的,这是保证不会在任何有效的存储器指向的指针。这是离开指针完全不同的初始化

 为int * P1 = NULL; //指向无处
INT * P2; //未初始化的指针,指针为无处不在,目前还不能使用

然而,你应该尝试通过一个空指针访问内存指着,你可以得到类似的问题使用未初始化的指针时为:崩溃或段故障。在最好的情况下,你的系统通知您试图访问地址为空,然后抛出空指针异常。

有关空指针异常bug的解决方法是一样的:你必须在使用它之前设定的指针在有效的内存指向


延伸阅读:

指针在无效数据指向结果
指向局部变量结果
<一href=\"http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope\">Can一个局部变量的内存是它的范围之外访问?

分段故障,并导致结果
什么是段错误?结果
<一href=\"http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha\">Why做写有字符*的初始化字符串时,而不是个char []?结果我得到一个分段错误
<一href=\"http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c\">What是个char []和char * S用C?结果之间的差异
<一href=\"http://stackoverflow.com/questions/33047452/definitive-list-of-common-reasons-for-segmentation-faults\">Definitive对于段错误结果的常见原因一览
什么是总线错误?

This question is meant to be used as reference for all frequently asked questions of the nature:

Why do I get a mysterious crash or "segmentation fault" when I copy/scan data to the address of an uninitialized pointer?

For example:

char* ptr;
strcpy(ptr, "hello world"); // crash here!

or

char* ptr;
scanf("%s", ptr); // crash here!

解决方案

A pointer is a special type of variable, which can only contain an address of another variable. It cannot contain any data. You cannot "copy/store data into a pointer" - that doesn't make any sense. You can only set a pointer to point at data allocated elsewhere.

This means that in order for a pointer to be meaningful, it must always point at a valid memory location. For example it could point at memory allocated on the stack:

{
  int data = 0;
  int* ptr = &data;
  ...
}

Or memory allocated dynamically on the heap:

int* ptr = malloc(sizeof(int));

It is always a bug to use a pointer before it has been initialized. It does not yet point at valid memory.

These examples could all lead to program crashes or other kinds of unexpected behavior, such as "segmentation faults":

/*** examples of incorrect use of pointers ***/

// 1.
int* bad;
*bad = 42;

// 2.
char* bad;
strcpy(bad, "hello");

Instead, you must ensure that the pointer points at (enough) allocated memory:

/*** examples of correct use of pointers ***/

// 1.
int var;
int* good = &var;
*good = 42;

// 2.
char* good = malloc(5+1); // allocates memory for 5 characters and 1 terminator
strcpy(good, "hello");


Note that you can also set a pointer to point at a well-defined "nowhere", by letting it point to NULL. This makes it a null pointer, which is a pointer that is guaranteed not to point at any valid memory. This is different from leaving the pointer completely uninitialized.

int* p1 = NULL; // pointer to nowhere
int* p2;        // uninitialized pointer, pointer to "anywhere", cannot be used yet

Yet, should you attempt to access the memory pointed at by a null pointer, you can get similar problems as when using an uninitialized pointer: crashes or segmentation faults. In the best case, your system notices that you are trying to access the address null and then throws a "null pointer exception".

The solution for null pointer exception bugs is the same: you must set the pointer to point at valid memory before using it.


Further reading:

Pointers pointing at invalid data
Pointer to local variable
Can a local variable's memory be accessed outside its scope?

Segmentation fault and causes
What is a segmentation fault?
Why do I get a segmentation fault when writing to a string initialized with "char *s" but not "char s[]"?
What is the difference between char s[] and char *s in C?
Definitive List of Common Reasons for Segmentation Faults
What is a bus error?

这篇关于为什么我得到一个神秘的崩溃或QUOT;分段故障&QUOT;当I /扫描数据复​​制到一个未初始化的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆