Heap Corruption Error help

This is a discussion on Heap Corruption Error help within the C Programming forums, part of the General Programming Boards category; Im making a program that will take a input file specified on the command line through argv[1] and a output ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    35

    Heap Corruption Error help

    Im making a program that will take a input file specified on the command line through argv[1] and a output file also specified on the command line argv[2], basically im just printing the information from the input file to the output file in ascii format zero padded, Im using fread to read the entire file into a dynamically allocated block of memory, I use fseek and ftell to get the size of the input file, and then use a loop to print the values to the output file, but somehow when I increment the pointer to move to the next byte in the allocated memory space. it gives me the _CrtIsValidHeapPointer(pUserData), Debug Assertion Error!

    Here is the following code for my program, please help me out in anyway possible, thank you! It still is correctly writing to the output file the values from the input file, but it's giving me that error.

    Code:
    #include
    <stdio.h>
    
    #include
    <stdlib.h>
    
    
    int
     main(int argc,char* argv[])
    
    {
    
    	
    int byteCount=1;
    
    	
    char* memorySpace;
    
    	
    char* memoryPosition;
    
    	
    long lSize;
    
    	
    char buffer[300];
    
    	FILE* ipf;
    
    	FILE* opf;
    
    
    	ipf=fopen(argv[1],
    "r");
    
    	opf=fopen(argv[2],
    "w");
    
    	fseek( ipf,0, SEEK_END );
    
    	lSize=ftell(ipf);
    
    	rewind(ipf);
    
    	memorySpace=(
    char*)malloc(lSize);
    
    	fread(memorySpace,1,lSize,ipf);
    
    	
    while (byteCount!=lSize)
    
    	{
    
    		fprintf(opf,
    "0%d ",*memorySpace);
    
    	    ++byteCount;
    
    		++memorySpace;
    
    		
    if(byteCount%10==0)
    
    		{
    
    			fprintf(opf,
    "\n");
    
    		}
    
    		
    
    	}
    
    	    
    
    	fclose(ipf);
    
    	fclose(opf);
    
    	
    
    
     
    
    		
    
    	
    
    	free(memorySpace);
    
    
    	
    return 0;
    
    
    }
    

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,437
    I'm tempted to say it from your atrocious code formatting and all the unnecessary white space, but we all know that's not the real reason:
    Code:
    memorySpace = (char *) malloc(lSize);  // memorySpace points to the start of the allocated block, what malloc returned
    ...
    ++memorySpace;  // you make memorySpace point elsewhere
    ...
    free(memorySpace);  // you are trying to free something other than the pointer malloc returned, hence the corruption
    You can't lose the pointer malloc returned, that's the only thing free will accept. Set your memoryPosition variable to the memorySpace, then print and increment memoryPosition, then free memorySpace when you're done.

    Also, no need to cast malloc. And you should check to make sure fopen works, and exit with an error if not, before trying to do anything with ipf or opf.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    hey thanks for the reply, i tried doing the following line, but it doesn't like it for some reason, it says value of type char** cannot be assigned entity of type char

    [/CODE]
    *memoryPosition=&memorySpace;
    [CODE]

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,437
    memoryPosition is a char *, so is memorySpace, so just assign them like so:
    Code:
    memoryPosition = memorySpace;  // make memoryPosition point to the start of memorySpace
    The way you wrote it, *memoryPosition is the char stored at the (uninitialized) address in memoryPosition, and &memorySpace is the address of memorySpace, i.e. a char **. Hence the error message.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    35
    Thanks Anduril, That really helped, I appreciate the quick replies, you solved my problem, really appreciate that man!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. This may be due to a corruption of the heap
    By Cobs in forum C++ Programming
    Replies: 3
    Last Post: 01-13-2011, 08:53 PM
  2. Heap Corruption Error
    By ralocorvette in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2010, 01:52 PM
  3. Possible Heap Corruption
    By Michael5978 in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2010, 04:09 PM
  4. Heap corruption Detected Error
    By mrsirpoopsalot in forum C++ Programming
    Replies: 2
    Last Post: 10-26-2009, 03:59 PM
  5. This may be due to a corruption of the heap
    By krishnampkkm in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2009, 03:19 AM

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