Microchip C18 - 奇怪的代码行为(可能与扩展模式/非扩展模式相关)
我在使用 PIC18F67J60 的 Microchip C18 编译器时遇到了这个奇怪的问题。
我创建了一个非常简单的函数,它应该返回较大字符串中子字符串的索引。
我不知道出了什么问题,但该行为似乎与是否启用扩展模式有关。
在 MPLAB.XI 中启用扩展模式后,得到:
- memcmppgm2ram 函数始终返回零。
在 MPLAB.XI 中禁用扩展模式时,得到:
- 迭代器变量
i
的值计数为:0, 1, 3, 7, 15, 21
我在想一些堆栈问题或其他问题,因为这真的很奇怪。 完整代码如下所示。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char bigString[] = "this is a big string";
unsigned char findSubStr(char *str, const rom char *subStr, unsigned char n, unsigned char m)
{
unsigned char i;
for (i=0; i < n-m; i++)
{
if(0 == memcmppgm2ram(&str[i], (const far rom void*)subStr, m))
return i;
}
return n; // not found
}
void main(void)
{
char n;
n = findSubStr(bigString, (const rom void*)"big", sizeof(bigString), 3);
}
I have this weird problem with the Microchip C18 compiler for PIC18F67J60.
I have created a very simple function that should return the index of a Sub-String in a larger String.
I don't know whats wrong, but the behavior seems to be related to wether extended mode is enabled or not.
With Extended-Mode enabled in MPLAB.X I get:
- The
memcmppgm2ram
function returns zero all the time.
With Extended-Mode disabled in MPLAB.X I get:
- The value of iterator variable
i
counts as:0, 1, 3, 7, 15, 21
I'm thinking some stack issue or something, because this is really weird.
The complete code is shown below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char bigString[] = "this is a big string";
unsigned char findSubStr(char *str, const rom char *subStr, unsigned char n, unsigned char m)
{
unsigned char i;
for (i=0; i < n-m; i++)
{
if(0 == memcmppgm2ram(&str[i], (const far rom void*)subStr, m))
return i;
}
return n; // not found
}
void main(void)
{
char n;
n = findSubStr(bigString, (const rom void*)"big", sizeof(bigString), 3);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
memcmppgm2ram()
期望指向数据存储器 (ram) 的指针作为其第一个参数。您正在传递一个指向字符串文字的指针,该字符串位于程序内存 (rom) 中。您可以使用
memcmppgm()
代替,或者使用memcpypgm2ram()
或strcpypgm2ram()
将其他字符串复制到 ram。不幸的是,我无法对此进行测试,因为我目前无法访问该编译器。
memcmppgm2ram()
expects a pointer to data memory (ram) as its first argument. You are passing a pointer to a string literal, which is located in program memory (rom).You can use
memcmppgm()
instead, or copy the other string to ram usingmemcpypgm2ram()
orstrcpypgm2ram()
.Unfortunately I can't test this, as I don't have access to this compiler at the moment.