segmentation fault dunnu why

Status
Not open for further replies.

Kenshin

meowww meoww
I wrote a program for booth's algorithm for signed multiplication.Attached is the c code...I am gettin segmentation fault...whyy :confused:


kenshin@cutie:~$ ./a.out

Enter the two numbers
-3
7

Segmentation fault (core dumped)
kenshin@cutie:~$
 
Last edited:
OP
Kenshin

Kenshin

meowww meoww
Heres the attachment....actually we elex ppl are not much familier with C...pls check up wat pointers i hv messed up



Code:
#include <stdio.h>

#include <math.h>

 void bin(int l,int *mat);

 void add(int *tem1,int *tem2);

 void shift(int *k,int *l,int *j);

void print(int *tem5,int *tem6, int b);
/*A[4]={0,0,0,0},Q[4],M[4]={0,0,0,0},c[4]={0,0,0,1},C1[8]={0,0,0,0,0,0,0,1};*/

int main()

{

   int s,a,i,p,q,q1,m;

   int ans[8],A[4]={0,0,0,0},Q[4],Q1=0,M[4]={0,0,0,0},c[4]={0,0,0,1},tee[4],C1[8]={0,0,0,0,0,0,0,1},x,z,d;

   printf("\nEnter the two numbers\n ");

   scanf("%d%d",&q,&m);

   printf("\n");

   printf("Binary equivalent of 1st no is ");

   bin(q,Q);

   printf("\n");

   printf("Binary equivalent of 2nd no is ");

   bin(m,M);

   printf("\n\nOpr      A    Q    Q(-1)");

   printf("\n\nInit --- ") ;

   print(A,Q,Q1);



   i=0;

   while(i<4)

   {

    if((Q[3]==0)&&(Q1==1))

    {

    add(A,M);

    printf("\nA+M ---- ");

    print(A,Q,Q1);

    }

    if((Q[3]==1)&&(Q1==0))

    {

     for(p=0;p<4;p++)

     {tee[p]=M[p];

     tee[p]=1-tee[p];

     }

     add(tee,c);

     add(A,tee);

     printf("\nA-M ---- ");

     print(A,Q,Q1);

    }

    shift(A,Q,&Q1);

    i++;

    printf("\nShift -- ");

    print(A,Q,Q1);

   }





for(i=0;i< 4;i++)

ans[i]=A[i];

for(i=0;i< 4;i++)

ans[i+4]=Q[i];

if(((q< 0)&&(m>0))||((q>0)&&(m< 0)))

{

for(i=0;i< 8;i++)

ans[i]=1-ans[i];

for(i=7;i>=0;i--)

{

x = ans[i];

ans[i]=d^x^C1[i];

if(((d==1)&&(x==1))||((x==1)&&(C1[i]==1))||((C1[i]==1)&&(d==1)))

d=1;

else

d=0;

}

}

printf("\n\n");

for(i=0;i< 8;i++)

printf("%d",ans[i]);

s=0;z=0;

for(i=7;i>=0;i--)

{

s = s + (pow(2,z) * ans[i]);

z = z+1;

}

if(((q< 0)&&(m>0))||((q>0)&&(m< 0)))

printf("\nTHE ANSWER IN DECIMAL IS : -%d\n",s);

else

printf("\nTHE ANSWER IN DECIMAL IS : %d\n",s);



return 0;

}





void add(int *tem1,int *tem2)

{



 int c=0,i,p;

 for (i=3;i>=0;i--)

 {

 p= tem1[i];

 tem1[i]=p^c^tem2[i];

 if((p&&c) || (p&&tem2[i]) || (tem2[i]&&c))

 c=1;

 else c=0;

 }

}





void bin(int l,int *mat)

{

int i,c[4]={0,0,0,1};

mat[i]=0;

if(l>0)

{

for(i=3;i>=0;i--)

{

   mat[i]=l%2;

   l = l/2;

}

}

else{

l= -1*l;

for(i=3;i>=0;i--)

{

   mat[i]=l%2;

   l= l/2;

   mat[i]= 1-mat[i];

}

add(mat,c);

}

for(i=0;i<4;i++)

printf("%d",mat[i]);

}



void shift(int *tem3,int *tem4,int *j)

{

 *j=tem4[3];

 tem4[3]=tem4[2];

 tem4[2]=tem4[1];

 tem4[1]=tem4[0];

 tem4[0]=tem3[3];

 tem3[3]=tem3[2];

 tem3[2]=tem3[1];

 tem3[1]=tem3[0];

}



void print(int *tem5,int *tem6, int b)

{

 int i;

for(i=0;i<4;i++)

printf("%d",tem5[i]);

printf(" ");

for(i=0;i<4;i++)

printf("%d",tem6[i]);

printf(" ");

printf("%d",b);

}


for sum unknown reason..was not able to attach file:confused:


The program works perfectly fine in turbo C...not in linux tho...and in college we hv fedora machines.
 
Last edited:
OP
Kenshin

Kenshin

meowww meoww
no...i mean i complied with the -lm thing...the prob is when i run the file..THe program asks for the 2 numbers.after enterin the numbers the segmentation fault occurs
 

Faun

Wahahaha~!
Staff member
no...i mean i complied with the -lm thing...the prob is when i run the file..THe program asks for the 2 numbers.after enterin the numbers the segmentation fault occurs
compile with -g flag and then
debug it to get the correct location.

My net is down so cant do much, hope u can debug.
Steps
1) compile with $ gcc -Wall -g -o outputfile inputfile.c
2) start gdb session $ gdb outputfile core

More here *www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html
 

nileshgr

Wise Old Owl
I wrote a program for booth's algorithm for signed multiplication.Attached is the c code...I am gettin segmentation fault...whyy :confused:


kenshin@cutie:~$ ./a.out

Enter the two numbers
-3
7

Segmentation fault (core dumped)
kenshin@cutie:~$

wikipedia said:
A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. 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 (for example, attempting to write to a read-only location, or to overwrite part of the operating system). Systems based on processors like the Motorola 68000 tend to refer to these events as address or bus errors.
Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy.
On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.



Example

Here is an example of ANSI C code that should create a segmentation fault on platforms with memory protection:
const char *s = "hello world";
*s = 'H';
When the program containing this code is compiled, the string "hello world" is placed in the section of the program binary marked as read-only; when loaded, the operating system places it with other strings and constant data in a read-only segment of memory. When executed, a variable, s, is set to point to the string's location, and an attempt is made to write an H character through the variable into the memory, causing a segmentation fault. Compiling and running such a program on OpenBSD 4.0 produces the following runtime error:
$ gcc segfault.c -g -o segfault
$ ./segfault
Segmentation fault
Backtrace from gdb:
Program received signal SIGSEGV, Segmentation fault.
0x1c0005c2 in main () at segfault.c:6
6 *s = 'H';
In contrast, gcc 4.1.1 on GNU/Linux produces a compile-time error by default:
$ gcc segfault.c -g -o segfault
segfault.c: In function ‘main’:
segfault.c:4: error: assignment of read-only location
The conditions under which segmentation violations occur and how they manifest themselves are specific to an operating system.
Because a very common program error is a null pointer dereference (a read or write through the null pointer, a pointer to address 0, commonly used in C to mean "pointer to no object" or as an error indicator), most operating systems map the first page of memory (starting at address 0) so that accessing it causes a segmentation fault.
int* ptr = (int*) 0x00000000;


*ptr = 1;
This sample code creates a pointer to memory address 0x00000000, and tries to assign a value to it. Doing so causes a segmentation fault on many compilers.

SRC: *en.wikipedia.org/wiki/Segmentation_fault
 

fun2sh

Pawned!... Beyond GODLIKE
half of our class wil fail (wen results comes for 3rd sem) in the DATA Strucructer lab BECOZ OF THIS SEGMENTATION FAULT.

dont know wat the hell i the problem with linux.

me too had once got this error durin error n after seein i became extremely nervous n scared but i did some modification n that prog whic was ABSOLUTELY CORRECT PROG (WHICH WAS EXACTLY SAME AS THE ONE I HAD DONE ON MY LAPPY IN TC N GOT OUTPUT) TO GET RID OF the frightening SEGEMNTATION FAULT. n we use such a stupid compile n linux. it really wierd to point out the errors. for seg fault it even doesnt tell where has it occured. **** LINUX N ITS STUPID COMPILER WE R USIN. we use vi edit n "cc" to compile the prog. teachers r damn stupid n dont know why dont they move to a better alternative like Visual c++ :mad:
 

eggman

I have Yolks not Brains!
This segmentation faults had driven me mad!!!!!!!!!!!!!!The the worst thing is that it won't even point where's the problem. Just Segmentation Fault.Nothing else.

Even when you just type the ditto prog,thats in the book..this occurs, because of a simple mistake, which could've been pinpointed.
 

mehulved

18 Till I Die............
I took some help of a friend, this is what we did
1) Copied the code to a file named prog.c
2) Compiled it like this
Code:
gcc -Wall -o prog prog.c -lm -ggdb
3) Then we ran it through the debugger
Code:
gdb ./prog.c
(gdb) run
Starting program: /usr/home/mehul/prog

Enter the two numbers
 1
2


Program received signal SIGSEGV, Segmentation fault.
0x08048b6d in bin (l=1, mat=0xbfbfe740) at prog.c:208
208     mat[i]=0;
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /usr/home/mehul/prog

Enter the two numbers
 1 3


Program received signal SIGSEGV, Segmentation fault.
0x08048b6d in bin (l=1, mat=0xbfbfe740) at prog.c:208
208     mat[i]=0;
(gdb) bt
#0  0x08048b6d in bin (l=1, mat=0xbfbfe740) at prog.c:208
#1  0x08048687 in main () at prog.c:30

So, the problem is at line 208.
Here's what he had to say about it
he is trying to subscript a pointer..big no..afaik

BTW, where do you get the value of i on line 208?
As, I can see you've just declared i but you haven't defined it.
I tried by changing i to 100 and program worked fine.
I dunno how it worked in Turbo C.
 
Last edited:
you are using the subscript i without initializing it.

you have written
Code:
int i,c[4]={0,0,0,1};

mat[i]=0;

here i is defined and then used as subscript in the array without initialization. This is causing problem.
GCC is a very good compiler and as a student you should use it instead of more 'forgiving' compilers which let you make mistakes that cause problems in long run. I have myself seen borland compiler missing many memory leak scenarios which are reported in advance in GCC. If you want to learn C or C++ by Standards, GCC is one of the best compilers to have.
 

mehulved

18 Till I Die............
And BTW, FYI, you have 2 unused variables
Code:
prog.c:18: warning: unused variable `a'
prog.c:18: warning: unused variable `q1'
 

mehulved

18 Till I Die............
It worked for me
Just add this line before line no. 208
Code:
for(i=3;i>=0;i++)

You're right your teachers are really stupid to make you use gcc, you should rather stick to cosy old tc++, it makes programming so easy. gcc is just a piece of crap.
 
F

FilledVoid

Guest
Sarcasm as its best ^ . Most people think gcc sucks and are happy with TC. Just wait till your job uses anything BUT TC.
 

praka123

left this forum longback
^no, blame Linux also as "he" does @#8 !that will be even more sweeter as Linux contains gcc :rolleyes: all fault of gcc and Linux.

yeah using vim/vi editor is a very big sin :rolleyes: sheesh..where these fellas are going?
 

QwertyManiac

Commander in Chief
What was the need to drag Linux into this topic? Its a compiler-specific and helpful output, not Linux, nor Windows and not any other "Platform".

If you don't know to code with pointers, please avoid doing so. Applies to you eggman. Its even better if you fail, for not understanding what you code. Atleast after that it'd kick some sense into you about 'proper programming'.

So you say that you have never encountered a segmentation fault in Windows? Thats cause the programs compiled on your stupid old compiler don't tell you what's wrong, they just cease to function and crash midway. Good ol' BSOD eh? Prefer that, really, its worth your doom.

If you wish to change your ways, use GNU Debugger along with GCC. It wasn't invented for nothing. Nothing is.

Over that, I don't know what good would using a non-conformal compiler like VC++ 03/05 do to an eventually bad code of yours. Have you ever tried assembly? It makes you think better, try that first.
 
Last edited:

vish786

"The Gentleman"
If you don't know to code with pointers, please avoid doing so. Applies to you eggman. Its even better if you fail, for not understanding what you code. Atleast after that it'd kick some sense into you about 'proper programming'.

@others, well thats reason most dont tend to use pointers !
 

mehulved

18 Till I Die............
If you don't know to code with pointers, please avoid doing so. Applies to you eggman. Its even better if you fail, for not understanding what you code. Atleast after that it'd kick some sense into you about 'proper programming'.
Not everyone is a good programmer like you :D
 
@mehulved : Well not everyone is a good programmer, but why is he studying programming then, to always be a 'not good programmer", huh?

In my opinion, old TC is not good enough even for beginners due to its forgiving nature. It causes you to 'learn' doing mistakes that cause havoc in big, real world programs. If gcc is a crap, then Linux and whole of its software repository is crap, which it is not , that even a noob can tell.

Pointer arithmetic is an important topic in C and C++ and no great programs can be made without learning it properly. Uninitialized variables before use, null pointers etc are so big of a problem that all the new programming languages are coming with strong checking for these troublesome chunks of code. are they all crap???
 
Status
Not open for further replies.
Top Bottom