Hi all. I'm working on a pretty large project. Everything's going well except that I'm having a very strange problem trying to put a chunk of code into a separate function from main. I am communicating with a CrystalFontz-brand LCD screen + keypad unit which connects to the computer via USB. It communicates in a pretty simple packet/handshaking protocol, and I have defined a struct which generally describes a single packet of data to or from the unit:
1 2 3 4 5
|
struct crystal_packet {
unsigned char type;
unsigned char length;
unsigned char data[30];
}
|
Now, there are many points in time at which I need to wait for a keypress from the user. Getting input from the device is a several-step process, including using a CRC algorithm to verify data validity. So obviously I want to functionalize the process of getting a key from the user, into something called, say, crystal_keystroke(). Here is the process:
1 2 3 4 5 6
|
crystal_packet response;
response.type = 0;
while (response.type != 128) {
if (!crystal_get(response)) response.type = 0;
}
int keypress = response.data[0];
|
Most of this snippet isn't relevant to my problem -- it's just the logistics of communicating with the device -- suffice it to say that at the end of this snippet, a key on the CrystalFontz has been pressed and "keypress" contains an indicator telling me which one.
Now, this code works without a hitch *if* it's sitting right in main(). The problem is that if I try to put this *exact* same code into a separate function called crystal_keystroke(), change the last line to return response.data[0] as the result, and call "int keypress = crystal_keystroke()" from main, the program craps out with a backtrace and the complaint "Stack smashing detected." This seems to occur on the "return" line of crystal_keystroke(). I've read a little bit about what this means but I don't quite understand it, or why it's happening here. Apparently it has to do with addressing memory illegally (similar to a segfault, but somehow different), but I don't see any way I'm doing that.
Oddly (in my perception), I discovered that if I instantiate the crystal_packet struct on main's stack instead of on crystal_keystroke's stack, and pass it by reference to crystal_keystroke, everything works dandily. I.e., if I write the function like this:
1 2 3 4 5 6 7
|
int crystal_keystroke (crystal_packet & response) {
response.type = 0;
while (response.type != 128) {
if (!crystal_get(response)) response.type = 0;
}
return response.data[0];
}
|
And call it like this, from main():
1 2
|
crystal_packet response;
int keystroke = crystal_packet(response);
|
Then it works without a hitch. The only thing I am trying to do differently from that is to instantiate the crystal_packet struct on crystal_keystroke's stack instead of main's stack, so that I don't have to keep a struct in memory on main's stack that I'm not using most of the time (and make main's code a bit bulkier). Why shouldn't I be able to do it that way? An additional bit of intrigue: I already have a *lot* of other functions, separate from main, that I'm using the same way as I'm trying to use crystal_keystroke(), that I've concocted for interfacing with the device in various ways. *They* instantiate crystal_packets on *their* stacks without a complaint...and I can run them as many times as I want! But a single call to crystal_keystroke() and everything explodes. Why can I not instantiate one on the stack of this function in particular?
If anyone can provide any help or insight it would be much appreciated. TIA!