Re: Malloc code

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




--
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.






.



Relevant Pages

  • Re: Arraym malloc() and free() question
    ... > I have a question regarding malloc and free. ... int main ... this produces clearly a memory leak as the reference to the memoty ... The second time I allocate ...
    (comp.lang.c)
  • Re: Huge memory allocation
    ... I'm in a need to allocate huge memory block of size 50kb. ... How can i use malloc() for this purpose, ... the argument to mallocis a size_t, not an int. ... undefined if a decimal integer constant has a value larger than ...
    (comp.lang.c)
  • Re: When to check the return value of malloc
    ... the size is that it is not a general replacement. ... calling this with 0x14000 and since int is 16 bit it will allocate 0x4000! ... If it was 32 bits large, why wouldn't I be allowed to allocate 3GiB? ... you could argue that a malloc() replacement should take an argument ...
    (comp.lang.c)
  • Re: Can auto_ptr<> be used with void and malloc()/free()
    ... safe to allocate with mallocand then free with delete. ... Whenever you fine yourself reaching for malloc in C++, ... same thing with the data structure, ...
    (microsoft.public.vc.language)
  • Re: allocating a 3D array
    ... > This is the first I have to allocate a 3D array somehow my imagination ... > void world_new(int x, int y, int z) ... you don't have to count stars. ... > (No need to point out the lack of malloc error checks. ...
    (comp.lang.c)