Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi guys can you guys help me solve this problem when I run final 10 I get segmen

ID: 3678770 • Letter: H

Question

Hi guys can you guys help me solve this problem when I run final 10 I get segmentation error please help me fix it

#include <stdio.h>
#include <stdlib.h>

double drand48();

int main(int argc, char *argv[])
{
srand48(0);
double x=0, y=0, sum=0, r=0, arg1=0;

x = drand48();
y = drand48();

arg1 = atoi(argv[1]);

//assembly code to find r
asm(//multiply x twice
" fldl %[x] "//load x onto floating point stack
" fld %%st "// duplicate value on top of of fp stack
" fmulp "// multiply the two values on the fp stack
" fstpl %[x] "//store the mult back into x
  
//Multiply y twice
" fldl %[y] "//load y onto floating point stack
" fld %%st "// duplicate value on top of of fp stack
" fmulp "// multiply two values on the fp stack
" fstpl %[y] "//store mult back into y
  
//Add value of x and y
" fldl %[x] "//load x onto floating point stack
" fldl %[y] "//load y onto floating point stack
" faddp "//Add x and y
" fstpl %[sum] "//store in variable sum
  
//Take Square root of the sum x^2 + y^2
" fldl %[sum] "//load sum onto floating point stack
" fsqrt "//Take square root
" fstpl %[r] "//store in variable r

: [r] "=m" (r),
[sum] "=m" (sum)

: [x]       "m"   (x),
[y]       "m"   (y)  
);
  
//if statement to check if r is less than 1
int pi=0,in=0, all=0 ;
arg1 = atoi(argv[2]);
while(arg1<atoi(argv[2])){

if(r<=1){
in++;
};

all++;

}

pi = 4*(in/all);

return pi;
}

Explanation / Answer

Hi, Below I have exaplined the steps to rectify your problem and understand the Segmentation fault cause and troubleshoot,

The error occurs when a process attempts to access memory in a way that the operating system and processor cannot or will not allow. The most common cause is reading from or writing to memory that the process is not authorized to so access. For example, processes do not have access to the page of memory beginning at 0x0 and thus dereferencing the NULL pointer results in a segmentation fault.

I'll attempt to tell you how you might go about finding and maybe even fixing them.

Suggested steps for trouble shooting:

1. If we use data instead of address (integer to pointer conversion) !

also you can only write format specifier inside scanf

ex:- scanf("hello %s", &s); // this will cause seg fault

2. If we allocate wrong size during memory allocation !

struct node* newNode=(struct node*) malloc(sizeof(struct node*));

// ^ * should not be

// present as it will become pointer

// hence size of ptr(4) will be allocated instead of the whole structure

3. If we are freeing memory twice !

4. If we have not allocated memory for pointer !

5. If we try to access I/O devices or other hardware components directly by using address. ex:- VGA !

6 . Stack Overflow !

7. When we try to write data in the array beyond boundary limit !

  int a[2]={0,1};

printf("%d ", a[3]); // this will work. no problem

a[3]=55; // this can cause seg fault

8. If we try to dereference a NULL Pointer

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote