提问人:DD Confederacy 提问时间:10/17/2021 最后编辑:DD Confederacy 更新时间:10/20/2021 访问量:102
我在运行代码时遇到错误,该代码接受用户的温度并以相反的顺序打印,为什么会这样?
I am running into an error while running my code, which accepts temperatures from user and prints in reverse order, why is that so?
问:
我正在编写这段代码,作为家庭作业,以满足以下要求:
(不允许使用函数 realloc)
最初分配一个阵列以容纳多达 5 个温度。
提示用户输入温度,并在输入完成后键入值 -100.0。
如果用户填满了数组,您的程序应该 动态分配一个大小翻倍的新数组。
将旧值复制到新数组中。 解除分配旧数组。
继续读取新数组。
读取完数组后,程序应像以前一样,以 1 个小数点的精度反向(从最近到最旧)输出温度读数,并在输入末尾换行
这是我的方法:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int size = 5;
float *temperature_first = malloc(sizeof(float)*size);
float *temperature_second;
float inputArray;
int switch_array = 0;
int count = 0;
printf("'Please enter temperature; type '-100' to end: ");
while(inputArray !=-100){
while((inputArray != -100)&&(count<=size)){
if(switch_array==0){
scanf("%f", &temperature_first[count]);
inputArray = temperature_first[count];
}
else{
scanf("%f", &temperature_second[count]);
inputArray = temperature_second[count];
}
count++;
}
if((count>=size)){
size = size*2;
if(switch_array==0){
temperature_second = malloc(sizeof(float)*size);
for(int elements = 0;elements<(size/2)+1;elements++){
temperature_second[elements] = temperature_first[elements];
}
free(temperature_first);
switch_array = 1;
}
else{
temperature_first = malloc(sizeof(float)*size);
for(int elements = 0;elements<(size/2)+1;elements++){
temperature_first[elements] = temperature_second[elements];
}
free(temperature_second);
switch_array = 0;
}
}
}
for(int i = count-2;i>-1;i--){
if(i==0){
if(switch_array==0)
{
printf("%0.1f", temperature_first[i]);
}
else{
printf("%0.1f", temperature_second[i]);
}
}
else{
if(switch_array==0){
printf("%0.1f ", temperature_first[i]);
}
else{
printf("%0.1f ", temperature_second[i]);
}
}
}
printf("\n");
if(switch_array==0){
free(temperature_first);
}
else{
free(temperature_second);
}
return 0;
}
运行特定测试时: 输入: 16.5 18.8 20.5 21.3 16.2 15.0 17.0 19.7 21.2 56.3 12.5 44.5
以下行用于编译:
gcc -Wall -Werror=vla -std=c11 -o temperatures02 temperatures02.c
这是我遇到的:
malloc(): corrupted top size
Aborted (core dumped).
我不太确定为什么。
任何帮助将不胜感激。
谢谢!
答:
1赞
Gerhardh
10/17/2021
#1
您根本没有分配足够的内存:
if((count>size)||(count==size)){
size = size*2;
if(switch_array==0){
temperature_second = malloc(sizeof(float)*count);
您需要而不是 .否则,您的条目将最终进入您不拥有的内存中。size
count
count .. size-1
代码的短得多的版本可能如下所示:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int size = 5;
float *temperatures = malloc(sizeof(float)*size); // TODO: Check for NULL
float temperature;
int count = 0;
printf("Please enter temperature; type '-100' to end: ");
scanf("%f", &temperature);
// TODO: Check result for error!
while (temperature != -100) {
temperatures[count] = temperature;
count++;
if (count == size) {
size *= 2;
#if DO_IT_LIKE_THE_TASK_DEMANDS_IT
float *newarray = malloc(sizeof(float)*size);
// TODO: Check for NULL!
for (int i = 0; i < count-1; i++) {
newarry[i] = temperatures[i];
}
free(temperatures);
#else
float *newarray = realloc(temperatures, sizeof(float)*size);
// TODO: Check for NULL!
#endif
temperatures = newarray;
}
printf("Please enter temperature; type '-100' to end: ");
scanf("%f", &temperature);
// TODO: Check result for error!
}
// Print them backwards (not in the task but in your code
// Last element was put into temperatures[count] before we did count++.
// Therefore now we need to start at index count-1, not count-2
for(int i = count-1;i>-1;i--) {
if (i==0) {
printf("%0.1f", temperatures[i]);
}
else {
printf("%0.1f ", temperatures[i]);
}
}
printf("\n");
free(temperatures);
return 0;
}
在 https://www.onlinegdb.com/online_c_compiler 展会上测试:
Please enter temperature; type '-100' to end: 16.5
Please enter temperature; type '-100' to end: 18.8
Please enter temperature; type '-100' to end: 20.5
Please enter temperature; type '-100' to end: 21.3
Please enter temperature; type '-100' to end: 16.2
Please enter temperature; type '-100' to end: 15.0
Please enter temperature; type '-100' to end: 17.0
Please enter temperature; type '-100' to end: 19.7
Please enter temperature; type '-100' to end: 21.2
Please enter temperature; type '-100' to end: 56.3
Please enter temperature; type '-100' to end: 44.5
Please enter temperature; type '-100' to end: -100
44.5 56.3 21.2 19.7 17.0 15.0 16.2 21.3 20.5 18.8 16.5
...Program finished with exit code 0
评论
-Wall -Wextra
inputArray
if((count>size)||(count==size))
if (count>=size)
realloc