简体   繁体   中英

Memory usage and overwrites in c++

I am working on a large piece of code. As part of my main class constructor I declare a large number of vectors which at one point or another get filled (all with doubles). Up until a while ago the code ran fine but after I added one further vector of doubles a completely unrelated variable (one which decides whether a particular 'run' has been succesfull or not) is being changed for some reason. I have not added any lines which change this success variable and when I print out it's value (a succesful run leads to the variable being zero) it changes to a massive integer everytime, but each time I run it gives a different value. I have a feeling I am doing something wrong with memory allocation but I don't know what exactly! Any advice welcomed, Cheers Jack

UPDATE

class MyClass {
            std::vector <std::vector<HLV> > qChains;    
    std::vector <std::vector<HLV> > VertexChains;
    std::vector <std::vector<double> > Virtuals;    
    std::vector <double> VProducts;         
    std::vector <double> QProducts;         
    std::vector <double> StrongCouplings;       
            int EventStatus
}

and then in another method of 'MyClass' I have a quick if loop checking the event is going ok:

if (GetEventStatus() != 0) cout << "ERROR!! " << GetEventStatus() << endl;

and ever since I added the line about StrongCouplings the status has been returning random huge integers. I have however noticed that if I place a series of print statements throughout checking the value of EventStatus at various places the problem goes away!

try to add char buf[128]; before the variable that changes its value - if its helps - it will mean some previous variable overrides your variable. It may be caused by ODR violation or by incorrect usage of a C array (if you write after the end of the array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM