Home > Net >  using malloc - c programming
using malloc - c programming

Time:01-13

I'm wondering if anyone can help me with my below code. I'm struggling to implement dynamic allocation for my program when reading data from a file and storing it in a struct. I'm new to both structs and dynamic allocation. While my program seems to be storing the data successfully into the struct I can see in Visual Studio that the allocation of these is not correct. Any advice on storing the data dynamic would be much appreciated. I was trying to use malloc but could not get this to work for me.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void load_data();


typedef struct {
    char name[12];
    char vaccinevendor[8];
    char vaccinationdate[12];
    char dob[12];
    char underlyingcond[8];
    char id[7];
}  Record;



int main(void)
{

    load_data();

    return 0;
}

void load_data()
{
    
    Record s1[2];


    FILE* Ptr;  /*file pointer*/

    if ((Ptr = fopen("records.txt", "rb")) == NULL) {
        printf("File does not exist\n");
    
        Ptr = fopen("records.txt", "w");
        if (Ptr != NULL)
            printf("records.txt file has now been created.\n");
        else {
            printf("Unable to create file!\n");
            return 1;
        }
    }


     Ptr = (char*)malloc(1000 * sizeof(char));

    // read file contents till end of file
    fread(&s1, sizeof(Record), 2, Ptr);
    
    fclose(Ptr); /*close stream*/

}

CodePudding user response:

Your code does not really make sense. Ptr points to the file opened through fopen which is fine. But then you allocate dynamic memory (through malloc) and assign the adress of this memory to the same Ptr. So basically you have lost the previous value of Ptr, in other terms the reference to your file (not to mention Ptr is of type FILE * and you assign to it a char *).

You don't need to allocate memory for Ptr, you need to allocate memory for the buffer where you copy data from this file. But here s1 is located on the stack, you don't need dynamic memory allocation.

Also be careful when copying raw data to a struct. Data structures may have "holes" in between their fields depending on data alignement (here as you have only chararrays, all the fields are probably contiguous).

I suggest you learn about data structures and the different kinds of memory allocation in C.

CodePudding user response:

Correcting the obvious error

// Ptr = (char*)malloc(1000 * sizeof(char));

// read file contents till end of file
fread(&s1, sizeof(Record), 2, Ptr);

fclose(Ptr); /*close stream*/

the commented out line is clearly thinking it neeeds to 'allocate a buffer' in some way. No, s1 is the buffer, and you certainly never want to change Ptr (its the pointer to the internals of the file system).

However this code almost certainly wont work unless your file is very precisely laid out. For example it needs to start like this (in hex)

4141414100000000000000004242420000000000

if the fist name is "AAAA" and the first vendor is "BBB". And even then it might not work due to struct padding issues

  •  Tags:  
  • Related