이 세분화 오류 11 오류의 출처를 찾으려고 시간을 보내고 있습니다. 여러분이 저를 도울 수 있다면 바라고 있습니다.내 프로그램에서 segfault를 찾지 못했습니다.
그래서 프로그램의 목적은 입력 문자열에서 리디렉션을 찾고 리다이렉션 방향 인 fileName과 argsWithoutFile을 제공하는 redirCode 구조체를 반환하는 것입니다.
예 입력 :
"ls -a > test.txt"
가와 REDIR 코드를 반환
argsWithoutFile = "ls -a"
fileName = "test.txt"
code = 2 //stdout
나는 독방 감금 오류가 하위 문자열을하려고에서 오는 확신 해요. 왜냐하면 내가 마지막 부분 문자열을 주석 처리 할 때 segfault를주지 않기 때문이다. 하위 문자열을 제외하고 모두 괜찮은 것 같습니다.
char *input = "ls -a > test.txt'
char *desiredSubString = "ls -a "
다음
는 전체 코드입니다 : : 기본적으로 문자열에 대한 I는이처럼되고 싶어 일부 터미널 로그는 여기에있다
#include <stdio.h>
#include <string.h>
#define BUFFER 1024
struct redirCode findRedirects(char *input);
struct redirCode {
/* For code:
* 0 = none
* 1 = stdin
* 2 = stdout
*/
int code;
char *argsWithoutFile;
char *fileName;
};
int main(){
const char *delims = "<>";
struct redirCode temp;
char line[BUFFER];
printf("Input: ");
fgets(line, 1024, stdin);
temp = findRedirects(line);
printf("temp:\n");
printf("temp.code = %d\n", temp.code);
printf("temp.fileName = %s\n", temp.fileName);
printf("temp.argsWithoutFile = %s\n", temp.argsWithoutFile);
}
/* Looks for '>', '<' in a string.
* Will destroy string *input
* Returns a redirCode struct with:
* 1. fileName - the name of file that
* wants to be redirected/stdin
* 2. code - the direction of redirect
* 3. args - the arguments w/o filename
* */
struct redirCode findRedirects(char *input)
{
const char *delims = "<>";
struct redirCode redirToReturn;
//Do an initial search for the delimeters
//before strtok destroys it. O(n) time.
int redirectOperatorReached = 0;
int count = 0;
int i;
for (i = 0; input[i] != 0; i++){
if (input[i] == '<'){
redirToReturn.code = 1;
redirectOperatorReached = 1;
}
else if (input[i] == '>'){
redirToReturn.code = 2;
redirectOperatorReached = 1;
}
else {
redirToReturn.code = 0;
}
if (redirectOperatorReached != 1){
count++;
}
}
printf("sizeof(input) = %lu\n", sizeof(input));
printf("count = %d\n", count);
strncpy(redirToReturn.argsWithoutFile, input, count);
printf("input = %s\n", input);
redirToReturn.argsWithoutFile[count] = '\0';
printf("argsW/oFile = %s\n", redirToReturn.argsWithoutFile);
return redirToReturn;
}
그리고
MacBook-Air:practice keithy$ cc strtokOnlyOnce.c
MacBook-Air:practice keithy$ ./a.out
Input: hi
sizeof(input) = 8
count = 3
Segmentation fault: 11
MacBook-Air:practice keithy$ cc strtokOnlyOnce.c
MacBook-Air:practice keithy$ ./a.out
Input: ls -a > test.txt
sizeof(input) = 8
count = 6
Segmentation fault: 11
MacBook-Air:practice keithy$
편집 : 작동하도록했습니다! 내가해야 할 일은 redirCode의 문자열을 malloc하는 것이 었습니다. 여기에 작업 코드 :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define BUFFER 1024
struct redirCode findRedirects(char *input);
struct redirCode {
/* For code:
* 0 = none
* 1 = stdin
* 2 = stdout
*/
int code;
char *argsWithoutFile;
char *fileName;
};
int main(){
const char *delims = "<>";
struct redirCode temp;
char line[BUFFER];
printf("Input: ");
fgets(line, 1024, stdin);
temp = findRedirects(line);
printf("temp.code = %d\n", temp.code);
printf("temp.fileName = %s\n", temp.fileName);
printf("temp.argsWithoutFile = %s\n", temp.argsWithoutFile);
}
/* Looks for '>', '<' in a string.
* Will destroy string *input
* Returns a redirCode struct with:
* 1. fileName - the name of file that
* wants to be redirected/stdin
* 2. code - the direction of redirect
* 3. args - the arguments w/o filename
* */
struct redirCode findRedirects(char *input)
{
const char *delims = "<>";
struct redirCode *redirToReturn = malloc(sizeof(struct redirCode));
//Do an initial search for the delimeters
//before strtok destroys it. O(n) time.
int redirectOperatorReached = 0;
int count = 0;
int i;
for (i = 0; input[i] != 0; i++){
if (input[i] == '<'){
redirToReturn->code = 1;
redirectOperatorReached = 1;
input[i] = ' ';
}
else if (input[i] == '>'){
redirToReturn->code = 2;
redirectOperatorReached = 1;
input[i] = ' ';
}
if (redirectOperatorReached != 1){
count++;
}
}
int lengthOfInput = strlen(input);
int sizeOfMalloc = (lengthOfInput+1)*sizeof(char);
redirToReturn->argsWithoutFile = (char *) malloc(sizeOfMalloc);
redirToReturn->fileName = (char *) malloc(sizeOfMalloc);
strncpy(redirToReturn->argsWithoutFile, input, count);
redirToReturn->argsWithoutFile[count] = '\0';
strncpy(redirToReturn->fileName, input + count, lengthOfInput - count);
return *redirToReturn;
}
/*OUTPUT
*./a.out
*Input: ls -a > test.txt
*temp.code = 2
*temp.fileName = test.txt
*temp.argsWithoutFile = ls -a
*/
당신이 디버거를 사용하여 시도 적이처럼
당신은 할 수 있습니까? 프로그램과 함께 디버거를 실행하면 프로그램에서 segfault가 발생한 지점을 정확하게 알려줍니다. – Frxstrem
argsWithoutFile이 NULL입니다 (음, 임의 값으로 초기화 됨). 먼저 버퍼를 할당 한 다음 strncpy에 사용할 수 있습니다. –
printf 디버깅을 잘 해봤지만 다음 단계로 넘어 가야합니다. 출력은'strncpy (redirToReturn.argsWithoutFile, input, count); '를 문제 행으로 식별합니다. 그래서, 거기 더 깊게 파십시오. 이 특정 프로그램의 문제점을 이해하는 것이 큰 문제는 아닙니다. 이것은 나머지 시간 동안 당신에게 도움이 될 수있는 디버깅 기술을 배울 수있는 기회입니다. –