Re: Malloc code
- From: Robby <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 3 Jan 2008 11:37:01 -0800
--
Best regards
Robert
"Ben Voigt [C++ MVP]" wrote:
"Robby" <Robby@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:1D5C4A5F-B80F-453D-9259-23550768B689@xxxxxxxxxxxxxxxx
Hello Ben,
Yeah, I know this was one of the bugs that I figured out. 176 should of
been
11!
The question is, what are you doing to make sure this same
overflow/wraparound error never bites you again?
[snip] hihihihi! it feels funny that I am doing this snip thingy ! :-)
I will have to just be carefull to not allocate more that 1.5 KB at any
given time.
And also I will have to define some lable to "max" as 1500 and include the
label in the allocation size. I haven't really figured what I will do
exactly, but man this was a burn and a lesson at the same time.
So what the hell am I doing here, inline, bottom posting or what!
[un snip] !
.
--
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
- References:
- Re: Malloc code
- From: Robby
- Re: Malloc code
- From: Ben Voigt [C++ MVP]
- Re: Malloc code
- Prev by Date: Re: MSDN volatile sample
- Next by Date: Re: Single Sourcing GUI - native and HTML
- Previous by thread: Re: Malloc code
- Next by thread: Re: Malloc code
- Index(es):
Relevant Pages
|