corruption of the heap

Reply

Join Date: Jul 2008
Posts: 36
Reputation: lehe is an unknown quantity at this point 
Solved Threads: 0
lehe lehe is offline Offline
Light Poster

corruption of the heap

 
0
  #1
Nov 19th, 2008
Hi,
I got this message "Windows has triggered a breakpoint in inout.exe. This may be due to a corruption of the heap, and indicates a bug in inout.exe or any of the DLLs it has loaded. The output window may have more diagnostic information" while debugging the following program in VC 2005 Express:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void) {
  5.  
  6. char file[]="test.txt";
  7.  
  8. FILE*fp=fopen(file,"w");
  9. for (int i=0;i<200;i++) fprintf(fp,"%d ",255);
  10. fclose(fp);
  11.  
  12. fp=fopen(file,"r");
  13. unsigned char*ptr;
  14. ptr=(unsigned char*)malloc(5*sizeof(unsigned char));
  15. for (int i=0;i<5;i++)fscanf(fp,"%d",ptr+i);
  16. fclose(fp);
  17.  
  18. free(ptr);
  19. return 0;
  20. }
What's the problem with my code? Thanks in advance!
Last edited by Ancient Dragon; Nov 19th, 2008 at 11:40 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,343
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: corruption of the heap

 
0
  #2
Nov 19th, 2008
>>ptr=(unsigned char*)malloc(5*sizeof(unsigned char));

you are only allocating 5 bytes to hold 5 integers! and ptr needs to be an int pointer, not a char pointer. Change that line to
int* ptr= malloc(5*sizeof(unsigned int));
Last edited by Ancient Dragon; Nov 19th, 2008 at 11:44 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 36
Reputation: lehe is an unknown quantity at this point 
Solved Threads: 0
lehe lehe is offline Offline
Light Poster

Re: corruption of the heap

 
0
  #3
Nov 19th, 2008
Thanks.

In fact I just made up the "test.txt" file. In my real case, I have to read in data ( each is an integer and ranges from 0 to 255) from a file into an unsigned char array that are dynamically allocated.

Since an unsigned char can hold a value as large as 255, is there something else wrong? Is it correct to use the conversion specifier "d" with unsigned char in "fscanf"?
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,812
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: corruption of the heap

 
0
  #4
Nov 20th, 2008
Read it in an integer and then place it in your unsigned char array.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 36
Reputation: lehe is an unknown quantity at this point 
Solved Threads: 0
lehe lehe is offline Offline
Light Poster

Re: corruption of the heap

 
0
  #5
Nov 20th, 2008
Hi,Thanks!
I still have the same questions:
Since an unsigned char can hold a value as large as 255, why bother to use integer as intermediate?
Is there something else wrong? Is it correct to use the conversion specifier "d" with unsigned char in "fscanf"? If it is not, which conversion spcifier is proper for unsigned char?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 707
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: corruption of the heap

 
0
  #6
Nov 20th, 2008
>Is it correct to use the conversion specifier "d" with unsigned char in "fscanf"?
Nope. You have no idea how your implementation of fscanf will work. It's especially dangerous to say you'll pass a pointer to a type of one size and actually pass a pointer to a type of a smaller size. By using an intermediate int variable for the input, you at least have more control over when and how the assignments are performed:
  1. for ( int i = 0; i < 5; i++ ) {
  2. int temp;
  3.  
  4. fscanf ( fp, "%d", &temp ); /* Always safe[1] */
  5.  
  6. /* Now you can verify the value before converting! */
  7. if ( 0 <= temp && temp <= 255 )
  8. ptr[i] = temp;
  9. else {
  10. /* Handle the error */
  11. }
  12. }
>If it is not, which conversion spcifier is proper for unsigned char?
In C99, you can say %hhd to read an unsigned char as an integral value rather than a character. In C89 and C++, you're SOL and have to use the intermediate if you want to be safe.

[1] It's not always safe because *scanf doesn't check for arithmetic overflow. For example, if the file contains 999999999999999999999 somewhere, you're borked.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 36
Reputation: lehe is an unknown quantity at this point 
Solved Threads: 0
lehe lehe is offline Offline
Light Poster

Re: corruption of the heap

 
0
  #7
Nov 20th, 2008
Thanks a lot!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC