开发者

Sequential calls to long method produces spilling

开发者 https://www.devze.com 2023-01-29 01:10 出处:网络
Hi the title says everything really. I have a method which reads in a file holding data on the composition of materials (like polythene, or the natural composition of lead) each of these materials is

Hi the title says everything really. I have a method which reads in a file holding data on the composition of materials (like polythene, or the natural composition of lead) each of these materials is held in a different object and each has a different composition. The make method is called sequentially, as the once the materials which make up the physical system are obtained from another file it is the natural next step. What is written into the list is o/p to a file, then what is actually in the list is op to the end of that file. How do I avoid spillage?

void material::make(char* filename)   
{           
    ifstream fin(filename) ;    
ofstream filenameOUT ;
string outfile = (string)filename + "OUT.txt" ;
filenameOUT.open(outfile.c_str()) ;
    string ZStr, propStr, isotope ;
    vector<float> component(2,0) ;

    getline(fin, propStr, '\n') ;   //store first entry in file as a str (prop is used to save mem allocation)
    lamda = atof(propStr.c_str()) ;   //then convert to float so calcs can be done
filenameOUT<<"lamda: "<<lamda<<endl;

    while(!fin.eof())
    {
        getline(fin, isotope, ' ') ;  //get the element name
        getline(fin, ZStr, '\t') ;   //get the Z's and abunancies from the file.
        getline(fin, propStr) ;
        component[0] = atof(ZStr.c_str()) ;
        component[1] = atof(propStr.c_str()) ;
filenameOUT<<"isotope: "<<isotope<<" isotope Z: "<<component[0]<<" proportional amount: "<<component[1]<<endl;
        composition.push_back(component) ;
        elements.push_back(isotope) ;
    }
filenameOUT<<filename<<" is loaded"<<endl;

    for(c=composition.begin();c!=composition.end();c++)
    {
filenameOUT<<(*c)[0]<<" : "<<(*c)[1]<<" is loaded"<<endl;
    }
}

for example, the input file for polyethylene:

.335657
carbon 12 .33333
hy开发者_开发技巧drogen 1 .66667

produces this (which contains isotopes of lead, copper, boron, hydrogen three times and carbon once):

lamda: 0.335657
isotope: carbon isotope Z: 12 proportional amount: 0.33333
isotope: hydrogen isotope Z: 1 proportional amount: 0.66667
poly.txt is loaded
11 : 0.04 is loaded
10 : 0.01 is loaded
12 : 0.31778 is loaded
1 : 0.63332 is loaded
1 : 0.63332 is loaded
204 : 0.014 is loaded
206 : 0.241 is loaded
207 : 0.221 is loaded
208 : 0.524 is loaded
208 : 0.524 is loaded
106 : 0.0125 is loaded
108 : 0.0089 is loaded
110 : 0.1249 is loaded
111 : 0.128 is loaded
112 : 0.2413 is loaded
113 : 0.1222 is loaded
114 : 0.2873 is loaded
116 : 0.0749 is loaded
12 : 0.33333 is loaded
1 : 0.66667 is loaded

any advice much appreciated! (and, yes, it says 'void' as the return. I could wait for a return from each method call before I run the next, but I don't know how to do that.)

I MAY NOT HAVE FOUND THE WAY TO DO THIS ON TINTERWEBS BECAUSE I DON'T REALLY KNOW WHAT THE TERMINOLOGY IS, IF THIS IS THE CASE LINKS TO FIXED PROBLEMS ARE GREAT!


In what way is my question incomprehensible? (Seriously, other than my lack of knowledge of technical programming terms, I wrote the code so it could be understood. What does the question lack?)

The method above is called six times from the main method.

Each time it is called it takes input from a file (such as the shortest one, which I have included to demonstrate how the code should behave, others are longer).

Following the code through you should notice that the first line of the input file is held in a variable called lamda which is not used again other than being output to the outpuf file.

Sequential lines of the inout file are then read and placed into a vector of two entries, corresponding to the 'Z' of the isotope and relative abundance of that particular 'Z'. This vector is then put into a list. The values which are held in the vector are clearly supposed to be the ones which are written to the output file.

As you can see, there are more entries in the list than there are lines in the input file.

These extraneous lines arise from the same method being run, in a different object.

The 'spillage' to which I refer is the spillage of items in a list, which is in a seperate object, into another. Due to the number of methods running, there is a lot of 'spillage'.

Due to its similarity of memory adresses being shared by things in c, I called it the same thing, as I thought it was, essentially.

What I require is advice on how to avoid this occurring, there must be many programs which open the same type of object and run their methods (as it seems in this case) in parallel. I cannot clear the lists from memory as each is required so that the material they describe behaves as it physically should.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号