“Segmentation fault” vs “run time” error?

懵懂的女人 提交于 2019-12-02 14:37:51

问题


Consider this piece of snippet :

  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.


回答1:


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.

Declaring

char *str = "hellow Ghost";  

is equivalent to

char const *str = "hellow Ghost";  

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

C11: J.2 Undefined behavior:

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




回答2:


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

Yes.

A segfault is always a memory error at runtime.

What I understood by segmentation fault is : Segmentation fault when you are accessing a memory that doesn't belong to you.

No.

That can happen when you are accessing memory that doesn't belong to you. It is a symptom of the problem. But you are not promised that it will happen.

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.

It means that you accessed a segment of memory that hasn't been reserved for your process. Your specific OS implements this. Or it doesn't. It's not required.

are there any differences between run time error and segmentation fault regarding memory.

If a segfault happens, you are guaranteed that you had a run-time memory error.

If a run-time memory error happens, you are not guaranteed a segfault.




回答3:


A segfault is one of several possible runtime errors; other runtime errors may include things like dividing by zero, domain error, range error, stack overflow, etc.

The exact meaning of "segfault" or "segmentation fault" can vary based on the action and the environment; it can mean that you're trying to access memory you don't own, or that you're trying to perform a disallowed operation (modifying read-only memory, which is what you're attempting to do in this case), or that you're trying to dereference an invalid pointer value (such as NULL), or something else.

Note that the C language does not mandate that a segfault be raised when you attempt to modify a string literal; it only states that the behavior is undefined. A segfault is one of several possibilities. Another possible outcome is that the code behaves as you expected.




回答4:


Modifying a string literal is undefined behavior. On most implementations you would get segfault.



来源:https://stackoverflow.com/questions/24637879/segmentation-fault-vs-run-time-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!