943,692 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 915
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 3rd, 2008
0

what is the execution sequence of this code?

Expand Post »
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?
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ALAZHARY is offline Offline
14 posts
since Jul 2006
Dec 3rd, 2008
0

Re: what is the execution sequence of this code?

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...
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 3rd, 2008
0

Re: what is the execution sequence of this code?

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Dec 3rd, 2008
0

Re: what is the execution sequence of this code?

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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ALAZHARY is offline Offline
14 posts
since Jul 2006
Dec 3rd, 2008
0

Re: what is the execution sequence of this code?

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.
Reputation Points: 92
Solved Threads: 16
Junior Poster
chococrack is offline Offline
149 posts
since Oct 2008
Dec 4th, 2008
0

Re: what is the execution sequence of this code?

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?
C++ Syntax (Toggle Plain Text)
  1. while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ALAZHARY is offline Offline
14 posts
since Jul 2006
Dec 4th, 2008
0

Re: what is the execution sequence of this code?

Click to Expand / Collapse  Quote originally posted by ALAZHARY ...
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?
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Dec 4th, 2008
0

Re: what is the execution sequence of this code?

Click to Expand / Collapse  Quote originally posted by ALAZHARY ...
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?
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Dec 4th, 2008
0

Re: what is the execution sequence of this code?

Don't give me a fish, teach me how to fish.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ALAZHARY is offline Offline
14 posts
since Jul 2006
Dec 4th, 2008
0

Re: what is the execution sequence of this code?

Click to Expand / Collapse  Quote originally posted by ALAZHARY ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Triangle made with numbers from 1 to 9
Next Thread in C++ Forum Timeline: Help Need for Resolving a C++ error C2664





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC