link87 0 Posted February 24, 2009 Report Share Posted February 24, 2009 I'm having a couple of issues with creating a static variable that points to a string/array in a function. Initially I was creating and using the variable data like this: static uint8 receive_bit(bool wait_for_start) { static const char *data = "abc123"; static uint8 bit_index = 0; uint8 byte; switch(bit_index) { case 0: ++bit_index; return 1; default: byte = (*data >> (8 - bit_index)) & 0x01; ++bit_index; return byte; case 9: ++data; bit_index = 0; return 0; } } This snippet of code is from a much larger source file. The problem with this particular creation of data is that the buffer that data points to, "abcdef", is getting overwritten by other parts of the program. It seems the compiler has optimized the code and laid other variables, particularly ones used for the passing of arguments to another function, over this buffer. I then changed the definition of data to this: static const char data[7] = "abc123"; And with that comes a nasty linker error: Executing: "C:\Program Files (x86)\SourceBoost\boostlink.pic.exe" "T:\test_dft\dft.obj" "C:\Program Files (x86)\SourceBoost\Lib\libc.pic18.lib" "T:\boost_crclib\boost_crclib.lib" -p "test_dft" -t 18F1320 -O2BoostLink Optimizing Linker Version 6.91 http://www.sourceboost.com Copyright© 2004-2008 Pavel Baranov Copyright© 2004-2008 David Hobday Optimisation level:2 Internal Error:Pseudo instruction mode error:INSTR_W_TO_MEM, in source file:'T:\test_dft\dft.c' line:374 Internal Error:Pseudo instruction mode error:INSTR_W_TO_MEM, in source file:'T:\test_dft\dft.c' line:374 Warning unreferenced functions removed: receive_bit_sample in: T:\test_dft\dft.c change_leds in: T:\test_dft\dft.c Internal Error: Var not found id:0x00000000: in Function 'receive_bit' failure I just tried moving the definition out of the function, so that the same two variable definitions including the static specifier remain but at the global level. The pointer version compiles and does not seem to suffer from the buffer corruption issue that it did in the function. The array version still fails with the same linker error however. Quote Link to post Share on other sites
Pavel 0 Posted February 24, 2009 Report Share Posted February 24, 2009 Thanks for reporting. We'll investigate and try to fix by the next release. Regards, Pavel Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.