내가 컴 플라이하면C/linked list/세분화 오류를 어떻게 해결할 수 있습니까?
strcat(arr, cur->texts);
에있는 세그먼트 오류가 dumpTB
기능에 있습니다. 주요 기능에서
의 dumpTB
함수가 호출 될 때, 그것이 내가 newTB
기능에 입력 한 것과 동일한
hello\ngood bye\nworld\n
처럼 인쇄해야합니다 ..
사람은 문제가 무엇인지 알아낼 수 있습니까?
저는 deleteTB (TB tb, int, int에서)라는 함수를 추가했습니다. 나는 '너는 나를 위해 이걸 할 수 있니?'하고 묻는 것뿐만 아니라, 나는 알고 있고 고치는 법을 배우고 싶다. 어젯밤부터 시도했지만 .. 여전히 붙어 ..
이상하게도, 같은 줄 'strcat (arr, cur-> texts)'에서 seg 오류가 발생했습니다. 다른 방식으로 코드를 다르게 수정하려고 시도했지만 알지 못합니다. 내 입력
것은 deleteTB (목록, 0, 1)과 같이 호출된다 .. "nworld \ 없음 \ 바이 ngood \ 안녕하세요"등이기 때문에// (헤드 노드 0)
같은 인쇄해야printbuffer (목록),
POS 0 : 세계
다음, dumpTB (목록)과 같은 인쇄해야합니다,세계.
내가 여기 규칙을 알지 못했기 때문에 나는 어젯밤에 거의 같은 일을 게시하여 사람들을 괴롭혔습니다. 그 죄송합니다. 그리고 나는 당신들에게 그것을하도록 요구하는 것이 아닙니다. 나는 정말로 배우고 싶다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "textbuffer.h"
#define MAX_TEXT 256
struct textbuffer {
char *texts;
int count;
TB next;
};
TB newTB (char text[]){
TB newText = malloc(sizeof(struct textbuffer));
char *cpy = (char *)malloc(MAX_TEXT * sizeof(char));
TB head = newText; // Store the first node to return
strcpy(cpy,text);
newText->count = 0;
newText->next = NULL;
int i = 0;
int j = 0;
while(cpy[i] != '\0') {
if(j == 0) {
head->texts = (char *)malloc(MAX_TEXT * sizeof(char));
}
if(cpy[i] == '\n') {
head->texts[j] = '\0';
newText->count++;
head->next = malloc(sizeof(struct textbuffer));
head = head->next;
j = 0;
i++;
} else {
head->texts[j++] = cpy[i++];
}
}
head->next = NULL;
return newText;
}
void releaseTB (TB tb) {
TB head = tb;
TB tmp;
while(head != NULL) {
tmp = head;
head = head->next;
free(tmp->texts);
free(tmp);
}
}
char *dumpTB (TB tb) {
if(tb == NULL) {
return NULL;
}
TB cur = tb;
char *arr = (char *)malloc(MAX_TEXT * sizeof(char));
while(cur != NULL) {
if(arr == NULL) {
strcpy(arr,"");
}
strcat(arr, cur->texts);
if(cur->next != NULL) {
strcat(arr, "\n");
}
cur = cur->next;
}
return (arr);
}
int linesTB(TB tb) {
return (tb->count);
}
void printBuffer(TB tb){
TB curr = tb;
int i=0;
while(curr->next != NULL){
printf("POS %d : %s\n", i++, curr->texts);
curr = curr->next;
}
}
void swapTB(TB tb, int pos1, int pos2) {
if((pos1 < 0) || (pos2 < 0) || (pos1 > linesTB(tb)-1) || (pos2 > linesTB(tb)-1)) {
printf("**GIVEN LINES ARE OUT OF RANGE**\n");
abort();
}
TB cur = tb;
TB head = tb;
int i = 0;
char *tmp = (char *)malloc(MAX_TEXT * sizeof(char));
tb->texts = cur->texts;
while(i < pos1) {
cur = cur->next;
i++;
}
strcpy(tmp, cur->texts);
cur->texts = NULL;
i=0;
while(i < pos2) {
head = head->next;
i++;
}
cur->texts = head->texts;
head->texts = tmp;
}
void deleteTB(TB tb, int from, int to) {
if((from < 0) || (to < 0) || (from > linesTB(tb)-1) || (to > linesTB(tb)-1)) {
printf("**GIVEN LINES ARE OUT OF RANGE**\n");
abort();
}
TB cur = tb;
int i = 0;
for(i = 0; i < from; i++) {
cur = cur->next;
}
while(i <= to) {
cur->texts = '\0';
free(cur->texts);
//free(cur);
cur = cur->next;
i++;
}
}
int main(int argc, char * argv[]) {
TB list = NULL;
list = newTB("hello\ngood bye\nworld\n");
printf("**THERE ARE %d LINES IN TEXTBUFFER**\n", linesTB(list));
printBuffer(list);
printf("**Dumping test**\n");
printf("%s\n",dumpTB(list));
printf("**Swapping test**\n");
swapTB(list, 0, 1);
printBuffer(list);
printf("**Deleteing test**\n");
deleteTB(list, 1, 1);
printBuffer(list);
printf("%s\n",dumpTB(list));
releaseTB(list);
return 0;
}
는 디버거가 즉시 실행합니다. –
결핵이란 무엇입니까? 그리고 [malloc의 반환을 캐스팅하지 마십시오] (http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc).또한'strdup()'를 사용하십시오 ... – Eregrith
dumpTB 함수에서 arr에 strdup()를 사용 하시겠습니까? –