连续调用长方法会产生溢出

发布于 2024-10-07 01:30:21 字数 2393 浏览 0 评论 0原文

嗨,标题确实说明了一切。我有一种方法,可以读取保存材料成分数据的文件(例如聚乙烯或铅的天然成分),每种材料都保存在不同的对象中,并且每种材料都有不同的成分。 make 方法是按顺序调用的,因为一旦从另一个文件中获取了构成物理系统的材料,下一步就是自然的。写入列表的内容是对文件的o/p,那么列表中的实际上就是对该文件末尾的op。如何避免溢出?

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;
    }
}

例如,聚乙烯的输入文件:

.335657
carbon 12 .33333
hydrogen 1 .66667

产生这个(其中包含铅、铜、硼、氢的同位素三次,碳一次):

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

非常感谢任何建议! (是的,它说“void”作为返回值。我可以在运行下一个方法调用之前等待每个方法调用的返回,但我不知道该怎么做。)

我可能还没有找到方法在 TINTERWEBS 上执行此操作,因为我真的不知道术语是什么,如果是这种情况,解决问题的链接很棒!

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
hydrogen 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗弃M 2024-10-14 01:30:21

我的问题在哪方面难以理解? (说真的,除了我缺乏技术编程术语知识之外,我编写的代码是为了让人们能够理解。这个问题缺少什么?)

上面的方法在主方法中被调用了六次。

每次调用它时,它都会从文件中获取输入(例如最短的文件,我将其包含在内以演示代码的行为方式,其他文件则更长)。

仔细阅读代码,您应该注意到输入文件的第一行保存在名为 lamda 的变量中,除了输出到 outpuf 文件之外,该变量不会再次使用。

然后读取 inout 文件的连续行并将其放入两个条目的向量中,对应于同位素的“Z”和该特定“Z”的相对丰度。然后将该向量放入列表中。向量中保存的值显然应该是写入输出文件的值。

正如您所看到的,列表中的条目比输入文件中的行数还要多。

这些无关的行是由在不同对象中运行的相同方法引起的。

我所说的“溢出”是指列表中的项目(位于单独的对象中)溢出到另一个对象中。由于运行的方法数量较多,因此存在大量“溢出”。

由于 c 中的事物共享内存地址的相似性,我将其称为同一个事物,正如我所认为的那样,本质上是一样的。

我需要的是如何避免这种情况发生的建议,必须有许多程序打开相同类型的对象并并行运行它们的方法(如本例所示)。我无法从内存中清除这些列表,因为每个列表都是必需的,以便它们描述的材料表现出物理上应有的行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文