Thread: Help, Dynamic allocation of memory using malloc. C

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    5

    Help, Dynamic allocation of memory using malloc. C

    I am not sure what is wrong, my .bin comes out to gibberish and I think I did the chain incorrectly, but I could be wrong.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    
    typedef struct flight_struct{
        char flightNum[7];
        char originAirport[5];
        char destAirport [5];
        int timestamp;
        struct flight_struct *next;
    } flightRec;
    
    
    int main(){
    struct flight_struct *head; // unchanging first node.
    struct flight_struct *tail; //the conductor.
    FILE* binFile = fopen("acars.bin","r");
    FILE* DataOut;
        
        struct flight_struct *p =(struct flight_struct*) malloc(sizeof(flight_struct)); //malloc the first struct
        fread(p,sizeof(flightRec),1,binFile);    //read the file into it.
    
    
        head = p; //make head point to that struct
        tail = p; //make tail point to that struct
    
    
    //    fclose(binFile);
        
        while (feof(binFile) ==0){
        flight_struct *temp = (struct flight_struct*) malloc(sizeof(flight_struct)); //malloc a new struct
        fread(temp,sizeof(flight_struct),1,binFile); //read the next struct from acars.bin into the structure you malloc'ed
        temp -> next = NULL; // add that struct to your linked list using the next memeber of the struct
        tail -> next = temp; // set tail to point to the element you just added
        tail = tail -> next;
        } //while not eof on acars file
    
    
        tail = head;
    
    
        while(tail != 0 ){    
            int t;
            t = tail -> timestamp;
            time_t time = t;
            printf("%s, %s, %s, %s\n\n",tail -> flightNum,tail -> originAirport,tail -> destAirport,asctime(gmtime(&time)));
            tail = tail -> next;
        } //starting at head traverse the list printing the leemnts of each strucure
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    FAQ > Casting malloc - Cprogramming.com
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    > time_t time = t;
    time is also the name of a function in time.h
    You should pick a different name for your local variable, if you want to avoid later confusion.

    > fread(p,sizeof(flightRec),1,binFile);
    Presumably, your flightRec (as stored in the file) does NOT contain the 'next' pointer you have in your struct declaration.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by Salem View Post
    FAQ > Casting malloc - Cprogramming.com
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    > time_t time = t;
    time is also the name of a function in time.h
    You should pick a different name for your local variable, if you want to avoid later confusion.

    > fread(p,sizeof(flightRec),1,binFile);
    Presumably, your flightRec (as stored in the file) does NOT contain the 'next' pointer you have in your struct declaration.
    I will take a look at the Casting malloc link you just sent me, that being said, my professor wants me to use a eof() loop but yeah I heard that it's a really bad thing to do...

    I won't lie, i'm not sure what you mean by "Presumably, your flightRec (as stored in the file) does NOT contain the 'next' pointer you have in your struct declaration."
    Are you saying that I didn't properly make the 'next' pointer in my struct?

    And thank you for the reply!


    I should add, what the output looks like is this
    Help, Dynamic allocation of memory using malloc. C-c6870146fb84123bb4f726ba90ba443a-png


    What it needs to look like is this.
    Help, Dynamic allocation of memory using malloc. C-d14c34617ec0988925eeb1c9330fdea4-png

    Those two are from the same .bin file.
    Last edited by Bryan Justo; 04-29-2015 at 04:55 AM. Reason: Adding more info

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Are you saying that I didn't properly make the 'next' pointer in my struct?
    I'm saying you need two structs.

    One for what the information looks like on disk.
    Another for what the information looks like in memory.

    If you just had an array, they would be the same thing.
    But for a linked list, your in-memory version has an extra 'next' pointer which isn't present in the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by Salem View Post
    > Are you saying that I didn't properly make the 'next' pointer in my struct?
    I'm saying you need two structs.

    One for what the information looks like on disk.
    Another for what the information looks like in memory.

    If you just had an array, they would be the same thing.
    But for a linked list, your in-memory version has an extra 'next' pointer which isn't present in the file.
    oh, I see, so I would need to add something like

    Code:
     
    struct flight_struct *next{
    //What ever it looks like in memory?
    };
    And to be honest, i'm not sure what it supposed to look like in memory, seeing as the format in the actual .bin looks like
    YV2827 KCLT KSRQ
    ,ƒŠRYV2782V KCLT KSTQ
    ect
    And thank you again Salem, for bearing with me, i'm a bit of new when it comes to programming..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    I suggest you start with a program which reads the file into a struct and prints the values out to the screen.

    When that works, then think about how you make a linked list out of it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by Salem View Post
    I suggest you start with a program which reads the file into a struct and prints the values out to the screen.

    When that works, then think about how you make a linked list out of it.
    I have done this, I made an array struct and made it work through there. I don't know how to properly make a linked list. I have even shown you the results of my other program.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Before you can build a linked list you need valid nodes, and you're not using the typedef structure type that you created.

    Your nodes should be of type flightRec.

    Edit: Clarification - you're nodes are valid - but why create a typedef and not use it?
    Last edited by gemera; 04-29-2015 at 12:56 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So if you have this for reading from the file,
    Code:
    struct foo {
      // members
    };
    then a linked list version would be
    Code:
    struct foolist {
      struct foo data;
      struct foolist *next;
    };
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2015
    Posts
    5
    Quote Originally Posted by gemera View Post
    Before you can build a linked list you need valid nodes, and you're not using the typedef structure type that you created.

    Your nodes should be of type flightRec.

    Edit: Clarification - you're nodes are valid - but why create a typedef and not use it?
    I won't lie, I assumed that typedef would automatically make everything with the flight_struct into that, I didn't know I needed to use flightRec, that is my bad sorry.
    Thank you for pointing it out, i would never have known otherwise!

    Quote Originally Posted by Salem View Post
    So if you have this for reading from the file,
    Code:
    struct foo {
      // members
    };
    then a linked list version would be
    Code:
    struct foolist {
      struct foo data;
      struct foolist *next;
    };
    okay,how you can call my really ignorant or hard headed, but I want to be 100% clear on this because I feel like this is really important to understand.

    using that, would in my code be
    Code:
    typedef struct flight_struct{
        char flightNum[7];
        char originAirport[5];
        char destAirport [5];
        int timestamp;
        struct flight_struct *next;
    } flightRec;
    
    
    struct next_ptr{
        struct flight_struct data;
        struct next_ptr *next;
    };
    or, are you saying that I should change it into just one, such as
    Code:
    typedef struct flight_struct{
        char flightNum[7];
        char originAirport[5];
        char destAirport [5];
        int timestamp;
            struct flight_struct data;
    
        struct flight_struct *next;
    } flightRec;
    or am I just completely left field and no where close to what you are trying to tell me
    because I feel like i'm understanding it more, but I just want to be completely sure.

    And seriously, thanks for being patient with me. I am still learning and I appreciate the help!
    Last edited by Bryan Justo; 04-29-2015 at 07:11 PM. Reason: thanking people.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > using that, would in my code be
    You almost got it right!

    Just remove line 6 - struct flight_struct *next;

    and you should be there.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc memory allocation
    By jamorp in forum C Programming
    Replies: 3
    Last Post: 10-17-2014, 06:57 AM
  2. warnings- dynamic memory allocation - malloc
    By raviraj123 in forum C Programming
    Replies: 6
    Last Post: 06-05-2013, 01:28 PM
  3. Help! Using dynamic allocation and malloc
    By jh294 in forum C Programming
    Replies: 5
    Last Post: 04-01-2011, 01:13 PM
  4. Dynamic Memory Allocation (malloc vs calloc)
    By lostandconfused in forum C Programming
    Replies: 10
    Last Post: 09-01-2010, 01:56 PM
  5. Memory allocation without malloc
    By messi89 in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 07:27 AM

Tags for this Thread