Segmentation Fault with fread() in C

Discussion in 'OT Technology' started by Seeders, Jun 2, 2008.

  1. Seeders

    Seeders OT Supporter

    Joined:
    Jul 10, 2007
    Messages:
    3,750
    Likes Received:
    0
    Code:
     
    fp = fopen("sample", "r");
     
    int *num_records;
    fread(num_records,1,1, fp);
    
    printf("%d\n", *num_records);
    
    why does this seg-fault?
     
  2. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    No memory allocated for num_records. Fread has no place to store what it reads in. You need to actually call malloc to dynamically allocate some memory for the info you're reading in. I wouldn't recommend going any farther with that function though, because it looks susceptible to buffer overflow.
     
    Last edited: Jun 2, 2008
  3. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    Your 3rd parameter is incorrect. The third parameter specifies the size of the object that you're trying to load into memory. You want to use sizeof(int) instead of 1.
     
  4. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I hope I never have to work in C. I'm such a soft-handed OOP bitch.
     
  5. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    You can do OOP in C. The first c++ compilers were nothing more than interpreters that converted to C code. Plus, memory management doesn't really have anything to do with OOP or procedural code. Its a fact of life in a lot of industrial strength, speed-sensitive languages.
     
  6. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    Also, check the return value from any file you open.
     
  7. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    How can you do OOP in C? It doesn't support classes. It supports structs, yeah, but they don't have the privacy element critical to OOP.
     
  8. Bruticus

    Bruticus half dead OT Supporter

    Joined:
    Apr 10, 2004
    Messages:
    4,608
    Likes Received:
    0
    Location:
    Melbourne
    You can call different source code files which can contain private functions, which can only be accessed by calling a non private function.

    However, it isn't the same as a class of course, but it can be somewhat simulated.
     
  9. critter783

    critter783 OT Supporter

    Joined:
    Jul 15, 2005
    Messages:
    1,785
    Likes Received:
    0
    You implement the privacy the same way the old c++ to c converters did it. You use file scope to simulate the privacy of struct data members. And its not so much the privacy element that's critical to OOP; its the encapsulation of data and functions that perform operations on the data into a nice little package.
     
  10. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    I didn't say privacy was the linchpin of OOP, just that it was a critical element.
     

Share This Page