내 프로그램에서 세그먼트 화를 생성하는 곳을 식별하지 못했습니다. 문자열 작업 중 또는 문자열 포인터가 런타임 중에 세그먼트 오류를 일으키는 것을 가리키는 데 도움이 필요합니다. 프로그램이 성공적으로 컴파일되지만 런타임에 세그먼트 화 오류가 발생합니다.내 C 코드의 세그먼트 화 오류에 도움이 필요합니다
#include<curses.h>
#include<strings.h>
#include<unistd.h>
#include<stdlib.h>
/*Implements a Scrolling headband that takes a string argument and continously scrolls*/
int main(int argc, char* argv[])
{
/*A Substring function to return substring of a string*/
char *substr(const char *src, int start, int len);
/*Appends a character to the Given string*/
void append(char* s, char c);
/***Check if the argument is invalid***/
if(argc!=2)
{
puts("Invalid number of arguments: Usage:headband <String>");
}
/**Get headtext from the string argument argv[1]**/
char *headtext = argv[1];
/*headband(headtext);*/
/*temporary variable to store strings as execution progresses*/
char temp[100];
/*Counter for streaming and scolling headband text*/
int count = 0;
/*Placeholder for temporary headband text*/
char hold[100];
int i;
/*maximum x and y co-ordinates of the Screen*/
int max_x,max_y;
/*Initialize screen for ncurses*/
initscr();
/*Don't show cursor*/
curs_set(0);
/*Get terminal console dimensions*/
getmaxyx(stdscr, max_y, max_x);
/*Get console width set to default console screen width=80*/
int consolewidth = max_x;
/*Clear the screen*/
clear();
/*Set the first value as end of String for the momment*/
temp[0] = '\0';
/*Run this loop continuously to keep scrolling*/
for (;;)
{
for(i=0; i < strlen(headtext); i++)
{
count++;
/*Append headband text character by character to string hold*/
append(temp, headtext[i]);
move(0,consolewidth - count);
if (consolewidth - count > 0)
{
mvaddstr(0,console_width-count,temp);
refresh();
}
else if (consolewidth - count == 0)
{
strcpy(hold, temp);
char q;
int com = i;
for(;;)
{
/*Scroll text by triming one character at a time*/
/*from the left, then adding that character to the*/
/*right side of the text*/
com = com + 1;
if (com < strlen(headtext))
{
q = headtext[com];
}
else
{
com = 0;
q = headtext[com];
//q = hold[0];
}
strcpy(hold, substr(hold, 1, strlen(hold) - 1));
append(hold, q);
move(0,0);
clear();
mvaddstr(0,0,hold);
refresh();
usleep(50);
}
}
usleep(50);
}
}
return 0;
}
/*A Substring function to return substring of a string*/
char * substr(const char *src, int start, int len)
{
char *dest = malloc(len+1);
if (dest)
{
memcpy(dest, src+start, len);
dest[len] = '\0';
}
return dest;
}
/*Appends a character to the Given string*/
void append(char s[], char c)
{
int len = strlen(s);
s[len] = c;
s[len+1] = '\0';
}
Linux 환경에 있다고 가정 할 때 GDB를 사용하여 디버깅해야합니다. 충돌이 발생하는 정확한 행에서 중지되므로 변수 값을 검사 할 수 있습니다. – SirDarius
아마도 디버거가 맞습니까? –
아마도 문제는 아니지만 첫 번째 댓글을 종료하지 않았습니다. 컴파일러가 혼란 스러우면 아무 일도 일어나지 않을 것입니다. – Dave