Re: Global Variables Being Overwritten



"jgurtner" <jgurtner@xxxxxxxxxxx> wrote in message
news:1148964547.983744.326300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Is there a better way of doing this:

LCDWrite("This is a test");

void LCDWrite(char *buffer)
{
while(*buffer) /* loop until buffer is empty */
{
PortC = *buffer; /* write data to LCD data port */
LCDEnable(); /* clock the LCD to accept data */
buffer++; /* increment the pointer to the next character */
}
}

This works fine but it seems that if I use this function a too many
times it starts to overwrite some of my global variables I have
declared in "main" for an interrupt routine I am using.

The above was written in C with Code Warrior for use on an 8 bit
Freescale 68HC08 microcontroller.

It should be fine - unless there's something in LCDEnable() that's doing it.
The code you're showing has precisely one variable that gets written to
(char *buffer), and that's an automatic variable. I'm also assuming that the
definition of "PortC" has no side-effects.

Some suggestions:
- Check your stack placement. Make sure it's not colliding with data
space.
- Look for instances of incrementing write pointers (the above is an
incrementing read pointer). Be sure you're ending where you think you are.
- If you have a debugger, watch the variables that are getting hit to
narrow down what's clobbering them.

In other circumstances, I'd also say "avoid global variables", but a bug
like the one you describe is a bug - it'd still break something.

HTH,

Steve
http://www.fivetrees.com


.



Relevant Pages

  • Buffer pair for lexical analysis of raw binary data
    ... the "buffer pair" scheme for lexical analysis described on pp. ... "Two pointers to the input buffer are maintained. ... of characters between the two pointers is the current lexeme. ... or else by recoding it all myself in Python). ...
    (comp.lang.python)
  • Re: Buffer overflows and asctime()
    ... to contain a "bug" as others are not. ... standard the year member receives a maximum value. ... will overflow its buffer if confronted with valid inputs. ... So you still do not understand "undefined behavior". ...
    (comp.std.c)
  • Re: [Full-disclosure] DNS TXT Record Parsing Bug in LibSPF2z
    ... A relatively common bug parsing TXT records delivered over DNS, ... filters that protect your systems use LibSPF2. ... rdlen byte buffer. ...
    (Full-Disclosure)
  • Bug analysis
    ... char *ReadTextFile ... the fgets function fills our buffer with a line or 119 ... The reallocation asks for "len" more characters, ... this bug can very well go completely undetected in many occasions giving ...
    (comp.lang.c)
  • Re: How to override basic_streambuf::seekoff?
    ... It shouldn't be undefined behaviour. ... If the error is unrecoverable, throw an exception (which will cause the owning stream to set badbit) - in this case, you should make sure that at least the destructor will work and deallocate resources correctly. ... The failure required by the standard in this case is not a failure of the buffer, it is a failure of the interface that is unable to return the return value using the channel provided. ... If I understand your position correctly, it is conformant to advance both pointers by the argument value and return -1, isn't it? ...
    (microsoft.public.vc.stl)