0

As the topic, I was confusing this question for two night.

I tried to use function build_BDDs to modify the value of map data structure outs, but the value printed inside the function is not the same as outside the function.

I changed the bool elements of struct map_Data to int type and had the same problem. I suspect I'm using the wrong pointer, but can't figure out what's wrong.

here is my codes.

#include <bits/stdc++.h>
using namespace std;

struct map_Data{
    bool hi;
    bool lo;
};
map<pair<int, int>, map_Data*> outs;

class MWVC{
    public:

    map_Data build_BDDs(map<pair<int, int>, map_Data*> & ins){
        map_Data txt;
        txt.hi = 1;
        txt.lo = 0;
        ins[make_pair(2,4)] = &txt;
        cout << ins[make_pair(2,4)]->hi << ";" << ins[make_pair(2,4)]->lo << endl;
 
        return * ins[make_pair(2,4)];
    }
};


int main(){
    MWVC ojb;
    map_Data a;
    a.hi = 1;
    a.lo = 0;
    outs[make_pair(1,2)] = &a;
    if(outs[make_pair(1,4)]){
        cout << outs[make_pair(1,4)]->hi << endl;
    }

    map_Data b = ojb.build_BDDs(outs);
   
    cout << outs[make_pair(2,4)]->hi << ";" << outs[make_pair(2,4)]->lo << ";" << b.hi << ";" << b.lo << endl;
    return 0;
}

and the result is

1;0
0;0;1;0

Thanks advance.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
JI HAO
  • 1
  • 4
  • 1
    cannot reproduce. The code you posted does not compile https://godbolt.org/z/vcr93arM5 – 463035818_is_not_an_ai Jun 10 '22 at 07:00
  • `#include ` and `using namespace std;` both are very very bad practices. Don't use them. (Google it) – JHBonarius Jun 10 '22 at 07:04
  • I'm sorry for a mistake. map_Data b = ojb.build_BDDs(outside) -->map_Data b = ojb.build_BDDs(outs) – JI HAO Jun 10 '22 at 07:09
  • I suggest to read this [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and [Why should I not `#include `?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – 463035818_is_not_an_ai Jun 10 '22 at 07:27
  • Thanks for the advice, it is indeed a lazy habit. I'm still a beginner and do this so I can get quick results to prevent losing interest in modifying bugs for a long time. However, I really need to learn more about compiling. – JI HAO Jun 10 '22 at 07:35
  • `#include ` and `using namespace std;` together is a good way to introduce bugs that can keep you puzzled for long time. – 463035818_is_not_an_ai Jun 10 '22 at 10:39

1 Answers1

2

You build_BDDs function stores the address of a local variable in the map. When the function returns, the memory it points to can be overwritten at any time.

For this instance, just drop the pointers and let the std::map worry about memory management:

map<pair<int, int>, map_Data> outs;

and change build_BDDs to:

map_Data build_BDDs(map<pair<int, int>, map_Data> & ins){
        ins[make_pair(2,4)] = map_Data{1, 0};
        cout << ins[make_pair(2,4)].hi << ";" << ins[make_pair(2,4)].lo << endl;
 
        return ins[make_pair(2,4)];
    }
Botje
  • 26,269
  • 3
  • 31
  • 41