strstr

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

strstr

1,642 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Thu Sep 16 04:21:14 MST 2010
I've been having problem with strstr. It get stuck (or take too long) in a loop that tests the contents of a vector. This is part of my code:

    for (loop = 0; loop < ROOT_DIR_SIZE; loop++)
    {
        if (strstr ((const char *)buffer, (const char *)dev) != NULL)
        {
            device = TRUE;
            break;
        }
        loop++;
    }


It is a simple code, ROOT_DIR_SIZE is 512! So it isn't that big.
Any help?

Thanks,
Renan
0 项奖励
回复
8 回复数

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Thu Sep 16 10:40:54 MST 2010
I know that...:o This code isn't good... I have to change it... :D

Renan
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Thu Sep 16 09:55:45 MST 2010
I see CodeRed's point though. You are doing the an identical comparison, 256 times (you are incrementing the loop counter in for statement and in the loop body).  No wonder it is slow.!
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Thu Sep 16 07:08:35 MST 2010
Ok.
When I changed to redlib, it began to work again.... :confused:
Whatever, if I find something I'll keep you informed.

Thanks,
Renan
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Sep 16 06:55:52 MST 2010
Yes I understand the point of strstr() :p The question really was why are you calling it multiple times (in the for loop) with the same parameters? Surely you will get the same result each time!

Anyway, as far as I can tell, there is no issue with strstr(). If you have evidence that there is an issue, then please provide a compilable example, along the lines of the one at:

http://www.cplusplus.com/reference/clibrary/cstring/strstr/

Regards,
CodeRedSupport
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Thu Sep 16 06:37:38 MST 2010

Quote:

Always helpful with C library related issues to know which library you are using though ! :)


You are right! :)

Well, the idea is to search for a string inside the buffer.
Just want to see if the buffer contains (in any position), for example, the string "S9700-DEV".
I just need to know if this example string exists in the buffer. :D
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Sep 16 06:16:10 MST 2010
Newlib strstr() seems to work OK too. Always helpful with C library related issues to know which library you are using though ! :)


Quote: renan

There is no  interrupt handler/another task updating buffer. I use memcpy to read from flash to ram. That's how my buffer is filled.


I'm not sure I follow why you have your call to strstr() inside a for loop then, as nothing is updating the buffer whilst you are in the for loop. Thus if strstr() returns NULL once, it will return NULL everytime.

Is there more going on in your code here than the snippet suggests ?

Regards,
CodeRedSupport
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by renan on Thu Sep 16 05:57:58 MST 2010

Quote:

I presume you are linking against redlib? And at what optimisation level? And what target MCU?



I'm using newlib (don't remember why). I will try redlib.
No optimizations.
Target MCU: LPC1768


Quote:

Your code does seem a little strange though - I presume that an interrupt handler/another task is updating buffer / dev ?



There is no  interrupt handler/another task updating buffer. I use memcpy to read from flash to ram. That's how my buffer is filled.

I will try linking againt redlib, and see if there is any difference.

Renan
0 项奖励
回复

1,623 次查看
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by CodeRedSupport on Thu Sep 16 05:32:05 MST 2010
I presume you are linking against redlib? And at what optimisation level? And what target MCU?

Anyway, I've just run a few simple tests on strstr() and can't see anything obviously wrong with it. If you pause your code after it has hung, where actually are you? And  are the values pointed to by buffer/dev actually sensible ?

Your code does seem a little strange though - I presume that an interrupt handler/another task is updating buffer / dev ? If that is the case, it might be worth disabling interrupts around the call to strstr() to see if that changes the behaviour that you see.

Regards,
CodeRedSupport.
0 项奖励
回复