1

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 sounds like you're asking _"What kinds of runtime memory errors exist, other than segfault?"_ Is that true? – Drew Dormann Jul 08 '14 at 17:25
  • My understanding is that *run time error* is any error that occurs during the execution of a program. Therefor a *run time memory error* is an error, involving memory, that occurs during a program execution. So one could say that a segmentation fault is a subclass or specialization of run time memory errors. If your platform's memory is not segmented, this would make no sense. – Thomas Matthews Jul 08 '14 at 17:28
  • regarding the line: char *str = "hellow Ghost"; produces a char pointer on the stack that is pointing to a string in the (I think) .const or .data area of the program image. so the line: str[0] = 'z'; is trying to change an area that may not available for change. Also, the line: printf("%s", str); is expecting the 'str' to be the first address of the string, however it is actually a pointer to the first address of the string. so the line should be: printf("%s", *str); – user3629249 Jul 09 '14 at 01:01

4 Answers4

9

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).

haccks
  • 104,019
  • 25
  • 176
  • 264
  • @pcGhost; Mind your language. – haccks Jul 08 '14 at 17:21
  • Would it be `const char*`? – rslemos Jul 08 '14 at 17:27
  • @rslemos; Better it should be `char const *`! Type qualifiers and specifiers can appear in any order. – haccks Jul 08 '14 at 17:28
  • @haccks, hummm. cdecl.org doesn't recognize it yet. Also it doesn't recognize the new qualifiers for arrays (like `char str[const]` for parameters). – rslemos Jul 08 '14 at 17:29
  • @rslemos; I don't know why it is not recognizing `char str[const]`, but it is a well defined syntax by [standard](http://stackoverflow.com/a/24627825/2455888). – haccks Jul 08 '14 at 18:19
  • I think cdecl.org implements an older standard. Perhaps C99. Or even older. That's why it doesn't recognize char const* yet. – rslemos Jul 09 '14 at 00:41
9

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.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
1

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

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

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345