'When assigning value to a member function pointer, the assignment tampered with other data

Before the assignment operation, the member variable u works fine, see figure 1

After the assignment operation, the data of the member variable u is tampered, see figure 2

Continuing resize operation will trigger an error: read access violation, see figure 3

The development environment is win10 Visual Studio Community 2022 17.0.4 x64 in Debug mode

Compiling by mingw does not show this problem and the code runs smoothly. I suspect it's a bug for msvc. In addition, this error is very strange, if merged the 4 code files, this error may or may not disappear, which is related to the code order.

Below is the complete code

test.h

#pragma once
class ETEST {
public:
    ETEST() {};
    void add() {};
};

order.h

#pragma once
#include <vector>
class ETEST;

class First {
public:
    First(const char*);
    void (ETEST::* add)();
    std::vector<double> u, v;
};

class Second : public First {
public:
    Second(const char*);
};

first.cpp

#include "test.h"
#include "order.h"

First::First(const char*) :
    add(NULL), u{}, v{} {
};

int main(int argc, char* argv[]) {
    Second chk("");
    return 0;
}

second.cpp

#include "order.h"
#include "test.h"
#include <cstdio>

Second::Second(const char*) :
    First(NULL) {
    add = &ETEST::add;
    u.resize(10, 0.0);
    printf("u resize sucess!\n");
};

Below are my actions:

1.create new solution

2.add code files

3,paste the code

4.Debugging, it throws an exception

(I did not modify any solution properties, all use the default settings)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source