Jump to content

Problem With Function-static String/array Variables?

Recommended Posts

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:
		return 1;

		byte = (*data >> (8 - bit_index)) & 0x01;
		return byte;

	case 9:
		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 -O2

BoostLink Optimizing Linker Version 6.91


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'





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.

Link to post
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...