| | |
what is the execution sequence of this code?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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?
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)
void EatSpace(char* Pbuffer) { int i = 0; int j = 0; while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0') if (*(Pbuffer + i) != ' ') i++; }
I'd give my right arm to Improve my english
>>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.
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.
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 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
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?
But the line contains many steps, I simply want to trace it, How?
C++ Syntax (Toggle Plain Text)
while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
I'd give my right arm to Improve my english
•
•
•
•
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)
if (*(Pbuffer + i) != ' ') i++;
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.
•
•
•
•
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)
while((*(Pbuffer + i) = *(Pbuffer + j++)) != '\0')
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.
![]() |
Similar Threads
- Help With syntax (Visual Basic 4 / 5 / 6)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- xmlDoc.onload problem, sequence of execution for Firefox (JavaScript / DHTML / AJAX)
- Timing Execution (C++)
- ajax refresh?? (JavaScript / DHTML / AJAX)
- blank pop-ups (Viruses, Spyware and other Nasties)
- Printing binary (C++)
- pygtk Glade GUI freezes (Python)
Other Threads in the C++ Forum
- Previous Thread: Triangle made with numbers from 1 to 9
- Next Thread: Help Need for Resolving a C++ error C2664
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






