what is the execution sequence of this code?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

what is the execution sequence of this code?

 
0
  #1
Dec 3rd, 2008
Hello Everyone,

I found the following code somewhere, I understand it but I have trouble with understanding the sequence of execution, in other word, How can I monitor every little step of the function like that?
  1. void EatSpace(char* Pbuffer)
  2. {
  3. int i = 0;
  4. int j = 0;
  5. while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
  6. if (*(Pbuffer + i) != ' ')
  7. i++;
  8. }
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: what is the execution sequence of this code?

 
0
  #2
Dec 3rd, 2008
Means..
n=0, m=0
1. if Buffer[n] = 'null' then stop
2. if Buffer[n] = 'space' then n = n + 1, m = m + 1, back to step 1
3. m = m + 1
4. Buffer[n] = Buffer[m], back to step 1
Last edited by cikara21; Dec 3rd, 2008 at 9:23 am. Reason: M...
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,336
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: what is the execution sequence of this code?

 
0
  #3
Dec 3rd, 2008
>>How can I monitor every little step of the function like that?

1) use a debugger

or
2) take a short test string and follow it manually. For example: "H W". Initially variables i and j are both 0, so it will just copy the character 'H' onto itself, then increment j so that j points to the space. Next it asks if i points to a space, and if it does not then incremenet i.

Second loop iteration: i and j are both 1 and both point to the space. So it will copy the space onto itself, then ask if i points to space. It does in this case, so it will NOT increment i.

Third loop iteration: i = 1 and j = 2 so it copies the 'W' onto the space. j is incremented to 3 and since the character at i is now a 'W' instead of a space the value of i is incremented to 2.
The string now looks like this: "HWW"

Fourth loop iteration: i = 2 and j = 3. The string's null terminator is copied to the second 'W' in the string so that the string is contains this: "HW". The whole thing stops because it just copied the '\0' character.
Last edited by Ancient Dragon; Dec 3rd, 2008 at 9:13 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: what is the execution sequence of this code?

 
0
  #4
Dec 3rd, 2008
Mr,Ancient Dragon
you said:
>>>>Next it asks if i points to a space, and if it does not then incremenet i.

how can you decide it, why not j?
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 118
Reputation: chococrack is on a distinguished road 
Solved Threads: 14
chococrack's Avatar
chococrack chococrack is offline Offline
Junior Poster

Re: what is the execution sequence of this code?

 
0
  #5
Dec 3rd, 2008
When I want to thoroughly test something I take it one TINY step at a time, but that might have a lot to do with my OCD

I would worry about the first step, ensure that works fine, then move on to testing the rest of them, rather than trying to monitor everything all at once.
I would love to change the world, but they won't give me the source code
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: what is the execution sequence of this code?

 
0
  #6
Dec 4th, 2008
I can break the execution of the following code before the compiler starting to execute it or after execution of it.
But the line contains many steps, I simply want to trace it, How?
  1. while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,336
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: what is the execution sequence of this code?

 
0
  #7
Dec 4th, 2008
Originally Posted by ALAZHARY View Post
Mr,Ancient Dragon
you said:
>>>>Next it asks if i points to a space, and if it does not then incremenet i.

how can you decide it, why not j?
  1. if (*(Pbuffer + i) != ' ')
  2. i++;
Read the code! Does the code say j? NOOOOOO
Last edited by Ancient Dragon; Dec 4th, 2008 at 8:18 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,336
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: what is the execution sequence of this code?

 
0
  #8
Dec 4th, 2008
Originally Posted by ALAZHARY View Post
I can break the execution of the following code before the compiler starting to execute it or after execution of it.
But the line contains many steps, I simply want to trace it, How?
  1. while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
First do whatever is inside the parentheses, then do the rest.

1) calculate the address Pbuffer+i -- increments pointer Pbuffer by the value of i, and store that value somewhere.

2) calcualte Pbuffer + j and saves that someplace else.

3) increment j

4) copy the character at the pointer calculated in 2) above to the address calculated by 1) above. If i and j had the same value then both 1) and 2) would result in the same address.

5) if the character at the address calculated in 2) above is '\0', then stop the loop.
Last edited by Ancient Dragon; Dec 4th, 2008 at 8:19 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 14
Reputation: ALAZHARY is an unknown quantity at this point 
Solved Threads: 1
ALAZHARY's Avatar
ALAZHARY ALAZHARY is offline Offline
Newbie Poster

Re: what is the execution sequence of this code?

 
0
  #9
Dec 4th, 2008
Don't give me a fish, teach me how to fish.
I'd give my right arm to Improve my english
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,336
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: what is the execution sequence of this code?

 
0
  #10
Dec 4th, 2008
Originally Posted by ALAZHARY View Post
Don't give me a fish, teach me how to fish.
Just trying to answer your question. If you don't like the answer I gave you, then I'll shut up.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC