Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the following will produce segmentation fault?

int main()
{
        char *temp = "Paras";

        int i;
        i=0;

        temp[3]='F';

        for (i =0 ; i < 5 ; i++ )
                printf("%c\n", temp[i]);

        return 0;
}

Why temp[3]='F'; will cause segmentation fault since temp is not const?

like image 314
cse Avatar asked Nov 20 '10 03:11

cse


People also ask

What are the reasons for segmentation fault?

Overview. A segmentation fault (aka segfault) is a common condition that causes programs to crash; they are often associated with a file named core . Segfaults are caused by a program trying to read or write an illegal memory location.

Why do we get segmentation fault in Python?

Tip: A segmentation fault (also known as segfault) is a common condition that causes programs to crash; A segmentation fault is typically caused by a program trying to read from or write to an illegal memory location, that is, part of the memory to which the program is not supposed to have access.

Why does segmentation fault occur in C?

Core Dump (Segmentation fault) in C/C++ Core Dump/Segmentation fault is a specific kind of error caused by accessing memory that “does not belong to you.” When a piece of code tries to do read and write operation in a read only location in memory or freed block of memory, it is known as core dump.

What are three kinds of pointers that can cause a segmentation fault?

Dereferencing or assigning to an uninitialized pointer (wild pointer, which points to a random memory address) Dereferencing or assigning to a freed pointer (dangling pointer, which points to memory that has been freed/deallocated/deleted) A buffer overflow. A stack overflow.


1 Answers

You are not allowed to modify string literals.

like image 74
Bill Lynch Avatar answered Nov 08 '22 15:11

Bill Lynch