Bizarre issue outputting sorted vector

So im having a very strange issue where my counter is outputting int he wrong order for whatever reason. When i first run the program it seems to run as intended, but subsequent runs it messes up. I've never encountered this before and my code logic i believe is sound so i dont know. I dont get any warnings or errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <chrono>
#include <string>
#include <random>
#include <algorithm>

std::mt19937 randomEngine(std::chrono::high_resolution_clock::now().time_since_epoch().count());

int RandomNumber();

int main()
{
    std::vector<int> numberStorage;

    for (int i{ 0 }; i < 30; ++i)
    {
        numberStorage.push_back(RandomNumber());
    }

    std::sort(numberStorage.begin(), numberStorage.end());

    std::cout << std::endl;

    int counter{ 1 };

    for (auto& i : numberStorage)
    {
        std::cout << ++counter << ": " << i << '\n';
    }

    return 0;
}

int RandomNumber()
{
    std::uniform_int_distribution<int> intDist(1, 100);

    int randomNumber{ intDist(randomEngine) };

    return randomNumber;
}


oddly enough it seems like the more numbers i output the worse it gets, but if i output say 5, it wont order them wrong, what is happening? Im using visual studio 2022. I tried runnign this in an online compiler and it never had any issues, it ran just fine.

Outputs


7: 2
8: 2
9: 30
10: 3
11: 3
12: 3
13: 3
14: 4
15: 51
16: 51
17: 53
18: 63
19: 65
20: 66
21: 67
22: 69
23: 74
24: 75
25: 76
26: 79
27: 84
28: 87
29: 88
30: 92
31: 96



25: 39
26: 42
27: 44
28: 49
29: 51
30: 58
31: 62
32: 63
8: 9
9: 10
10: 11
11: 15
12: 16
13: 21
14: 22
15: 27
16: 29
17: 29
18: 31
19: 33
20: 33
21: 34
22: 34
23: 35
24: 35
25: 39
26: 42
27: 44
28: 49
29: 51


26:
27:
28:
29: 6
30: 6
31: 6
32: 6
33: 6
34: 6
35: 69
36: 73
37: 74
38: 75
39: 75
40: 75
41: 80
42: 87
43: 88
44: 89
45: 91
46: 91
47: 92
48: 95
49: 96
50: 97



I just noticed something, when i ran it once, i noticed a number way off to the right for some reason, I have no idea whats going on wit this, this is exactly how it looks in the console window


25: 48
26: 48
27: 48
28: 52
29: 53
30: 55
31: 59
32: 61
33: 62
34: 64
35: 71
36: 72
37: 73
38: 73
39: 80
40: 81
41: 82
42: 82
43: 82
44: 86
45: 87
46: 89
47: 90
48: 94
49: 96                                                                                                                  25: 48
26: 48
27: 48
28: 52
29: 53
Last edited on
Ok so i tried this code with code blocks and it does the same thing, however i created a new account on my PC and tried it and it works just fine so theres something on my account causing the issue. The code works just fine on my laptop and on an online IDE too.

I noticed that when i modify the program and build it, it works as expected, then when i close it and run it again, it messes up and produces the output like above. absolutely bizarre.
Last edited on
Ok so I figured out that its the .pdb file generated in visual studio. If i make any kind of modification to my program, literally anything, even a newline, the program will work correctly upon running it, but if i run it again without modifying it, then it will output the garbage that I posted above. However when its outputting garbag, and i delete the .pdb file without modifying the program, it works as expected, and as long as i delete the .pdb file each time i run the program, it will continue to output as expected even if i dont modify the code. How bizarre, and why is it doing this now? does anyone know? any way to fix it? The only thing I can think of is i installed PVS studio to my pc today and this started happening after that. I have since uninstalled it and removed it from my pc and VS. I also did a repair on VS, restarted my pc, reset VS settings etc to no avail.

I made a video here:

https://www.youtube.com/watch?v=wwpJdcz2k4w
Last edited on
Runs fine for me using VS2022 as release build. Are you compiling as release or as debug? If you run the program outside of VS (ie from a command prompt) what happens?
I'm compiling as debug, but compiling as release still does the same thing. I still get the same issue running it in release or debug in the IDE and running the generated .exe in the console. I simplified the program like this:

EDIT: I also want to say i uninstalled VS and reinstalled it, same issue. Im going to try code blocks again and see what happens.

EDIT 2: The same thing happens when compiling this in code::blocks so its not VS specific.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main()
{
    int num{ 0 };

    for (int i{ 0 }; i < 50; ++i)
    {
        std::cout << num++ << '\n';
    }

    std::cin.get();

    return 0;
}


and this produces:























21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41                                                                                                                      21
22
23
24
25
26
27
28
29


I copy pasted that from the console so thats exactly as i saw it outputted.

Deleting the .pdb file makes the program run as expected, but also, so does modifying the program in any way. even if i just add a space and save it, it will output correctly, also if i do Build > Rebuild Solution, then hit run without debuggin, it will work correctly every time. The issue is that when i run it would doing any of those things, i get that garbage output. This code works on my laptop, on online IDE's and I created a new user account on my pc and tried it and it works correctly on that profile, so it has to do with my specific profile, but switching profiles for this issue is not something i can do, So I have to try to figure it out.

I deleted VS 22's cache files, did sfc /scannow, nothing fixed it so far. But it has to have something to do with the way its building the program, if rebuilding the program and running it makes it work that has to be a clue to something, I just dont know what.

Rebuilding and running produces this output every time, which is whats expected:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Last edited on
So i kind of figured it out. I tried repairing and resetting the windows terminal, neither worked so i just uninstalled it and now visual studio uses the visual studio debugger console and that works, i ran it like 15 times to make sure, and it outputs like i want it to. I'll try reinstalling it to see if it works with a fresh install.

EDIT: fresh install didnt work, so im not gonna use it. Hopefully if someone stumbles across this post this will help them.
Last edited on
Topic archived. No new replies allowed.