我需要做一个功能,以接收一个无限的输入,这将停止阅读时,达到和EOF。我必须使用realloc
函数。
这是我目前的代码:
#include <stdio.h>
#include <stdlib.h>
char *getInput() {
char *currentString, currentChar;
int currentSize;
currentString = (char *)malloc(sizeof(char) * 2);
currentSize = 0;
currentChar = 0;
if (currentString != NULL) {
while (currentChar != EOF) {
currentChar = getchar();
currentString[currentSize++] = currentChar;
currentString = realloc(currentString, currentSize);
}
}
return currentString;
}
int main(int argc, char *argv[]) {
char *userInput;
userInput = getInput();
printf("\n string is: %s", userInput);
return 0;
}
代码正常工作,除非我输入的字符串大小超过13个字符,否则会出现以下错误(文件名为list_ab):
*** Error in `./list_ab': corrupted size vs. prev_size: 0x08ff6010 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x67377)[0xb75a7377]
/lib/i386-linux-gnu/libc.so.6(+0x6d2f7)[0xb75ad2f7]
/lib/i386-linux-gnu/libc.so.6(+0x7049e)[0xb75b049e]
/lib/i386-linux-gnu/libc.so.6(realloc+0x10e)[0xb75b162e]
./list_ab[0x804851d]
./list_ab[0x8048544]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf7)[0xb7558637]
./list_ab[0x80483d1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:02 359220 /home/user/Desktop/Maman12/list_ab
08049000-0804a000 r--p 00000000 08:02 359220 /home/user/Desktop/Maman12/list_ab
0804a000-0804b000 rw-p 00001000 08:02 359220 /home/user/Desktop/Maman12/list_ab
08ff6000-09017000 rw-p 00000000 00:00 0 [heap]
b7400000-b7421000 rw-p 00000000 00:00 0
b7421000-b7500000 ---p 00000000 00:00 0
b750d000-b7529000 r-xp 00000000 08:02 931606 /lib/i386-linux-gnu/libgcc_s.so.1
b7529000-b752a000 rw-p 0001b000 08:02 931606 /lib/i386-linux-gnu/libgcc_s.so.1
b7540000-b76f0000 r-xp 00000000 08:02 933356 /lib/i386-linux-gnu/libc-2.23.so
b76f0000-b76f2000 r--p 001af000 08:02 933356 /lib/i386-linux-gnu/libc-2.23.so
b76f2000-b76f3000 rw-p 001b1000 08:02 933356 /lib/i386-linux-gnu/libc-2.23.so
b76f3000-b76f6000 rw-p 00000000 00:00 0
b770b000-b770e000 rw-p 00000000 00:00 0
b770e000-b7710000 r--p 00000000 00:00 0 [vvar]
b7710000-b7711000 r-xp 00000000 00:00 0 [vdso]
b7711000-b7733000 r-xp 00000000 08:02 931813 /lib/i386-linux-gnu/ld-2.23.so
b7733000-b7734000 rw-p 00000000 00:00 0
b7734000-b7735000 r--p 00022000 08:02 931813 /lib/i386-linux-gnu/ld-2.23.so
b7735000-b7736000 rw-p 00023000 08:02 931813 /lib/i386-linux-gnu/ld-2.23.so
bfd35000-bfd56000 rw-p 00000000 00:00 0 [stack]
Aborted (core dumped)
在大量搜索之后,我发现每当我试图写到一个无法访问的位置时,错误就会发生,但是上面提到的修复都没有效果。我怎样才能解决这个问题?如果它有助于在linux上运行。
发布于 2022-04-24 17:01:19
代码中存在多个问题:
(-1)
).类型currentChar
的类型应该是int
,以适应getchar()
返回的所有可能的值,即unsigned char
类型的所有值(8位字节的0
到255
)和特殊的负值EOF
(通常定义为getchar()
)。
currentChar != EOF
时,变量currentChar
未初始化,导致未定义的行为。之前测试EOF
。
malloc()
或realloc()
失败。main()
.中没有释放字符串的
以下是修改后的版本:
#include <stdio.h>
#include <stdlib.h>
char *getInput(void) {
size_t len = 0;
size_t size = 8;
char *str = malloc(size);
char *newstr;
int c;
if (str == NULL) {
fprintf(stderr, "malloc failure for %zu bytes\n", size);
return NULL;
}
while ((c = getchar()) != EOF) {
if (len + 2 > size) {
/* increase the allocated block size by 50% */
size += size / 2;
newstr = realloc(str, size);
if (newstr == NULL) {
free(str);
fprintf(stderr, "realloc failure for %zu bytes\n", size);
return NULL;
}
str = newstr;
}
str[len++] = c;
}
/* try and reduce the size of the allocated string */
newstr = realloc(str, len + 1);
if (newstr != NULL)
str = newstr;
/* set the null terminator */
str[len] = '\0';
return str;
}
int main() {
char *userInput = getInput();
if (userInput) {
printf("string is: %s\n", userInput);
free(userInput);
}
return 0;
}
发布于 2022-04-24 16:58:53
总结评论中的所有意见:
不使用
argc
& argv
,那么为什么声明them?getchar()
返回一个int
来覆盖通常设置为1
的所有位在2的补码表示法中成为-1
。因此,需要为int
.currentChar
不是null终止的,在使用后作为以nul结束的内存使用时可能会导致缓冲区溢出。str
中为nul
('\0')留出空间&检查realloc()
.简化代码:
#include <stdio.h>
#include <stdlib.h>
char* getInput() {
char *str = malloc (sizeof (char) * 2);
if (!str) {
perror ("malloc"); return NULL;
}
int ich;
int slen = 0;
while ((ich = getchar()) != EOF) {
str[slen++] = ich;
char* str2 = realloc (str, slen + 1);
if (!str2) {
perror ("realloc"); free (str); return NULL;
}
str = str2;
}
str[slen] = '\0';
return str;
}
int main() {
char *userInput = getInput();
if (userInput) {
printf ("String is: %s\n", userInput);
free (userInput);
}
return 0;
}
https://stackoverflow.com/questions/71990243
复制相似问题