views:

38

answers:

1
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
char arr[200],res[200];
char table[150][200];

string multiply(char n[],int m)
{
    int N=strlen(n),M,temp=0,x=0;
    for(int i=0;i<N;i++)
       arr[i]=n[N-1-i];
    for(int i=0;i<N;i++)
    {
        x=m*(arr[i]-'0')+temp;
        x=m*(arr[i]-'0')+temp;
        arr[i]=(x%10)+'0';
        temp=x/10;
    }
    while(temp>0)
    {
        arr[N]=(temp%10)+'0';
        temp/=10;
        N++;
    }
    M=strlen(arr);
    for(int i=0;i<M;i++)
       res[i]=arr[M-1-i];
}
void make_table()
{
    table[0][0]='1';
    for(int i=1;i<101;i++)
    {
        multiply(table[i-1],i);
        int u=strlen(res);
        for(int j=0;j<u;j++)
        {
            table[i][j]=res[j];
        }
    }
}
int main()
{
    int tc,n;
    scanf(" %d",&tc);
    make_table();
    while(tc--)
    {
        scanf(" %d",&n);
        printf("%s\n",&table[n]);
    }
    return 0;
}

That's my code for this problem : http://www.spoj.pl/problems/FCTRL2/ It generates correct answers for me but when i submit it , it tells me Runtime error(segmentation fault) . Can anyone explain to me what is the segmentation fault? cause i read it on the spoj website and i didn't understand how to avoid it and how to upgrade my code?

+2  A: 

If you replace the return type of the function multiply from string to void the segfault is gone.

A segmentation fault happens when you try to read/write memory you don't have access to. For instance you can try writing on read only memory, or reading at address 0x00000000. A common way to achieve segfaults is by using an uninitialized pointer.

A debugger is often a good help to find a segmentation fault, as it will stop and show you where is happened.

f4
Well my bad ... Thanks :D it worked
magiix
if you know what else could generate a seg fault , can you enlighten me ?
magiix
Thanks ........
magiix