Reputation: 873
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. 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
.
#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? Or is it still able to perform the calculation using less RAM?
When the int function is performing the calculation, does it still have to store the number as a double, before returning the int?
Upvotes: 0
Views: 152
Reputation: 2547
The answer depends on
1.Size of integer on your platform (Compiler Specific).
2.The way in which your compiler+processor supports floating point arithmetic. Floating point arithmetic could be emulated by your compiler if your processor doesn't have FPU.
Consider Below Points:
Assuming for your platform double
needs more bytes than integer
:
Stack Usage will be more in getMean2
function.
Assuming your processor don't have FPU: Text(Code) Segment will consume more memory in getMean2
function.
return sum/size;
will be a integer division in getMean1
and it will be a floating point division in getMean2
Note:
As you are neither allocating memory dynamically nor you are having global variables your data
segment and heap
will be unaffected.
Upvotes: 1
Reputation:
When the int function is performing the calculation, does it still have to store the number as a double, before returning the 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:
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).
In your other function, one operand is already a double
. Have a look at
C11 (draft 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
.
So in this case, your size
is implicitly converted to double
and therefore /
performs a floating point division, the result is a double
.
Upvotes: 2