Re: Malloc code
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 1 Jan 2008 18:51:02 -0800
Hello Ben,
Yeah, I know this was one of the bugs that I figured out. 176 should of been
11!
--
Best regards
Robert
"Ben Voigt [C++ MVP]" wrote:
.========================================TCP.h
struct MCB{
int ENDOFCHAR; //:TRUE:=End of char / :FALSE:=Valid char
int A0; //LSB OF MESSAGE ADDRESS IN PAR FLASH
int A1; //MIDSB OF MESSAGE ADDRESS IN PAR FLASH
int A2; //MSB OF MESSAGE ADDRESS IN PAR FLASH
int LEADING_PIX; //LEADING SPACES, BEFORE CHARACTER
int TRAILING_PIX; //TRAILLING SPACES, AFTER CHARACTER
int CHARSPACING; //SPACES BETWEEN CHARACTERS
int CHAR_TRSP; //CHARACTER TRANSPARENCY
int mCHAR_BCOLOR; //MSB OF BACK COLOR
int lCHAR_BCOLOR; //LSB OF BACK COLOR
int mTEXT_COLOR; //MSB OF TEXT COLOR
int lTEXT_COLOR; //LSB OF TEXT COLOR
};
[snip]
pMCB1 = malloc(176*(sizeof(struct MCB)));
Wait a sec! I thought you're using a microcontroller with limited RAM. You
also said that for your compiler, int is 8 bits. Please show the
declaration of malloc from your compiler's malloc.h.
Your structure is up to 12 ints, so sizeof (struct MCB) is at least 12. 176
* 12 = 2112. How much RAM do you have again? BTW 2112 modulo 256 is 64, so
if your malloc takes an argument of type int and your int is 8 bits then you
are getting only 64 bytes allocated. This wraparound explains why the
malloc doesn't fail outright and trigger your error handling code, but all
elements beyond the first five are outside the allocated buffer.
- Follow-Ups:
- Re: Malloc code
- From: Ben Voigt [C++ MVP]
- Re: Malloc code
- Prev by Date: Re: VS2005, x86/x64 build questions
- Next by Date: Re: Malloc code
- Previous by thread: Re: VS2005, x86/x64 build questions
- Next by thread: Re: Malloc code
- Index(es):
Relevant Pages
|