Codewarrior / c problem?

I hope someone can tell me what is going wrong, im fairly new to both codewarrior/c and the HCS08 family.

I'm making a LED driver on a HCS08GT32A.

I have the following method:

BUFFER_LENGTH = 4 BUFFER_HEIGHT = 10

void display_string_still(const unsigned char *str0, const unsigned char

*str, const unsigned long int time)

{ unsigned char disp_con[4][6] = {0}; unsigned char disp_buffer[BUFFER_LENGTH][BUFFER_HEIGHT] ={0}; unsigned char *stillptr; unsigned char *tempptr; unsigned char i; unsigned char j; unsigned char x; unsigned char y; unsigned long int time_cnt; unsigned int length_of_incomming_str; unsigned char *charptr;

length_of_incomming_str = strlen(str); for(j = 0; j < length_of_incomming_str; j++) { charptr = (unsigned char *)ascii_to_font(str[j]); for(i = 0; i < BUFFER_HEIGHT; i++) { disp_buffer[j][i] = charptr[i]; } } //fixes byte0 and byte1 for(y = 0; y < BUFFER_HEIGHT; y++) { disp_buffer[0][y] = disp_buffer[0][y] >> 1; }

for(y = 0; y < BUFFER_HEIGHT; y++) { if(disp_buffer[1][y] & 0b10000000) // MSB from byte1 becomes LSB in byte0

{ disp_buffer[0][y] = disp_buffer[0][y] | (1

Reply to
JesperAagaard
Loading thread data ...

You have a potential buffer overrun here. j *must* be trapped if it's greater than or equal to BUFFER_LENGTH but length_of_incomming_str could be (is?) larger.

Oh, and you're not using The One True Brace Style. ;-)

--
Rich Webb     Norfolk, VA
Reply to
Rich Webb

The arrays are local in the function, so they are allocated on stack. How large has you configured the stack size? Try add a "static":

static unsigned char disp_con[4][6] = {0};

Then it will be allocated somewhere in the RAM (but it is not local anymore to the function, but same as if you woul declare it as a global variable).

But looks like the disp_con variable is not used anyway in the rest of your code, so you can delete this.

--
Frank Buss, fb@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
Reply to
Frank Buss

It looks like you are using a total of 77 bytes of stack for local variables. I would be sure your stack can handle that much allocation.

Maybe I am not up on all my C concepts, but in your first two arrays, are you trying to zero them out using the "= {0};"? If those arrays were either declared global or static (which is preferred over global), they would be zeroed by the compiler once on startup, unless you specifically tell it not to do that. In your code, are you expecting the arrays to be automatically cleared every time the function is entered? I don't think it does that, if that is what you want.

Lou

Reply to
Mr. C

In that case you would be thinking incorrectly. The C compiler is definitely required to perform all explicit initialization of automatic variables every time their enclosing block begins executing.

You may be mixing this up with a closely related case: it's not required to initialize variables that have no explicit initializer. I.e. a variable

{ int random_garbage; /* ... */ }

will contain exactly that.

Reply to
Hans-Bernhard Bröker

It depends on the compiler setting. "Full ANSI C" mode in the Codewarrior HC08 compiler. You can set it to do minimal initialization before jumping to main(), only setting the stack pointer, and maybe a few other tiny things, but it won't zero global variables.

Reply to
Ben Bradley

I meant to type more there:

There are forums at

formatting link
that appear to be excellent for technical questions such as this.

Reply to
Ben Bradley

Yes, you are correct. I use it so little that I had forgotten that. I almost always initialize local variables in the code, so I had forgotten initializing in that way. Thanks.

Lou

Reply to
Mr. C

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.