|
|
View previous topic :: View next topic |
Author |
Message |
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
Problem with constant strings |
Posted: Thu Jun 14, 2012 10:56 am |
|
|
Hi all,
I'm having some trouble with constant strings, I'm trying the c code as found on this page:
http://www.ccsinfo.com/content.php?page=compiler-features#pconst
I have put the strings definition in the .c file just before the main() function and the loop into the main() function. My only change has been to make printf into fprintf.
Code: |
const char *strings[] =
{
"HELLO",
"WORLD",
"CONST",
"STRINGS"
};
const char *ptr;
int i;
while (i = 0; i < (sizeof(strings) / sizeof(const char *)); i++)
{
ptr = strings[i];
fprintf(RS232A, "%s", ptr);
} |
To start with this doesn't compile, the declaration of the pointer seems right to me (pointer to constant char*) but it expects an = after the declaration like the pointer is constant (it shouldn't be).
If you change the pointer to just char *ptr; then the loop fails to compile. To simplify it all I tried this:
Code: | const char *ptr = strings[2];
fprintf(RS232A, "%s", ptr); |
But I just get classic bad ptr gibberish.
All suggestions would be most welcome, I am clearly misunderstanding something!
Thanks very much
Ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Thu Jun 14, 2012 11:24 am |
|
|
Compiler version, chip?????.
Change the word 'const', to 'rom'.
CCS has two types of rom constant string. The older type (which is that standard), by default uses the 'const' keyword. The later type, uses the rom keyword. The earlier type is simpler to handle, _but does not support pointers_. This is the only type available on some PIC's (early PIC16's etc.). Here there is no hardware support for this, and const arrays are 'simulated' by generating a program that returns the characters when called with an index. If your chip is an old one, then this may apply.
You can swap so that 'const' behaves as 'rom', at a cost in program size, with the keyline '#DEVICE CONST=ROM'.
There is also a third way of handling 'const'. In ANSI C 'const' does not imply storage in ROM, instead just meaning the bytes are protected from writing. You can switch to this mode with the keyline '#DEVICE PASS_STRINGS=IN_RAM'. This costs a significant amount of RAM, copying the strings to RAM before use, but then allowing const to support pointers.
I'd suspect the code you are using has this option.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Fri Jun 15, 2012 5:33 am |
|
|
hi guys,
Thanks for the responses, I had a look through the stuff and revised my code to this:
Code: |
char rom *strings[] =
{
"HE",
"WO",
"CON",
"STRINGS"
};
fprintf(RS232A, "Ed: %s", strings[2]); |
This still doesn't work correctly and neither does the simpler:
Code: | char rom string[] = "hello";
char rom *ptr; // ptr is stored in RAM, is a pointer to ROM
ptr = string;
fprintf(RS232A, "Ed: %s", ptr); |
I'm using compiler 4.132 for an 18F87K22. It seems like I missing something fundamental here I still get streams of incorrect characters from the fprintf call. I haven't defined any of the copy in ram compiler settings or anything like that.
Cheers
ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jun 15, 2012 3:59 pm |
|
|
I think there may be a problem with rom pointers that is version
dependent. I don't have a work-around for it right now. I'm going
to investigate it some more. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 17, 2012 2:52 pm |
|
|
Rather than me spending more time trying to make 'rom' work, I just
made some semblance of your original code work. In MPLAB simulator,
the code shown below displays the following:
Quote: |
HELLO
WORLD
CONST
STRINGS
|
This is with compiler vs. 4.132.
Code: |
#include <18F87K22.h>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT
#use delay(clock=4M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
const char strings[4][8] =
{
"HELLO",
"WORLD",
"CONST",
"STRINGS"
};
//======================================
void main(void)
{
int8 i;
for(i=0; i < 4; i++)
{
printf("%s \r", strings[i]);
}
while(1);
} |
|
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Mon Jun 18, 2012 1:38 am |
|
|
Hi PCMProgrammer,
Thanks for having a look at this, I tried your code and it works for me, although of course you're allocating the longest string length to every string even when they don't need the space. I tried this:
Code: | const char strings[4][] =
{
"HELLO",
"WORLD",
"CONST",
"STRINGS"
};
fprintf(RS232A, "Ed: %s", strings[2]); |
But I get string truncated warnings and it doesn't work.
I would also expect this to work:
Code: | const char *string2 = strings[2]; |
But I get error 27 must evaluate to a constant.
My actual code at the moment looks like:
Code: | char_t va_state_str0[] = "Closed";
char_t va_state_str1[] = "Partly Open";
char_t va_state_str2[] = "Open";
char_t* va_state_str[] = { va_state_str0, va_state_str1, va_state_str2 }; |
And as I have no run out of RAM it would be nice to move all this stuff to ROM. Do you think I should report this as a bug?
Thanks
Ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 18, 2012 3:43 pm |
|
|
Variable length constant strings used to work. I think it's broken for vs.
4.132. I made a demo program for it here, and it worked in vs. 4.104
but it doesn't work now:
http://www.ccsinfo.com/forum/viewtopic.php?t=41353
I just did some more testing, and it works up through vs. 4.130.
Then starting with 4.131, it fails. It's the old problem of lack of
regression testing with CCS.
With the rom ptr example that I linked to earlier, it works up through
vs. 4.129. But with 4.130 and later it fails. So CCS has been doing
some changes and either they changed the syntax and didn't tell us,
or they broke those features.
If you want to report these problems, go ahead. I reported a bug in
memset() on May 27, and never got even an acknowledgment response.
I also asked about the CRC used algorithm in the hex file on June 7, and
never got a reply. So I don't know what's going on. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Tue Jun 19, 2012 12:48 am |
|
|
Besides the same old story of undocumented arbitrary changes to the CCS compiler, it's worth to think
about how variable length strings are implemented and why we expect them to work.
Technically, an array definition is just a rule to calculate object addresses. Obviously, it can't work
to access an array of variable length strings. They need an additional means like a table of string
addresses "hidden" behind the [number_of_strings][] syntax, as it's done in the Microchip Cxx compilers.
I didn't yet check the behaviour with different compiler versions. I understood that according to CCS
the [*] syntax should be used for variable length strings. This suggests, that the working of [] in previous
versions would be an unspecified side effect. |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Tue Jun 19, 2012 6:34 am |
|
|
Variable length strings would be great but I can guess I can do without, shouldn't this work?
Code: | const char string_a[6] = "hello";
const char string_b[6] = "world";
const char *strings[2] = {string_a, string_b}; |
I get an error 27 must evaluate to a constant, but string a and b are constant! Maybe my syntax is bad, what I think I want is, an array in ram of pointers to const strings. So I can then use the syntax:
Code: | fprintf(RS232A, "ed: %s\r", strings[1]); |
Does that seem reasonable?
Cheers
ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Thu Jun 21, 2012 8:56 am |
|
|
I can confirm the problem shown by my code is still present in 4.133.
Cheers
Ed |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jun 21, 2012 1:05 pm |
|
|
Did you email CCS support about the problems with rom pointers and
variable length strings in the last few versions ? Or do you want me to do it ? |
|
|
EdWaugh
Joined: 07 Dec 2004 Posts: 127 Location: Southampton, UK
|
|
Posted: Fri Jun 22, 2012 2:06 am |
|
|
Sorry I haven't yet, I'll do it today. Perhaps if we both emailed about the same issue and included the link to this thread we might get a better response. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|