1

I used following function to read double values from a text file and then convert it into a floating point array, but it ends up with unexpected termination during program running. Can anyone tell me what goes wrong here.

float *readFile_double_to_float(char *fileName, int fileSize)
{
    double *array = (double *)malloc(fileSize * sizeof(double *));
    float *array_float = (float *)malloc(fileSize * sizeof(float *));

    FILE *fp = fopen(fileName, "r");
    if (fp == NULL)
    {
        printf("Error: Cannot open file for reading ===> %s\n", fileName);
        exit(1);
    };
    for (int i = 0; i < fileSize; i++)
    {
        fscanf(fp, "%lf", array + i);
    };
    fclose(fp);

    for (int i = 0; i < fileSize; i++)
    {
        array_float[i] = (float)array[i];
    };

    printf("CHECK VAL DOUBLE TO FLOAT : float: %.20f | double: %.20f\n", array_float[0], array[0]);

    printf("FILE READ COMPLETED ===> %s\n", fileName);

    free(array);
    return array_float;
}
4
  • 3
    sizeof(double *) is the size of a pointer to double, not a double value. On e.g. a 32-bit platform sizeof(double) is typically larger than sizeof(double *), which means you won't allocate enough memory. Commented Jun 26, 2020 at 12:35
  • And why the temporary double array? Why not read directly into the float array? Commented Jun 26, 2020 at 12:35
  • 1
    Why all the casting? Don't cast unless you need it. Commented Jun 26, 2020 at 12:49
  • regarding: double *array = (double *)malloc(fileSize * sizeof(double *)); and float *array_float = (float *)malloc(fileSize * sizeof(float *)); 1) allocate double or float NOT double * or float * 2) the returned type is void* which can be assigned to any pointer, Casting just clutters the code and is error prone. 3) always check (!=NULL) the returned value to assure the operation was successful. If not successful (==NULL) then call perror( "malloc failed" ); so the user is informed of the problem. Commented Jun 26, 2020 at 13:44

1 Answer 1

2

Wrong size allocated. @Some programmer dude

//                                          vvvvvvvvvvvvvvvv  size of a pointer
double *array = (double *)malloc(fileSize * sizeof(double *));

Avoid mistakes, drop the cast and allocate to the size of the referenced data. Easier to code right, review and maintain.

double *array = malloc(sizeof *array * fileSize);
//                     ^^^^^^^^^^^^^ Sizeof referenced data      

Better code would also test allocation success.

if (array == NULL) {
  fprintf(stderr, "Out of memory");
  return NULL;
}

More informative to use "%e", "%g" or "%a".

//printf("CHECK VAL DOUBLE TO FLOAT : float: %.20f | double: %.20f\n", 
printf("CHECK VAL DOUBLE TO FLOAT : float: % .20e | double: % .20e\n", 
    array_float[0], array[0]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.