Thread: Trouble with Memory Allocation and File Handling

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    3

    Trouble with Memory Allocation and File Handling

    I have a Binary Text File that contains details about a structure that I would like to make a program to read. I'm fairly new to C programming and can't see why the program I have made does not work. When ran the program just crashes. The compiler say there is 0 errors or warnings. I ahve attached both the binary file I'm trying to read and my program. Thank You any help you can give.
    Attached Files Attached Files

  2. #2
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    The compiler won't show the errors. What does the terminal say when you run the program? Have you considered trying to get a core dump?

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    I have a Binary Text File that contains details about a structure that I would like to make a program to read.
    No you have a binary file, not a text file.

    It appears that you probably haven't allocated the memory correctly. You need to allocate memory for the number of bytes, not the number of structures. You would be better off reading a struct person instead of a single character when trying to determine the number of structures.

  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    3
    Quote Originally Posted by catacombs View Post
    The compiler won't show the errors. What does the terminal say when you run the program? Have you considered trying to get a core dump?
    The terminal crashes when the program is ran.

  5. #5
    Registered User
    Join Date
    May 2020
    Posts
    3
    Quote Originally Posted by jimblumberg View Post
    No you have a binary file, not a text file.

    It appears that you probably haven't allocated the memory correctly. You need to allocate memory for the number of bytes, not the number of structures. You would be better off reading a struct person instead of a single character when trying to determine the number of structures.
    As stated in the post I am fairly new to C programming and don'tt have a clue how to do that.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    As stated in the post I am fairly new to C programming and don'tt have a clue how to do that.
    Then just statically allocate the memory for your array and leave the dynamic allocation until you're more familiar with the language.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,215
    > while(!feof(infile))
    This will make your count of the number of bytes off by 1.
    while ( getc(infile) != EOF ) size++;

    > realloc(input,size);
    Two things about realloc
    - it assumes the input pointer is either NULL, or pointing to allocated memory; neither is true in this code.
    - it also returns a pointer to the new memory, which you ignore anyway.

    Start with this.
    input = malloc(size);



    > while(fread(&input, sizeof(struct person), 1, infile))
    input is already a pointer, there is no need for the & here.
    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.

  8. #8
    Registered User
    Join Date
    Feb 2019
    Posts
    746
    > realloc(input,size);
    Two things about realloc
    - it assumes the input pointer is either NULL, or pointing to allocated memory; neither is true in this code.
    - it also returns a pointer to the new memory, which you ignore anyway.

    Start with this.
    input = malloc(size);
    And to add one more to @Salem's list:
    - never do this:
    Code:
    input = realloc(input, size);
    If you do and realloc cannot allocate memory, you'll end up with a "memory leakage".

    []s
    Fred

  9. #9
    Registered User catacombs's Avatar
    Join Date
    May 2019
    Location
    /home/
    Posts
    81
    Quote Originally Posted by Danai View Post
    The terminal crashes when the program is ran.
    Yes, but you can still get a core dump of the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-17-2012, 04:27 PM
  2. exception handling ( memory allocation)
    By manzoor in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2008, 06:43 AM
  3. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM
  4. pointer and memory allocation trouble
    By volk in forum C++ Programming
    Replies: 27
    Last Post: 05-17-2003, 11:14 AM
  5. Dynamic memory allocation for file handling
    By prashanth_sh in forum C Programming
    Replies: 2
    Last Post: 11-07-2002, 05:50 AM

Tags for this Thread