|
|
View previous topic :: View next topic |
Author |
Message |
bdeb
Joined: 05 Nov 2010 Posts: 42 Location: Sweden
|
strlen() on ROM strings |
Posted: Sat Mar 19, 2022 2:14 pm |
|
|
CCS 5.107
MPLAB-X 5.50
18F16Q41
Sorry for stupid question, not a problem that an extra strcpy() can't fix, but I'd like to know why...
NOTES:
PASS_STRINGS=IN_RAM is defined
I have "u8" defined as unsigned int8, "s8" as signed int8, etc
Code: |
const char LookUp[3][20] = {"Text5", "String7", "LongData9" };
u8 idx = 0;
u8 cmdlen = 0;
for (idx=0; idx<3; idx++)
{
cmdlen=strlen(LookUp[idx] );
fprintf(PC,"[%d] Len=%d Txt=%s\r\n", idx, cmdlen, LookUp[idx]);
}
|
This prints:
[0] Len=5 Txt=Text5
[1] Len=6 Txt=String7
[2] Len=6 Txt=LongData9
What gives?
Is strlen() not "overloaded" like printf() to handle ROM->RAM?
And why does it work <=6 ??
I need even more coffee..
All the best:
/Björn |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Sun Mar 20, 2022 4:49 am |
|
|
In the past i have the same problem with const, maybe some of the more clever one can explain about it.
You can remove the const and it work, or you can use rom as this:
Code: |
rom char LookUp[3][20] = {"Text5", "String7", "LongData9" };
char buff[21];
int8 idx = 0;
int8 cmdlen = 0;
for (idx=0; idx<3; idx++)
{
read_program_memory(LookUp[idx],&Buff,20);
cmdlen=strlen(buff);
printf("StrLenTest: [%u] Len=%u Txt=%s\r\n", idx, cmdlen, buff);
}
}
|
or:
Code: |
void strlentest(){
const char LookUp[3][20] = {"Text5", "String7", "LongData9" };
char buff[21];
int8 idx = 0;
int8 cmdlen = 0;
for (idx=0; idx<3; idx++)
{
sprintf(buff,LookUp[idx]);
cmdlen=strlen(buff);
printf("StrLenTest [%u] Len=%u Txt=%s\r\n", idx, cmdlen, buff);
}
}
|
|
|
|
bdeb
Joined: 05 Nov 2010 Posts: 42 Location: Sweden
|
|
Posted: Sun Mar 20, 2022 6:19 am |
|
|
Thank you hmmpic,
I use a variant of your second example, but with strcpy().
BTW Congrats to Kevin being back in F1! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19537
|
|
Posted: Sun Mar 20, 2022 10:28 am |
|
|
I'm actually surprised your code compiled.
I'd have expected there to have been a compiler error that you are
attempting to construct a pointer to rom.
Now 'pass strings' works to virtualise a string, but does it using a very
small buffer. What is happening is this doesn't realise you want a whole
string, so virtualises just the start, expecting to be called again, when the
rest of the string is needed, but strlen doesn't actually access the
virtual string, instead making it's own pointer to this. Unfortunately
this means the extra virtualisation doesn't then happen.
rom as against const, does allow pointers to be constructed. However
they are rom pointers, so normally do need functions specifically written
to use these. hmmpic's code virtualises these to ram.
As you say, strcpy can be used to do the same virtualisation and is
a simple fix. |
|
|
bdeb
Joined: 05 Nov 2010 Posts: 42 Location: Sweden
|
|
Posted: Sun Mar 20, 2022 11:43 am |
|
|
Thank you Ttelmah - just made me a little wiser!
/Björn |
|
|
|
|
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
|