Thread: Need help with allocating an array with calloc

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    Need help with allocating an array with calloc

    I have a binary file that has a large number of records in it. All I need to do is open the file and read it into memory. I am new to programming so bare with me. When my program reaches the fread statement, I get a runtime error that says “EXC_BAD_ACCESS”. I'm using Xcode on GCC, configured for ANSI-C. Can someone help me with my logic?

    Code:
    extern int read_recs_from_bin( char * binfile, data_p_t * zips_ptr, int * num_recs )
    {
    	int count;
    	int inx;
    	int rcode = TRUE;
    	
    	FILE *file   = NULL;
    	errno = 0;
    	
    	if ( (file = fopen( binfile, "rb" )) == NULL )
    		fprintf(stderr,
    				"Open file error: %d, \"%s\"\n",
    				errno,
    				strerror( errno )
    				);
    	else
    	{
    		fseek( file, 0, SEEK_END );
    		count = ftell( file ) / sizeof(ZIPS_data_p_t);
    		fseek( file, 0, SEEK_SET );
    		
    
    		file = calloc(count, sizeof(ZIPS_data_p_t));
    		if (file == NULL)
    			exit (1);
    		for (inx = 0; inx < count; inx++)
    		{
    			fread(zips_ptr, sizeof(data_t), 1, &file[inx]);
    		}
    		free(file);
    
    	}
    
    	return rcode;
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    You're overwriting your file handle with your call to calloc(). Presumably you wanted to assign the result of calloc() to another pointer, but I'm really not sure which. You have types all over the place: ZIPS_data_p_t, data_t, data_p_t... I don't know what they all mean.

    I can only guess that data_p_t is a typedef'd pointer (which I think is bad style unless you're trying to create an opaque type); if so, presumably you'd want to assign the result of calloc() to *zips_ptr. But, I don't know, since I can't see the type definitions.

    If you're not sure why assigning calloc()'s return value to file is wrong, I suggest starting with a simpler program. This is too complex if you don't yet understand file I/O.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Just do that:

    Code:
    else {
        fseek(file, 0, SEEK_END);
        count = ftell(file) / sizeof(data_p_t);
        fseek(file, 0, SEEK_SET);
    
        *zips_ptr = calloc(count, sizeof(ZIPS_data_p_t));    /* Not file. I think you should also use malloc. */
    
        for (i = 0; i < count; i++)
            /* Each call to fread will move the file position automatically so you're reading something different each time */
            fread(zips_ptr[index], sizeof(data_t), 1, file);
    }
    I think you're mixing up file and zips_ptr.

    @above: I agree, you should really use "data_t *" instead of "data_p_t". You don't say FILE_p_t, do you?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Dynamicly allocating an array of pointers
    By kzar in forum C Programming
    Replies: 2
    Last Post: 05-05-2005, 04:50 PM
  3. dynamically allocating a 2dim array
    By agerealm in forum C++ Programming
    Replies: 14
    Last Post: 03-10-2004, 02:40 PM
  4. allocating memory for char* array
    By creeping_death in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2003, 04:49 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM