What is the meaning of segmentation fault? What causes the fault to arise in mai
ID: 3909550 • Letter: W
Question
What is the meaning of segmentation fault? What causes the fault to arise in main() when executing a.out? Add two additional #ifdef conditional debug printf() calls in the function changeval() to pinpoint where the seg fault occurs.
#include <stdio.h>
#define MYDEBUG
int main()
{
int x;
void changeval(int *);
x = 6;
printf("%d ",x);
#ifdef MYDEBUG
printf("ok 1 ");
#endif
changeval(&x);
#ifdef MYDEBUG
printf("ok 2 ");
#endif
printf("%d ",x);
#ifdef MYDEBUG
printf("ok 3 ");
#endif
changeval(x);
#ifdef MYDEBUG
printf("ok 4 ");
#endif
printf("%d ",x);
}
void changeval(int *a)
{
*a = 3;
}
Explanation / Answer
Hi
I have fixed the issue and highlighted the code changes below
A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed
#include <stdio.h>
#define MYDEBUG
int main()
{
int x;
void changeval(int *);
x = 6;
printf("%d ",x);
#ifdef MYDEBUG
printf("ok 1 ");
#endif
changeval(&x);
#ifdef MYDEBUG
printf("ok 2 ");
#endif
printf("%d ",x);
#ifdef MYDEBUG
printf("ok 3 ");
#endif
changeval(&x);
#ifdef MYDEBUG
printf("ok 4 ");
#endif
printf("%d ",x);
}
void changeval(int *a)
{
*a = 3;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.