简体   繁体   English

难以理解C中的内存分配

[英]Difficulty understanding memory allocation in C

I am trying to understand the different aspects of memory allocation in C. In the example below, I am calculating the mean of an array. 我试图了解C语言中内存分配的不同方面。在下面的示例中,我正在计算数组的均值。 I have defined one function in which the return is an int = 4 , and the second in which the return is a double = 4.57 . 我定义了一个函数,其中返回值为int = 4 ,第二个函数中返回为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;
}

In the case of the mean function returning an int, is the same memory allocation in RAM still used as in the function returning the double? 在均值函数返回int的情况下,RAM中仍使用与返回double的函数中相同的内存分配吗? Or is it still able to perform the calculation using less RAM? 还是仍然可以使用更少的RAM执行计算?

When the int function is performing the calculation, does it still have to store the number as a double, before returning the int? 当int函数执行计算时,在返回int之前,它仍然还必须将数字存储为双精度吗?

When the int function is performing the calculation, does it still have to store the number as a double, before returning the int? 当int函数执行计算时,在返回int之前,它仍然还必须将数字存储为双精度吗?

This question seems to assume that the result of the following line: 这个问题似乎假设以下行的结果:

return sum/size;

is always a floating point. 总是浮点数。 But this assumption is wrong. 但是这个假设是错误的。 See for example 例如看

C11 (draft N1570), §6.5.6 p6 : C11(草案N1570),第6.5.6 p6节

When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. 当整数相除时, /运算符的结果是代数商,其中舍弃了任何小数部分。

So, if both operands have an integer type, you just get an integer division , the result is an integer type, in your example int (with a value that just discards any fractional part). 因此,如果两个操作数都为整数类型,则只得到一个整数除法 ,结果为整数类型,在您的示例int (其值仅丢弃任何小数部分)。


In your other function, one operand is already a double . 在另一个函数中,一个操作数已经是double Have a look at 看一下

C11 (draft N1570) §6.3.1.8 p1 : C11(N1570草稿)§6.3.1.8p1

[...] [...]
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 . 否则,如果一个操作数的相应实型为double ,则另一个操作数将在不更改类型域的情况下转换为其相应实型为double类型。

So in this case, your size is implicitly converted to double and therefore / performs a floating point division , the result is a double . 因此,在这种情况下,您的size隐式转换为double ,因此/执行浮点除法 ,结果为double

The answer depends on 答案取决于

1.Size of integer on your platform (Compiler Specific). 1.您平台上整数的大小(特定于编译器)。

2.The way in which your compiler+processor supports floating point arithmetic. 2.您的编译器+处理器支持浮点运算的方式。 Floating point arithmetic could be emulated by your compiler if your processor doesn't have FPU. 如果您的处理器没有FPU,则编译器可以仿真浮点算法。

Consider Below Points: 考虑以下几点:

Assuming for your platform double needs more bytes than integer : Stack Usage will be more in getMean2 function. 假设您的平台double需要比integer更多的字节: getMean2函数中的堆栈使用量会更多。

Assuming your processor don't have FPU: Text(Code) Segment will consume more memory in getMean2 function. 假设您的处理器没有FPU:文本(代码)段会在getMean2函数中消耗更多的内存。

return sum/size; will be a integer division in getMean1 and it will be a floating point division in getMean2 将在整数除法getMean1 ,这将是在一个浮点除法getMean2

Note: As you are neither allocating memory dynamically nor you are having global variables your data segment and heap will be unaffected. 注意:由于您既没有动态分配内存,也没有全局变量,因此data段和heap将不受影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM