如何使用lldb调试分段错误

时间:2016-10-16 19:44:14

标签: c lldb

我通过为我的程序运行LLDB来获得此异常。

然而,这个例外并没有给我任何找到根本原因的具体线索。

当然,根据经验,我可能无法正确访问内存。

有什么想法找到LLDB的根本原因?感谢

continue to play (y/n)? y
Process 30020 stopped
* thread #1: tid = 0x24b72, 0x00007fff893c8365 libsystem_platform.dylib`_platform_strncmp + 325, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x79)
    frame #0: 0x00007fff893c8365 libsystem_platform.dylib`_platform_strncmp + 325
libsystem_platform.dylib`_platform_strncmp:
->  0x7fff893c8365 <+325>: movzbq (%rsi,%rcx), %r8
    0x7fff893c836a <+330>: subq   %r8, %rax
    0x7fff893c836d <+333>: jne    0x7fff893c837d            ; <+349>
    0x7fff893c836f <+335>: testq  %r8, %r8
(lldb)
error: No auto repeat.

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

const int USER_INPUT_SIZE_EACH_LINE = 999;

void draw_middle_lines(char *middle_line, int width) {
    printf("%s", middle_line);
    for (int i = 2; i < width; i++) {
        printf(" ");
    }
    printf("%s\n", middle_line);
}

void draw_bound_line(char *bound_char, int width) {
    for (int i = 0; i < width; i++) {
        printf("%s", bound_char);
    }
    printf("\n");
}


void clearNewline(char *line, int max_len) {
    for (int i = 0; i < max_len; i++) {
        if (line[i] == '\n') {
            line[i] = '\0';
        }
    }
}

char **ask_user_preference() {
    const int NUM_OF_PROMPTS = 4;
    char *prompt[] = {
            "please input height:",
            "please input width:",
            "please input horizontal char:",
            "please input vertical char:",
    };
    char **preference;
    preference = malloc(NUM_OF_PROMPTS);
    for (int i = 0; i < NUM_OF_PROMPTS; i++) {
        preference[i] = malloc(USER_INPUT_SIZE_EACH_LINE);
        memset(preference[i], 0, sizeof(char) * USER_INPUT_SIZE_EACH_LINE);
        printf("%s", prompt[i]);
        fgets(preference[i], USER_INPUT_SIZE_EACH_LINE, stdin);
        clearNewline(preference[i], USER_INPUT_SIZE_EACH_LINE);
    }
    return preference;
}

void draw(int w, int h, char *bound_char, char *middle_char) {
    draw_bound_line(bound_char, w);
    for (int i = 2; i < h; i++) {
        draw_middle_lines(middle_char, w);
    }
    draw_bound_line(bound_char, w);
}

void clr_input_buffer() {
    for (;;) {
        int c = getchar();
        if (c == EOF || c == '\n')
            break;
    }

}

int main() {
    char *cont;
    do {
        char **pref = ask_user_preference();
        int height = atoi(pref[0]);
        int width = atoi(pref[1]);
        char *bound_char = pref[2];
        char *middle_char = pref[3];
        draw(width, height, bound_char, middle_char);
        printf("continue to play (y/n)? ");
        fgets(cont, 10, stdin);
        if (strncmp(cont, 'y', 1) != 0) {
            break;
        }
    } while (1);
    printf("BYE BYE~");
    return 0;
}

1 个答案:

答案 0 :(得分:0)

函数调用

strncmp(cont, 'y', 1)

可能会导致段错误,因为'y'是作为内存位置传递的字符值。我建议

strncmp(cont, "y", 1)

虽然我不确定你的比较感。无论如何,我建议这会更好

toupper(cont[0]) == 'Y'

我将比较感留给你。最后请启用所有可能发现此问题的编译器警告。