#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);
}
compile with -g flag and thenno...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
I wrote a program for booth's algorithm for signed multiplication.Attached is the c code...I am gettin segmentation fault...whyy
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";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:
*s = 'H';
$ 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;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.
*ptr = 1;
gcc -Wall -o prog prog.c -lm -ggdb
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
he is trying to subscript a pointer..big no..afaik
int i,c[4]={0,0,0,1};
mat[i]=0;
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 youIf 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'.