Ich habe diese Ausnahme durch Ausführen von LLDB für mein Programm.Wie lldb zum Debuggen der Segmentierung Fehler
Die Ausnahme gab mir jedoch keine konkreten Hinweise für das Finden der Grundursache.
Natürlich, basierend auf Erfahrung, könnte ich den Speicher nicht richtig zugreifen.
Gibt es eine Idee, die Ursache von LLDB zu finden?
Dankcontinue 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.
Code
#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;
}
Hallo, vielen Dank für diesen guten Punkt. Das Problem der Segmentierung wurde behoben. Gibt es dennoch eine Möglichkeit, dieses Problem wahrscheinlicher zu finden? es scheint, dass Sie Ihre Erfahrung verwenden, um es zu beheben, anstatt die Hilfe von Debugger oder Ausnahmebedingungsnachricht ^^ – newBike
Der Compiler gibt eine Warnung aus. –
Tatsächlich gibt der MSVC-Compiler * zwei * Warnungen aus: eine über den Parametertyp und eine über den Grad der Indirektion. –