|
|
View previous topic :: View next topic |
Author |
Message |
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
Problem overwritten my own pointer. |
Posted: Fri Feb 11, 2011 1:56 pm |
|
|
What is the smartest way to do this
All code is only test, and not for production.
Suggestion?
Code: | //Just a storage buffer
char Buffer[50];
//This Buffer handle incoming RS232 storage, from RX interrupt..
char Fifo[128];
//Problem because it use pointer to same FiFo
char *F1(){
char *ptr;
//do something... The RX interrupt handle all the char there are coming and store them in the FiFo.
ptr=strtok(FiFo,"_;");
return (ptr);
}
//Problem because it use pointer to same FiFo
char *F2(){
char *ptr;
//do something... The RX interrupt handle all the char there are coming and store them in the FiFo.
ptr=strtok(FiFo,"#_;");
return (ptr);
}
//This is working!
char *F3(){
char *ptr;
static Buf[30];
//do something... The RX interrupt handle all the char there are coming and store them in the FiFo.
ptr=strtok(FiFo,"#_;");
strcpy(Buf,ptr);
return (Buf);
}
//The problem here is that F1() & F2() wont work together because they both point to FiFo
//I thought that F1() was processed first and then F2() but I'm wrong.
main(){
sprintf(Buffer,"F1:%s F2:%s F3:%s",F1(),F2(),F3()};
.....
} |
|
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Fri Feb 11, 2011 9:22 pm |
|
|
F1 and F2 are both tokenizing FiFo from the beginning. If that's not what you want, you need to read the strtok documentation again.
As a blatant hint, perhaps F2 and F3 should be using 0 instead of FiFo? _________________ Andrew |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Sat Feb 12, 2011 1:34 am |
|
|
Hi
Hmm Not the real problem. I only use one call to strtok, therefore NULL is not needed. The FiFo is handled in RX interrupt, and the first line in all the function is _clear_ FiFo, I just forgot to write it.
This is only symbol coding not real coding.
Look it that way, this will work:
Code: | Buffer= F1();
Buffer&=F2();
Buffer&=F3(); |
But the sprintf() do it that way:
Code: | ptr1=F1();
ptr2=F2();
ptr3=F3();
Now is start formating from here.
|
Ptr1 will be killed after a call to F2()! Therefore the way F3() is written will work, but take a lot more RAM.
I have not expected that behavior from the sprintf(). I have hoped it have called the function in the order is need them. But NOT it start calling all the function.
Hints. |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Sat Feb 12, 2011 10:03 pm |
|
|
Frankly, you need to help yourself:
hmmpic wrote: | I just forgot to write it. This is only symbol coding not real coding. |
Not including your actual code is just wasting everyones time.
Quote: | Code: | Buffer= F1();
Buffer&=F2();
Buffer&=F3(); |
|
I have no idea what &= is doing there. It's not valid code, nor have you explained what it's doing.
Quote: | I have not expected that behavior from the sprintf(). I have hoped it have called the function in the order is need them. But NOT it start calling all the function. |
Perhaps you're not understanding how C (or basically any procedural language) works. When you call a function with function calls in the arguments, eg: Code: | sprintf(buffer, fmt, func1(), func2()); | All the arguments are evaluated first, left to right. Only then is sprintf called. sprintf *does not* call func1() or func2().
If you're still having trouble with this, find a book on C and review the section on function calls. _________________ Andrew |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Feb 13, 2011 2:55 am |
|
|
Quote: | Ptr1 will be killed after a call to F2()! Therefore the way F3() is written will work, but take a lot more RAM. |
I fear, you didn't yet get the real problem. In my opinion it's in the simple fact, that the first call to strtok is modifying the buffer, placing \0 behind the end of the first token returned. So the second call will fail, unless it uses the strtok(0," ") option. Or a copy of the original string. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Feb 13, 2011 3:45 am |
|
|
I don't think the problem is however to do with the code 'using a pointer to the same FIFO'...
It is to do with the fundamental operation of strtok, which is designed to split of a string with tokens, and changes the string when it does so. So the string passed to F2, has already been shortened....
Three possibilities:
1) Use a copy - you have already mentioned this.
2) Don't use strtok. Write your own search, that replaces the tokens you want, but maintains a global 'flag' 'first_time' say, and the location of the last match, and a copy of the character replaced. Have it automatically set 'first_time' to false, and save the character it is replacing on a search. However, if called with 'first_time' false, it just puts back the saved character, before proceeding.
Then before the first call, set 'first_time', and call the function. On subsequent calls, it'll put back the last substituted character before proceeding, avoiding the problems.
3) remember strings are just character arrays. Just find the section you want in each case, and copy this. Lot smaller than using the whole string.
Best Wishes |
|
|
hmmpic
Joined: 09 Mar 2010 Posts: 314 Location: Denmark
|
|
Posted: Mon Feb 14, 2011 3:02 am |
|
|
Thanks all for your hints.
Yes the topic was from the start bad written, take a look at the list file from a sprintf(); It start calling _all_ the function, and it was what confusing me and make the mess, from the start.
I have now tested the code in normally "c" and the behavior is the same. The real problem was that I newer have used a pointer to the same Buffer in a sprintf(); statement before, like this one.
All the mess start when I want to save RAM.
All fine now after using a "static char buffer" in all the function there return a pointer.
Thanks for the support all. |
|
|
|
|
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
|