Segmentation 11 Fault: Fread struct pointer from file pointer

This is a discussion on Segmentation 11 Fault: Fread struct pointer from file pointer within the C Programming forums, part of the General Programming Boards category; Hi All - have researched this error for several days and tried different ways of referencing the struct pointer - ...

  1. #1
    Registered User
    Join Date
    Jun 2012
    Posts
    7

    Segmentation 11 Fault: Fread struct pointer from file pointer

    Hi All - have researched this error for several days and tried different ways of referencing the struct pointer - though just leads to the same error. Debugging via gdb returns the following error:

    Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000054
    0x00007fff8d8beb8e in __fread ()


    Backtracing has confirmed the following:
    loadWavFile (wav_p=0x0, filename=0x100002710 "/Users/pathandfilename") at wav.c:175
    #3 0x0000000100002612 in main () at main.c:1

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    So you don't find wav_p as an input pointer to be weird in any way?

    Is that where you're supposed to be storing the data you read from 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Code:
    oops!! apologies - I didn't meant to post without the code (cllcked by mistake).  Here is the related code up to where struct pointer is first called by fread: 
    
    WAV_HDR loadWavFile(WAV_HDR *wav_p, FILE **filename)
    {
     char *data_buffer;
        long int lSize;
        //size_t result;
        BYTE *sampleArray;
        WAV_HDR *wav_p, *wavcurrent;
    
        wavcurrent = (WAV_HDR*) malloc (sizeof(WAV_HDR));
    
    
    
    
     //Start with RIFF_HDR
      printf("\nReading RIFF_HDR ChunkID ... ");fflush(stdout);
      fread(wavcurrent->R.ChunkId, sizeof(BYTE), 4, filename);                     // Load first 4 bytes
      printf("%c%c%c%c", wavcurrent->R.ChunkId[0], wavcurrent->R.ChunkId[1], wavcurrent->R.ChunkId[2], wavcurrent->R.ChunkId[3]);fflush(stdout);
    
      if(!strcmp(wav->R.ChunkId, "RIFF"))//!=0)
      {
      printf("\n\nNot a RIFF File. Exiting...\n\n");fflush(stdout);
      exit(EXIT_FAILURE);
      }
    
    etc...}
    
    The function call in main is as follows:
    WAV_HDR *wav;
        FILE *fp;
    
        fp= ATFileOpen(FILE_NAME);
        mallocWavstruct(&wav); //allocate memory
        initWav(wav);
        loadWavFile(wav, fp);
        cleanUp(wav);
    
    Thanks in advance for any help on this

  4. #4
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Hi - thanks for the very quick response - I am new to C.. so am learning all the time. The program initialises a struct and reads the data from a wav file (following error checking). The next step (once this error is fixed), will be to copy the wav file to a buffer to be clipped, then output to a new file. I have managed to get this working without using a struct and functions, but just need to refine the code.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Does this even compile without warnings or errors?

    > WAV_HDR loadWavFile(WAV_HDR *wav_p, FILE **filename)
    What is the point of ** here?

    > loadWavFile(wav, fp);
    This should be &fp to make it consistent with the function declaration.

    Did you really copy/paste from your code editor, or is this some crap from memory with you trying to be flash writing from some crappy iDontCare device.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    You have one too many '*' in your function header for loadWavFile(), 2nd parameter 'filename' which is your file pointer.

  7. #7
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Thanks Salem /Nonoob.. Salem in answer to your questions... yes -there were warnings - which i was going to work through after the error, and my code compiled and ran up to the loadWavFile(wav, &fp); function.. Btw - thanks for the advice on changing the pointer to a reference parameter. What's strange is the following error has been occurring today:
    symbols not found for architecture x86_64 with an Id returned 1 exit status.. so no further processing, errors or warnings occur.. I've tried looking for a fix, but the error disappeared when I copied the code into a new project. Now it's reoccurring!! Salem - the few lines of code are copied from Code Blocks - am sure that I added the code tags.. if that's what you mean? no intention on being flash.. these 2 errors have driven me to post on forums that I'd much rather be reading:-)
    Nonoob - have removed the * - but not able to update on whether it's resolved the first issue, as the x86_64 symbols happening again.. !!

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    You're doing just fine by copy/pasting directly from your code editor, and using code tags

    If you're getting warnings when you compile, and you can't figure them out, then post the relevant code and warnings, and we'll help you get them sorted out.
    Running code with "it's only a warning" is a recipe for disaster at some point.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    Thanks Salem.. much appreciated. Have made progress and worked through the warnings http://im.cprogramming.com/images/smilies/smile.png- so am just left with the segmentation 11 fault - that displays as follows in the debugger:
    Code:
    "Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
    0x0000000000000000 in ?? ()"
    
    
    
    backtracing displays the following in frame 0: #0  0x0000000000000000 in ?? ()
    
    Though  the error isn't giving much away have a feeling it's related to the  following code which involves a null data type in the following  functions: WAV_HDR loadWavFile(WAV_HDR *wav_p, FILE *filename)
    and void initWav(WAV_HDR *wav_p);
    
    The code is as follows:
    //part of the struct initialisation
    //typedefined as:
    
    typedef struct DATA_HDR
    {
        RIFF_ID Subchunk2ID;//NOT 32 bits to store the size of the file
        DWORD Subchunk2Size;
       //void *data[NumChannelsxSampleRatexLengthinSecondsx(BitsPerSample/8)];
        void *Data;
    
    }DATA_HDR;
    
    wav->D.Data=NULL;
    printf("Data_Hdr Data address is:  %d \n", *(int*)&wav->D.Data);
    printf("Initialisation completed.... \n");fflush(stdout);
    
    nested in:
    typedef struct WAV_HDR {
    
        RIFF_HDR R;
        FMT_HDR F;
        DATA_HDR D;
    
    }WAV_HDR;
    
    
    //This code allocates memory to the struct within a function - 
    
    // allocate memory for a struct
    void mallocWavstruct(WAV_HDR *wav_p)
    {
      //WAV_HDR *wav_p;
      // allocate for mystruct
      *wav_p = (WAV_HDR*) malloc (sizeof(WAV_HDR));//dereference the pointer
      //memory allocation to WAV_HDR failure
      if (*wav_p ==NULL)
      {
          printf("ERROR: Out of memory\nFailed to allocate memory from the heap to WAV_HDR....\n");
          exit(EXIT_FAILURE);
      }
          //printf("Memory allocated from the heap to WAV_HDR struct address  %#10.8x \n", (sizeof(*wav_p)));//display the address as unsigned  integer
    
          printf("Memory allocated from the heap to WAV_HDR struct address  %lu \n", (sizeof(*wav_p)));//display the address as unsigned integer
    
    }
    
    //This is the final part of the load wavfile function -WAV_HDR loadWavFile(WAV_HDR *wav_p, FILE *filename)
    
    
    
    //Make space for sound stored in DATA_HDR Data
    
      printf("\nMaking space for sound stored in DATA_HDR Data chunk... ");fflush(stdout);
      data_buffer= (BYTE*) malloc(sizeof(BYTE) * wav->D.Subchunk2Size);//space made for DATA
      printf("\nsReading in DATA_HDR Data chunk... ");fflush(stdout);
      fread(data_buffer, sizeof(BYTE),1,filename);
      printf("%d", *(int*)wav->D.Data);
    
     //load data
      printf("\nReading DATA_HDR Data ... ");fflush(stdout);
      fread(&wav->D.Subchunk2Size, sizeof(DWORD), 1, filename);             // How many bytes of sound data we have?
      printf("%d", wav->D.Subchunk2Size);
    
    
      // Prepare Memory to Hold Sample Data
        printf("\nAttempting to Allocate Memory to Hold Sound ... ");fflush(stdout);
    
      //wavcurrent = (WAV_HDR*) malloc (sizeof(WAV_HDR)* &wav->D.Subchunk2Size);
      //wavBuffer=(WAV_HDR*) malloc(sizeof(WAV_HDR)* &wav->D.Subchunk2Size);
     wavBuffer=(WAV_HDR*) malloc(sizeof(WAV_HDR));
    
      printf("Done.");fflush(stdout);
    
      // Load Data
    
      printf("\nLoading Sound Samples ... ");fflush(stdout);
      fread(wavBuffer, sizeof(BYTE), 1, filename);         // Read Samples into Array
      printf("Done.");fflush(stdout);
    
      return *wav;
      fclose(*filename);
    By the way I amended the function call to loadwav: loadWavFile(wav, &fp);

    Much appreciated

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Read your book on memory allocation and pointers (again).

    > *wav_p = (WAV_HDR*) malloc (sizeof(WAV_HDR));//dereference the pointer
    If this doesn't get you a warning, then you need another compiler.
    Like I said previously, do NOT run code with warnings still present - it only leads to random crashes you have no clue about.

    > fread(&wav->D.Subchunk2Size, sizeof(DWORD), 1, filename);
    Rename 'filename' to be something more appropriate.
    Most people just use fp (for 'file pointer') when there is just one file being processed at the moment.
    FILE *fp = fopen("filename.txt","r");
    The filename is the first parameter to fopen, NOT it's result.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jun 2012
    Posts
    7

    Smile

    Thanks Salem- the segmentation 11 fault disappeared after updating the dynamic memory assignment to the struct - i.e.: WAV_HDR *wav = malloc(sizeof(WAV_HDR));
    I have a void data type in my WAV_HDR struct, which is nested in DATA_HDR as: void *Data. A warning is being generated when attempting to assign a pointer array to store the data held in void *Data (accessed by the struct pointer wav->D.Data). Warning as follows: assignment makes integer from pointer without a cast..

    I have a function that opens a file btw: FILE* ATFileOpen(const char *filename);
    filename is assigned the value of static variable FILE_NAME i.e. static char *FILE_NAME ="/Users
    The following functions are called from main:

    Code:
    int main()
    
    {
        WAV_HDR *wav;
        FILE *fp;
    
        fp= ATFileOpen(FILE_NAME);
        initWav(wav);
        loadWavFile(wav, fp);
    
    
        //cleanUp(wav*);
        //free(wav);
    
        return 0;
    
    
    //Struct definition:
    
    typedef char  BYTE;
    typedef short WORD;
    typedef int DWORD;
    typedef BYTE RIFF_ID[4];
    
    
    
    typedef struct RIFF_HDR
    {
        RIFF_ID ChunkId; //hold 'RIFF'
        DWORD ChunkSize;
        RIFF_ID Format;
    } RIFF_HDR;
    
    
    typedef struct FMT_HDR
    {
        RIFF_ID Subchunk1ID; //NOT 32 bits to store the size of the file
        DWORD Subchunk1Size;
        WORD AudioFormat, NumChannels;
        DWORD SampleRate, ByteRate;
        WORD  BlockAlign, BitsPerSample;
    
    }FMT_HDR;
    
    
    typedef struct DATA_HDR
    {
        RIFF_ID Subchunk2ID;//NOT 32 bits to store the size of the file
        DWORD Subchunk2Size;
       //void *data[NumChannelsxSampleRatexLengthinSecondsx(BitsPerSample/8)];
        void *Data;
    
    }DATA_HDR;
    
    
    typedef struct WAV_HDR {
    
        RIFF_HDR R;
        FMT_HDR F;
        DATA_HDR D;
    
    }WAV_HDR;
    
    
    //start of load function:
    
    WAV_HDR loadWavFile(WAV_HDR *wav_p, FILE *filename)
    {
    
       WAV_HDR *wav;
       BYTE *dataBuffer;
       int i;
       dataBuffer[i] = malloc(sizeof(BYTE) * wav->D.Subchunk2Size);//this is the declaration that is causing the warning 
    
    //The aim is to load the data chunk into dataBuffer for editing and then create an output file
    
     dataBuffer = (BYTE*) malloc(sizeof(BYTE)* wav->D.Subchunk2Size); //this code assigns memory to dataBuffer - using the datasize read from the input file into the corresponding struct member
    
    
    
    }
    
    I am also getting an outofbounds exception when attempting to free pointers -i.e. with a value of 0x1
    #3  0x0000000100002312 in cleanUp (wav_p=0x7fff5fbffb08) at wav.c:258
    258         free(&wav->R.ChunkId[1]);
    
    The code in question is as follows:
    
    void cleanUp(WAV_HDR *wav_p)
    {
        WAV_HDR *wav;
        printf("Wav file processing ended.. freeing up memory on the heap....");
        //RIFF_HDR
        free(&wav->R.ChunkId[0]);
        free(&wav->R.ChunkId[1]);
        free(&wav->R.ChunkId[2]);
        free(&wav->R.ChunkId[3]);
    etc
    
    Thanks for getting back

  12. #12
    Registered User
    Join Date
    Jun 2012
    Posts
    7
    This seems to have resolved the cast warning above:

    Code:
    WAV_HDR loadWavFile(WAV_HDR *wav_p, FILE *filename)
    {
    
       WAV_HDR *wav;
       BYTE *dataBuffer = malloc(sizeof(BYTE) * wav->D.Subchunk2Size + 1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2011, 02:21 AM
  2. Segmentation Fault With pointer to pointer strcpy
    By touchy in forum C++ Programming
    Replies: 3
    Last Post: 03-09-2011, 12:35 AM
  3. Big structure + file pointer = segmentation fault
    By ERJuanca in forum C Programming
    Replies: 6
    Last Post: 03-02-2010, 05:46 PM
  4. pointer - segmentation fault
    By thescratchy in forum C Programming
    Replies: 6
    Last Post: 02-23-2010, 04:17 PM
  5. Segmentation fault with a file pointer?
    By Matt13 in forum C Programming
    Replies: 14
    Last Post: 07-31-2004, 06:53 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21