什么是“空指针分配错误"? [英] What is "null pointer assignment error"?

查看:92
本文介绍了什么是“空指针分配错误"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于C指针的求职面试问题之一

One of job interview questions on C pointers here is the following: what is null pointer assignment error?

我已经搜索了一段时间,没有看到任何合理的解释.那是什么?试图写一个空指针?某些特定于体系结构或环境的东西?那个错误到底是什么?

I've googled for a while and don't see any reasonable explanation. What is that? Trying to write through a null pointer? Something architecture- or environment-specific? What exactly is that error?

推荐答案

http://www.faqs.org/qa/qa-3786.html

NULL指针分配是运行时错误 发生这种情况的原因多种多样,其中之一是您的程序试图访问非法的内存位置. 非法位置意味着该位置在操作系统地址空间中或在其他进程的内存空间中. 在stdio.h中,NULL定义为0 因此,每当您的程序尝试访问第0个位置时,操作系统都会由于运行时分配错误而杀死您的程序,因为第0个位置位于操作系统的地址空间中,并且操作系统不允许用户程序访问其地址空间.

A NULL pointer assignment is a runtime error It occurs due to various reasons one is that your program has tried to access an illegal memory location. Illegal location means either the location is in the operating systems address space or in the other processes memory space. In stdio.h NULL is defined as 0 So whenever your program tries to access 0th location the operating system kills your program with runtime assignment error because the 0th location is in the operating systems address space and operating system doesn't allow access to its address space by user program .

示例代码:

int* ptr = NULL;  
*ptr = 3;

说明:
在几乎每个系统上,地址0都是保留的.系统不允许您写入该位置.如果尝试尝试,则会遇到运行时异常(访问冲突,分段错误等).

Explanation:
On almost every system, address 0 is reserved. System won't allow you to write to that location. If you try, you will get a runtime exception (access violation, segmentation fault, etc.).

这篇关于什么是“空指针分配错误"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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