线程:信号SIGABRT(lldb),程序以退出代码-1结束

时间:2014-11-27 22:47:43

标签: c xcode multithreading printf sigabrt

程序停止,线程出现在sprint行中:

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

这不是将int(x2)转换为字符串(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 个答案:

答案 0 :(得分:0)

如果您发布了实际编译的编译行+代码(即包含了正确的标题!),那将会更有用。移动到数组大小为10这可以正常工作(虽然需要一段时间才能退出!):

修改后的代码:

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; }

编译和链接:

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

调试:

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

世界上一切都很好! :d