Thread: please help on memory allocation

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    please help on memory allocation

    Hi all I have trouble in allocating my memory.
    I am not quit sure what's going on there and need your help.
    My program is to read a file that was combined of serveral image files(called Wad file)
    Each of combined image file has header :

    file size: 4 bytes
    file name: 28 bytes
    buffer

    and my code is bellow:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef unsigned int UINT4;
    typedef unsigned char UCHAR;
    const int file_name_length	= 28;
    
    
    
    typedef struct {
      UINT4 file_size;
      char file_name[file_name_length];
      UCHAR *file_buffer;
      
    } WAD;   //struture of file
    
    WAD *WadPtr; // poter of struture
    int file_count;
    
    static void readWAD(char *filename) {
    
    	int count =0,i, j=0;
    	FILE *fp, *out;
    
       if (!(fp=fopen(filename,"rb")))     //read file
    	{
    		printf ("check your ass bud!\n");
    		exit(1);
    	}
    
    	if (!(out=fopen("report","wb")))    //test 
    	{
    		printf ("check your ass bud!\n");
    		exit(1);
    	}
    
    	while(!feof(fp)){
    		WadPtr = (WAD*)realloc(WadPtr, sizeof(WAD)* (j+1));        // reallocate poniter every one image
    		fread((UINT4* )&WadPtr[count].file_size, sizeof(UINT4), 1, fp);  // read file size
    		printf("File size %d: ", WadPtr[count].file_size);
    		if(WadPtr[count].file_size < 0) {
    		printf ("syzstem freaking out\n!");
    		exit(1);
    		}	
    		fread((char* )WadPtr[count].file_name, sizeof(char), file_name_length, fp); // read file name
    		printf("  %s \n", WadPtr[count].file_name);
    	
    
    		WadPtr[count].file_buffer = (UCHAR *)malloc(sizeof(UCHAR)*WadPtr[count].file_size);   // read buffer
    		// problem is here....how should i deal with memory here?
    
    		fread((void *)WadPtr[count].file_buffer, sizeof(UCHAR), WadPtr[count].file_size, fp);
    		
    		count++;
    		j++;
    		
    	}
    	fwrite((void *)WadPtr[count].file_buffer, sizeof(UCHAR), WadPtr[count].file_size, out);
    	free(WadPtr);
    
    	 if(fp != stdin)
    	   fclose(fp);
    	 fclose(out);
         file_count=count;
    } 
    
    int main (int argc, char *argv[])
    {
    	
    	if (argc != 2)
        {
          printf ("Usage: %s <File name> \n", argv[0]);
          exit (1);
        }
    
    	printf ("argv[1]: %s  \n", argv[1]);	
        readWAD(argv[1]);
    
    }
    and system crashed when loop is finish
    can anyone help me please!

  2. #2
    Registered User cbastard's Avatar
    Join Date
    Jul 2005
    Location
    India
    Posts
    167
    Quote Originally Posted by acfror
    WadPtr[count].file_buffer = (UCHAR *)malloc(sizeof(UCHAR)*WadPtr[count].file_size); // read buffer
    // problem is here....how should i deal with memory here?

    fread((void *)WadPtr[count].file_buffer, sizeof(UCHAR), WadPtr[count].file_size, fp);
    You are casting malloc to UCHAR*.then you are using it to read in binary.read faq

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    The first parameter to realloc() is a pointer that was previously returned by malloc(), calloc(), realloc() or a NULL pointer. What is the value of WadPtr the first time that function is entered? I don't see where WadPtr was ever initialized to anything, including NULL.

    Second point: WadPtr is being freed before the function ends. So why is it a global variable? It should be local to that function.

    Third: make readWAD() return an int (the record count) instead of using a global file_count variable. Whatever function calls readWAD() can easily grab the file count that way.

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by Ancient Dragon
    The first parameter to realloc() is a pointer that was previously returned by malloc(), calloc(), realloc() or a NULL pointer. What is the value of WadPtr the first time that function is entered? I don't see where WadPtr was ever initialized to anything, including NULL.
    WadPtr is declared outside of any function, so it is of static storage duration, it's initialised to a NULL pointer on startup.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > char file_name[file_name_length];
    This isn't C

    2. Remove all those casts

    3. while(!feof(fp)){
    Read the FAQ
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 11:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 05:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 08:36 AM