崩溃或“分段错误"当数据被复制/扫描/读取到未初始化的指针时

编程入门 行业动态 更新时间:2024-10-12 20:20:44
本文介绍了崩溃或“分段错误"当数据被复制/扫描/读取到未初始化的指针时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题旨在作为所有常见问题的参考:

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 where an uninitialised pointer points to?

例如:

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

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* the null terminator strcpy(good, "hello");

请注意,您还可以通过让它指向 NULL 来设置一个指向明确定义的无处"的指针.这使它成为空指针,这是一个保证不指向任何有效内存的指针.这与让指针完全未初始化不同.

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

然而,如果您尝试访问由空指针指向的内存,您可能会遇到与使用未初始化指针时类似的问题:崩溃或分段错误.在最好的情况下,您的系统会注意到您正在尝试访问地址 null,然后抛出空指针异常".

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.

进一步阅读:

指向无效数据的指针如何使用指针从不同的函数访问局部变量?局部变量的内存可以在其作用域外访问吗?

分段错误及原因什么是分段错误?为什么要这样做写入用char *s"初始化的字符串时出现分段错误但不是char s[]"?char s有什么区别[] 和字符 *s?分割错误常见原因的明确列表什么是总线错误?

更多推荐

崩溃或“分段错误"当数据被复制/扫描/读取到未初始化的指针时

本文发布于:2023-07-28 02:29:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1226463.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   指针   错误   数据   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!