Re: Malloc code

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



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.



.



Relevant Pages

  • Re: String manipulation program not returning expected output
    ... But I'm getting others with jumbled characters. ... int m=0; ... There are two errors in the malloc of smaller. ... need an additional character for the nul. ...
    (comp.lang.c)
  • Re: Cipher Lab / Syntech
    ... this is not Google Groups. ... The "simple" library without malloc is needed because I was told ... void foo { ... int dprintf { ...
    (comp.lang.c)
  • Re: why still use C?
    ... I was talking about the malloc expression, ... >>the assignment expression. ... >>enum parameter is not an error in C, but in my coding style it's a mistake). ... I took it you meant "int where an enum" is expected, ...
    (comp.lang.c)
  • Re: malloc + 4??
    ... >>information into the malloc is solid. ... The variable inSize is a plain int and has ... > the loop will never terminate. ... > yet also return the special marker value EOF. ...
    (comp.lang.c)
  • Re: Unknown function
    ... How can the function call of 'malloc' work at all if it is unknown? ... I thought that each function that is unknown to the compiler at a specific ... returns an int. ... problem since malloc actually takes size_t and returns void *. ...
    (comp.lang.c)