Making changes in Code Segmentation issue

Making changes in Code(or read-only) segment

Question: The following code seg-faults (crashes). Can you tell the reason why?

int main(void)
    char *ptr = "Linux";
    *ptr = 'T';

    printf("\n [%s] \n", ptr);

    return 0;
Answer: This is because, through *ptr = ‘T’, the code is trying to change the first byte of the string ‘Linux’ kept in the code (or the read-only) segment in the memory. This operation is invalid and hence causes a seg-fault or a crash. appreciates your contribution please mail us the questions you have to so that it will be useful to our job search community

No comments:

Top Blogs