难以理解 C 中的内存分配
Difficulty understanding memory allocation in C
我试图了解 C 中内存分配的不同方面。在下面的示例中,我正在计算数组的平均值。我定义了一个函数,其中 return 是 int = 4
,第二个函数中 return 是 double = 4.57
。
#include <stdio.h>
#include <stdlib.h>
int getMean(int arr[], int size);
double getMean2(int arr[], int size);
int main()
{
int mean1;
double mean2;
int array[7] = {1,3,5,7,5,4,7};
mean1 = getMean(array, sizeof(array)/sizeof(array[0]));
printf(" Mean 1 = %d", mean1);
mean2 = getMean2(array, sizeof(array)/sizeof(array[0]));
printf("\n Mean 2 = %.2f", mean2);
return 0;
}
int getMean(int arr[], int size) {
int i;
printf("\n");
int sum = 0;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum/size;
}
double getMean2(int arr[], int size) {
int i;
printf("\n");
double sum = 0;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum/size;
}
在 mean 函数 returning 一个 int 的情况下,RAM 中的内存分配是否仍与函数 returning double 中使用的相同?还是它仍然能够使用更少的 RAM 执行计算?
当 int 函数执行计算时,在 returning int 之前,它是否仍然必须将数字存储为 double?
When the int function is performing the calculation, does it still have to store the number as a double, before returning the int?
这道题似乎假设了下面一行的结果:
return sum/size;
始终是一个浮点数。但这个假设是错误的。参见示例
C11(N1570 草案),§6.5.6 p6:
When integers are divided, the result of the /
operator is the algebraic quotient with any
fractional part discarded.
所以,如果两个操作数都是整数类型,你只是得到一个整数除法,结果是一个整数类型,在您的示例 int
(其值仅 丢弃 任何小数部分)。
在您的另一个函数中,一个操作数已经是 double
。看看
C11(N1570 草案)§6.3.1.8 p1:
[...]
Otherwise, if the corresponding real type of either operand is double
, the other
operand is converted, without change of type domain, to a type whose
corresponding real type is double
.
所以在这种情况下,您的 size
被隐式转换为 double
,因此 /
执行 浮点除法 ,结果是 double
.
答案取决于
1.Size 您平台上的整数(特定于编译器)。
2.The 编译器+处理器支持浮点运算的方式。如果您的处理器没有 FPU,则您的编译器可以模拟浮点运算。
考虑以下几点:
假设您的平台 double
需要比 integer
更多的字节:
getMean2
function.
中的 Stack Usage 会更多
假设您的处理器没有 FPU:文本(代码)段将在 getMean2
函数中消耗更多内存。
return sum/size;
在 getMean1
中将是整数除法,在 getMean2
中将是浮点数除法
注意:
由于您既没有动态分配内存也没有全局变量,因此您的 data
段和 heap
将不受影响。
我试图了解 C 中内存分配的不同方面。在下面的示例中,我正在计算数组的平均值。我定义了一个函数,其中 return 是 int = 4
,第二个函数中 return 是 double = 4.57
。
#include <stdio.h>
#include <stdlib.h>
int getMean(int arr[], int size);
double getMean2(int arr[], int size);
int main()
{
int mean1;
double mean2;
int array[7] = {1,3,5,7,5,4,7};
mean1 = getMean(array, sizeof(array)/sizeof(array[0]));
printf(" Mean 1 = %d", mean1);
mean2 = getMean2(array, sizeof(array)/sizeof(array[0]));
printf("\n Mean 2 = %.2f", mean2);
return 0;
}
int getMean(int arr[], int size) {
int i;
printf("\n");
int sum = 0;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum/size;
}
double getMean2(int arr[], int size) {
int i;
printf("\n");
double sum = 0;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum/size;
}
在 mean 函数 returning 一个 int 的情况下,RAM 中的内存分配是否仍与函数 returning double 中使用的相同?还是它仍然能够使用更少的 RAM 执行计算?
当 int 函数执行计算时,在 returning int 之前,它是否仍然必须将数字存储为 double?
When the int function is performing the calculation, does it still have to store the number as a double, before returning the int?
这道题似乎假设了下面一行的结果:
return sum/size;
始终是一个浮点数。但这个假设是错误的。参见示例
C11(N1570 草案),§6.5.6 p6:
When integers are divided, the result of the
/
operator is the algebraic quotient with any fractional part discarded.
所以,如果两个操作数都是整数类型,你只是得到一个整数除法,结果是一个整数类型,在您的示例 int
(其值仅 丢弃 任何小数部分)。
在您的另一个函数中,一个操作数已经是 double
。看看
C11(N1570 草案)§6.3.1.8 p1:
[...]
Otherwise, if the corresponding real type of either operand isdouble
, the other operand is converted, without change of type domain, to a type whose corresponding real type isdouble
.
所以在这种情况下,您的 size
被隐式转换为 double
,因此 /
执行 浮点除法 ,结果是 double
.
答案取决于
1.Size 您平台上的整数(特定于编译器)。
2.The 编译器+处理器支持浮点运算的方式。如果您的处理器没有 FPU,则您的编译器可以模拟浮点运算。
考虑以下几点:
假设您的平台 double
需要比 integer
更多的字节:
getMean2
function.
假设您的处理器没有 FPU:文本(代码)段将在 getMean2
函数中消耗更多内存。
return sum/size;
在 getMean1
中将是整数除法,在 getMean2
注意:
由于您既没有动态分配内存也没有全局变量,因此您的 data
段和 heap
将不受影响。