Thread: signal SIGABRT (lldb) and program ended with exit code -1

1.3k Views Asked by At

Program stops and the thread appears in the line of sprint:

sprintf(x,"%d",x2);

Isn't this the best way to turn an int (x2) to a string(x)??

int check(int n,char [8]);

int main(){
    char x[8]="0";
     int N,x2;

    scanf("%d",&N);
    while(strlen(x)<9){
        if(check(N,x)) printf("%s\n",x);
        x2=atoi(x);
        x2++;
        sprintf(x,"%d",x2);
        }
    return 0;
    }


int check(int n,char x[8]){
    int l,i,y,count;
    l=strlen(x);
    y=atoi(x);
    count=0;
    for(i=0;i<l;i++){
        count=count+pow((x[i]-'0'),n);
    }
    if(count==y) return 1;
    else return 0; }
1

There are 1 best solutions below

1
On BEST ANSWER

It would be more useful if you'd have posted your compile line + code that actually compiled (i.e. had the correct headers included!). With the move to an array size of 10 this works fine (although takes a while to exit!):

The modified code:

C:\...>cat main.c

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

int check(int n,char [10]);

int main(){
    char x[10]="0";
     int N,x2;

    scanf("%d",&N);
    while(strlen(x)<9){
        if(check(N,x)) printf("%s\n",x);
        x2=atoi(x);
        x2++;
        sprintf(x,"%d",x2);
        }
    return 0;
    }


int check(int n,char x[10]){
    int l,i,y,count;
    l=strlen(x);
    y=atoi(x);
    count=0;
    for(i=0;i<l;i++){
        count=count+pow((x[i]-'0'),n);
    }
    if(count==y) return 1;
    else return 0; }

Compile and link:

C:\...>gcc -Wall -g main.c

Debug:

C:\...>gdb a.exe
GNU gdb (GDB) 7.6.2
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from C:\...\a.exe...done.
(gdb) run
Starting program: C:\.../a.exe
[New Thread 8396.0x199c]
3
0
1
153
370
371
407
[Inferior 1 (process 8396) exited normally]
(gdb) quit

All is good with the world! :D