'Bus Error' using fread

This is a discussion on 'Bus Error' using fread within the C++ Programming forums, part of the General Programming Boards category; Hi, I have been struggling with this one for many hours now. Code compiles fine but when I run it ...

  1. #1
    Registered User no0b's Avatar
    Join Date
    Jan 2009
    Posts
    2

    'Bus Error' using fread

    Hi,

    I have been struggling with this one for many hours now. Code compiles fine but when I run it I get a 'bus error'. No more details are given by the program. I have narrowed it down to when I call fread.
    Code:
    #include <math.h>
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 
    using namespace std;
    
    char *wave;
    char *data;
    char *riff;
    unsigned long int length;
    unsigned int fs, numchannel, bytes, byterate, chunksize, vectsize;
    short nframes, fstart, fend, comp, bits;
    int toggle = 0;
    int *l, *r;
    
    ...........
    int main(){
    
    
    //open .wav file and verify header works fine problem occurs below
    
    ...
            int *r;
            int *l;
    	int left;
    	int right;
    	for(int i = 0; i <=50; i++){
    		
    		if(toggle%2 == 0){
    			fread(l, 4, 1, audio);
    			left = *l;
    			channelL.push_back(left);
    			cout <<channelL.at(left)<<endl;
    			
    		}
    		if (toggle%2 == 1){
    			fread(r, bytes, 1, audio);
    			right = *r;
    			channelR.push_back(right);
    			cout <<channelR.at(right)<<endl;
    			
    		}
    		toggle = toggle++;
    	}
    }
    It works fine if I just replace left and right with k and j and increment the counters. The problem is when I call fread. I am sure it is something so obvious.

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    variable l is not initialized pointer,so it points nowhere. And you still hope to sucessfully write to this unknown location? you extend your luck too greately with this hope.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User no0b's Avatar
    Join Date
    Jan 2009
    Posts
    2
    Thanks. I guess I didn't fully understand how to use pointers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problems or memory problems
    By Lechuza in forum C Programming
    Replies: 1
    Last Post: 03-22-2009, 12:45 PM
  2. Another link from Microsoft about bug in fread
    By vart in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-06-2008, 11:56 AM
  3. How to load pixels of BMP into an array
    By brconner in forum Windows Programming
    Replies: 10
    Last Post: 06-02-2007, 04:30 AM
  4. Why is fread sometimes taking so long?
    By manugarciac in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2007, 11:25 PM
  5. fread: Bus error
    By fnoyan in forum Linux Programming
    Replies: 4
    Last Post: 08-20-2004, 10:30 AM

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