0

Currently have a program where I read a binary file where the first 4 bytes contains an integer n which is the amount of double values in the file, and the rest of the file has 8n bytes containing an array of double values.

I can read the file fine and determine the coefficients, the issue lies with detecting if the information given by n is correct. My question is why does

sizeof(*coeff_values_p)

return a value of 4 instead of 8n. I am using a file where n=29 so shouldn't the size be 232?

Any help would be appreciated.

#include "Ass-01.h"
#include <stdio.h>

int read_coefficients(int *coeff_num_p, double **coeff_values_p, char 
*filename)
{
    //uint32_t n;                           //Number of coefficients
    FILE *f;                            //File to be opened

    //File reading storing the value of n
    f = fopen(filename, "rb");
    fread(coeff_num_p, sizeof(uint32_t), 1, f);

    *coeff_values_p = malloc(8 * *coeff_num_p);

    fread(*coeff_values_p, sizeof(uint64_t), *coeff_num_p, f);

    if (sizeof(*coeff_values_p) != (*coeff_num_p*sizeof(uint64_t)))
    {
        return -1;
    }
    else
    {
        return 0;
    }
}
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
Koobz866
  • 166
  • 1
  • 2
  • 17
  • 6
    The problem is that you *don't* have an "array". You have a pointer that points to some memory of (for the compiler) unknown size. When you use `sizeof` on a pointer, you get the size of the *pointer* and not what it points to. – Some programmer dude Mar 13 '18 at 10:13
  • 1
    `sizeof` is a compile time operator. `coeff_values_p` is of type `double **`, so `sizeof (*coeff_values_p)` is equal to the size of a `double *`. That is fixed, regardless of the value of `coeff_values_p` or of `*coeff_values_p` or of `**coeff_values_p` – Peter Mar 13 '18 at 10:14
  • 1
    You should probably check what [`fread`](http://en.cppreference.com/w/c/io/fread) *returns*. – Some programmer dude Mar 13 '18 at 10:14
  • Doesn't malloc allocate that pointer with the size I've given it? – Koobz866 Mar 13 '18 at 10:15
  • 2
    `malloc()` returns a pointer to dynamically allocated memory. `sizeof` cannot be used to check the size of what is allocated. – Peter Mar 13 '18 at 10:17
  • Yes, if `malloc` succeeds (and doesn't return a null pointer, which you doesn't check for) then you will have some memory of the requested size. But if you want to make sure that the `fread` call read all elements, then you need to check what it returns. And you still can't use `sizeof` on a pointer to get the size of the memory it points to. – Some programmer dude Mar 13 '18 at 10:17
  • why not closing as duplicate instead of commenting the same thing over and over again? let's save time. – Jean-François Fabre Mar 13 '18 at 10:18
  • @Peter Not fully compile-time. C allows [variable-length arrays](https://en.wikipedia.org/wiki/Variable-length_array) which have their size set at run-time. So for those `sizeof` have a run-time component (which is handled by the compiler by generating the correct code). – Some programmer dude Mar 13 '18 at 10:19
  • @Someprogrammerdude - true. But if the operand is `double *`, as in this question, it is not a VLA - and `sizeof` is a compile-time operator for anything except a VLA. – Peter Mar 13 '18 at 10:22
  • Thanks guys, fixed the issue by using the return value of fread. – Koobz866 Mar 13 '18 at 10:23

0 Answers0