“分段故障"指的是“分段故障".与“运行时"错误? [英] "Segmentation fault" vs "run time" error?

查看:89
本文介绍了“分段故障"指的是“分段故障".与“运行时"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下片段:

  char *str = "hellow Ghost";
  str[0] = 'z';
  printf("%s", str);

这是分段错误.还会出现运行时内存错误吗?

It is a segmentation fault. Also does it come under run time memory error ?

我对分段错误的理解是:当您访问不属于您的内存时出现分段错误.它基本上是为您创建的实用程序,可在不破坏内存的情况下简化您的工作.

What I understood by segmentation fault is : Segmentation fault when you are accessing a memory that doesn't belong to you. It is basically a utility created for you to ease your work without letting you corrupt the memory.

分段错误涵盖了多少种内存错误,什么原因导致分段错误来检查指针或引用在处理内存方面是否错误.

How much and what kind of memory errors Segmentation fault covers and what invokes it to check out that a pointer or reference is wrong dealing with memory.

由于逻辑不正确,也会发生运行时错误.除此之外,关于内存的运行时错误和分段错误之间是否有任何区别.

Run time error also occurs due to improper logic. Apart from that, are there any differences between run time error and segmentation fault regarding memory.

推荐答案

这是分段错误.还会在运行时出现内存错误吗?

It is a segmentation fault. Also does it come under run time memory error ?

严格来说,这是未定义的行为.您可能会或可能不会遇到细分错误.什么事情都可能发生.

Strictly speaking, its an undefined behavior. You may or may not get segmentation fault. Any thing could happen.

声明

char *str = "hellow Ghost";  

等效于

char const *str = "hellow Ghost";  

字符串文字存储在只读节中,任何对其进行修改的尝试都会调用UB.

String literals are stored in read-only section and any attempt to modify it invokes UB.

C11:J.2未定义的行为:

在以下情况下该行为未定义:
...
—程序尝试修改字符串文字(6.4.5).

The behavior is undefined in the following circumstances:
...
— The program attempts to modify a string literal (6.4.5).

这篇关于“分段故障"指的是“分段故障".与“运行时"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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