거대한 stdin이라는 단어가 가득 찬 프로그램을 작성하고 있습니다. 입력을 100 자까지의 문자열로 나누고 싶습니다. 그래서 여기에 내 코드가있다.realloc의 주소가 잘못되었습니다.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static char* string = "\0";
void getChar(char new){
if (strcmp(string,"\0") == 0){
free(string);
string = (char *) malloc(sizeof(char));
if (string == NULL){
exit(EXIT_FAILURE);
}
string[0] = new;
} else {
char* newString = (char*) realloc(string, sizeof(string)+sizeof(char));
if (newString == NULL){
exit(EXIT_FAILURE);
}
string = newString;
string[strlen(string)]=new;
}
if (strlen(string) > 100){
printf("%s\n",string);
dosomething(string);
string = "\0";
}
}
void getInput(){
int temp;
while((temp = fgetc(stdin)) != EOF){
getChar((char) temp);
}
}
int main(int argc, char *argv[]){
getInput();
}
내가 emmediately 말하는 오류가 컴파일하고 코드를 실행 한 후 : 이후 버전에서
*** glibc detected *** ./sort: realloc(): invalid next size: 0x08f02008 //ofc this address always changes
, 내가 문자열이 100 개보다 큰 문자가 ingnored되고있는와 n \에 의해 필터링됩니다. 이 가리키는 것은 아닌 길이 (즉, string
이 무엇인지 이후, 포인터)
제안 : C 프로그램에서'new'와 같은 C++ 키워드를 피하십시오. –