Thread: C++ arrays setting last value of one array to th first value of another
hi,
have noticed problem when using c++ on ubuntu laptop, title suggests, when using 2 arrays seems reason first value of 1 array being set thesecond value of array , not sure why, here example code of mine, please note root code have noticed happen normal c++ code:
of pseudo code, reading in ascii file containing 2 lists separated space in file, these read pair of vectors, couldnt find simple way used counter keep track of number looking at, put in correct vector.
root not seem vectors have move vectors simple arrays.
arrays used plot graph
i have indicated using comment string in code line seems sort out, no means proper solution.....code:#include "riostream.h" #include<iostream> #include<fstream> #include"tvector.h" #include"tgraph.h" #include"tcanvas.h" #include"troot.h" #include"tpad.h" #include"taxis.h" #include"tmath.h" #include"th1f.h" #include"tframe.h" #include"tlegend.h" int main() { double_t x; double_t e[260], res[260]; vector<double_t> en; vector<double_t> re; int_t = 0; ifstream file("q3data.txt"); while(file>>x) { if (i%2==0) { en.push_back(x); } if (i%2>0) { re.push_back(x); } i=i+1; } for(int_t j=0;j<=260;j++) { e[j]=en[j]; res[j]=re[j]; cout<<e[j]<<" "<<res[j]<<endl; } res[0]=re[0]; // if line not present reason res[0]=e[261] cout<<res[0]<<endl; cout<<e[0]<<endl; tcanvas *c1 = new tcanvas("c1","1",200,10,700,500); c1->setfillcolor(0); c1->setgrid(); //th1f *hr = c1->drawframe(20,0,100,150); tgraph *gr = new tgraph(260,e,res); gr->setlinecolor(2); gr->setlinewidth(4); gr->setmarkercolor(4); gr->setmarkerstyle(21); gr->settitle("dsfdf"); gr->getxaxis()->settitle("df"); gr->getyaxis()->settitle("df"); gr->draw("acp"); c1->saveas("3.1.pdf"); return 0; }
in regards particular code reason res[0] set equal e[261] or en[261] if res[0]=re[0] isnt added code shown above
have idea why happening?
bunch
edit should add running ubuntu 11.10 x64
res , e right next each other on stack. when access e[261], you're going past end of e array , res array. problem in loop:
the last thing copy element 260. remember, however, arrays in c++ indexed 0, making elements of e , res 0-259, line should have j < 260 (not <=). e[260] same place in memory res[0].code:for(int_t j=0;j<=260;j++)
code:| | | | | | | | | | | | | | | | | | | | | | | ^ e[0] ... e[259] ^ | ^ res[0] ... ^ e[260]
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk C++ arrays setting last value of one array to th first value of another
Ubuntu
Comments
Post a Comment