c - 在 C 中使用 getchar() 读取输入时是否需要分配内存?
问题描述
在下面的代码中,是否应该c
使用函数为指针分配内存malloc()
?我担心递增c
可能会导致它指向另一个变量,因此在*c = getchar()
被调用时会覆盖它。
char *c;
int count = 0;
while( (*c=getchar()) != '\n' ){
c++;
count++;
}
解决方案
贴出的代码有问题:
c
未初始化,写入它会立即产生未定义的行为,增加它只会使情况变得更糟。- 您不测试文件结尾,也不测试任何数组边界,因此即使
c
指向实际数组、静态、自动或动态从堆分配的malloc()
,您也必须检查是否c
保持在边界内那个数组。
这是一个更正的版本:
#include <stdio.h>
int main() {
char buf[100];
int c, count, limit;
char *p;
p = buf; /* p points to an automatic array, no malloc needed */
count = 0;
limit = sizeof(buf) - 1; /* maximum number of characters to store */
while ((c = getchar()) != EOF && c != '\n') {
if (count < limit)
*p++ = c;
count++;
}
if (count < limit)
*p = '\0';
else
buf[limit] = '\0';
printf("%s\n", buf);
return 0;
}
这是一个内存分配:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *buf, *p;
int c, count, limit;
limit = 99;
p = buf = malloc(limit + 1); /* p points to an array allocated from the heap */
count = 0;
if (buf == NULL) {
printf("allocation failure\n");
return 1;
}
while ((c = getchar()) != EOF && c != '\n') {
if (count < limit)
*p++ = c;
count++;
}
if (count < limit)
*p = '\0';
else
buf[limit] = '\0';
printf("%s\n", buf);
free(buf);
return 0;
}
笔记:
while ((c = getchar()) != EOF && c != '\n')
是一个经典的 C 习惯用法,从标准输入中读取一个字节,并将其存储在一个int
变量c
中,检查 end if 文件并检查单个控制表达式中的行尾。&&
首先评估其左侧,并且仅在无法从左侧的值确定结果(布尔值0
或1
类型)时才评估其右侧。int
这种特性称为快捷评估,也适用于||
三元运算符?
/:
。c
必须有一个类型,可以容纳所有返回的值getchar()
:所有类型unsigned char
的值和特殊的负值EOF
。既不char
也不适合这一点,因为要么不正确地匹配signed char
(在ISO-8859-1 中)有符号的情况,要么永远不匹配无符号的情况。是正确的类型。unsigned char
c == EOF
\377
'ÿ'
char
char
int
c
推荐阅读
- javascript - 使用路径从 JSON 对象获取数据
- ansible - Ansible playbook 执行中的“apt”模块每次在不同主机上随机失败,并显示消息“无法锁定 apt 以进行独占操作”
- opencl - Intel FPGA OpenCL:追踪内核时钟频率低的原因
- javascript - 发送带参数的 URL 并在参数中使用 javascript 值
- r - 使用 foreach 和 doMC 时如何控制发送给工作人员的内容
- reactjs - 使用 Reach-Router、React-Transition-Group 和 GSAP 的页面转换问题?
- neo4j - 在所有元素中有效地找到属性的最大值
- elasticsearch - 时间序列 AVG 聚合
- python - 如何正确存储循环迭代的中间结果?
- scala - 如何从 Scala 连接到 Exasol?