0

I filled up my array with the following elements:

91.54
-96.58
-5.14
-78.25
15.33
89.34
-12.76
-92.56
38.21
36.11
-43.83
48.39
79.57
34.34
-26.07
-10.47
54.37
9.06
-67.17
53.83
-8.13
89.71
85.77
-68.44
-88.22
-86.24
-5.44
53.32
75.62
51.08
-80.42
91.94
33.5
-29.44
70.33
70.56
-66.75
10.41
92.84
-33.14
-0.34

The last element of my array is the sum of all of these float values.

And I want to print the number of elements with the sizeof technique.

#include <stdio.h>
#include <stdlib.h>


int main(){

    FILE *f = fopen("floats.txt", "r");
    float *V, num, sum = 0;
    int i = 0;
    V = (float *)malloc(sizeof(float));

    while(fscanf(f, "%f", &num) == 1){
        i++;
        V = (float *)realloc(V, i*sizeof(float));
        V[(i-1)] = num;
        sum += num;
    }

    i++;
    V = (float *)realloc(V, i*sizeof(float));
    V[(i-1)] = sum;

    /*printf("%f\n", soma);
    printf("%f\n", V[(i-1)]);
    printf("%f\n", size);*/

    printf("%ld\n", sizeof(V)/sizeof(V[0]));
    printf("Sum = %f\n", V[sizeof(V)/sizeof(V[0]) -1]);
    return 0;

}

enter image description here

The number of elements must be 41 but it is printing 2 and the Sum must be 375.779999 the last element stored in the array.

Why is this printing 2 instead of 41? That is my main question!

Andrej Hatzi
  • 312
  • 4
  • 18
  • `sizeof(V)` is the size of the pointer `V`, which is normally 4 or 8 bytes. It is **not** the size of the array it is pointing to. You must keep track of that information yourself, which you do by using the variable `i`. – Andreas Wenzel Mar 23 '20 at 15:01
  • The code you posted does not compile. You are using an undeclared variable `soma`. Therefore, the code fails to meet the requirements of a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Andreas Wenzel Mar 23 '20 at 15:04
  • Just as a side note: Dynamic memory allocation functions such as malloc() and realloc() are rather expensive in terms of performance. Therefore, they should not be called in a loop, if possible. Especially calling realloc() in a loop is very expensive, as the whole buffer will have to be copied if the memory address changes. Therefore, it would be better for performance to allocate a larger amount at once, and only extend it if you detemine that you need more memory. However, this is not the cause of the problem you are having. – Andreas Wenzel Mar 23 '20 at 15:09
  • Please post the output as text, not as a screenshot. Also, the output in the screenshot does not correspond to the code you posted, which is another reason why this is not a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). The code you posted should output 5 lines, whereas your screenshot only shows 2 lines of output. – Andreas Wenzel Mar 23 '20 at 15:15
  • @Andreas Wenzel, it compiles I just changed the names from Portuguese to English while writing this question, my bad. – Andrej Hatzi Mar 24 '20 at 15:24
  • That still doesn't explain why your screenshot only shows 2 lines of output, while the program you posted outputs 5 lines. Obviously, the output you posted does not correspond to the program you posted. Therefore, we have no way of knowing what is causing the output you posted. – Andreas Wenzel Mar 24 '20 at 16:10
  • I mistyped the comment. – Andrej Hatzi Mar 24 '20 at 17:15

0 Answers0