Random C++ execution flow when using breakpoints in Visual Studio 2017?

Random C++ execution flow when using breakpoints in Visual Studio 2017?



I added breakpoints to every line in visual studio 2017 community edition for the following code


#include "pch.h"
#include <iostream>
using namespace std;

// base class
class Vehicle
public:
Vehicle()

cout << " **** This is a Vehicle **** " << endl;

;

// sub class derived from two base classes
class Car : public Vehicle

;

// main function
int main()

// creating object of sub class will
// invoke the constructor of base classes
Car obj;
return 0;



An action is set here to print a debug text when each line is executed.



I build the program then I ran a new debug instance. I got this



enter image description here



Output #1 : first 7 lines


main function
class car
blank line inside car class
// sub class derived
vehicle class closed
blank line after car class
// main



Then I rebuild the program and ran a new instance and I got this



enter image description here



Output #2: first 7 lines


main function bracket open
// main
car class close
class car
blank line inside car class
blank line after car class
blank line after vehicle class



I have optimization turned off.



I'm new to C++ and would like to know why this is happening, Is it compiling differently or is there something wrong with the breakpoint action execution?



EDIT: I have added the first 7 lines of the output in the screenshot. There you can see that the order of execution of the breakpoint action is different for the two runs.



NOTE: Please note that the output lines you see are added by me using the breakpoint -> action functionality, this way Visual studio will print out my message when that breakpoint is hit. The execution won't stop it will just print out whatever text I put in as the breakpoint action






Don't upload images of code on SO when asking a question. Just copy all the input and output texts and paste here

– phuclv
Sep 12 '18 at 2:08







If I had showed you the code as text then how would you be able to see the breakpoints? Don't downvote a question for such a small reason.

– Vizkrig
Sep 12 '18 at 2:09







@Vizkrig it is sufficive to say that there are breakpoints. Also, please describe what it is that "this is happening".

– Swordfish
Sep 12 '18 at 2:11







Okay. I added the code and removed the screenshot

– Vizkrig
Sep 12 '18 at 2:12






@Swordfish The problem is that the output is different for the two runs. I have edited the question and added the first 7 lines of each output log, now the difference is pretty clear. And I'm sorry if my question didn't make sense at first. Thank you for patiently pointing that out

– Vizkrig
Sep 12 '18 at 4:51




1 Answer
1



You have breakpoints at many lines that do not actually represent executable code. When the application is executed, these breakpoints are moved to the closest executable statement.



You can see that when you actually break at a breakpoint in Visual Studio. Some of the breakpoints seem to be gone, but several breakpoints just moved to reference the same statement.



For the breakpoints that are moved to reference the same statement, the order of their debug messages is not defined.



Editor View:



enter image description here



Debugger view:



enter image description here






So you're saying the order of break statements is random for each run?

– Vizkrig
Sep 12 '18 at 4:38






@Vizkrig Yes, you end up with 2 or more breakpoints on one line, there is no defined order for multiple breakpoints on the same line to execute in

– Alan Birtles
Sep 12 '18 at 7:37



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)