我在运行我的代码时遇到了一个错误,它接受用户的温度并以相反的顺序打印,为什么会这样?



我写这段代码,作为作业,以满足以下要求:

(不允许使用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). 

我不太清楚为什么。

如有任何帮助,不胜感激。

谢谢!

您根本没有分配足够的内存:

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

相关内容

  • 没有找到相关文章

最新更新