Another question
-
QUESTION: Hello Zlatko; I have some problems with my program or my compiler.I use Borland C++ 5.02. Some times when i run my program, it starts running and then after some seconds it stops running and displays message: "Thread stopped [my file name]:Fault:access violation at 0x4020d2:write of address 0x130a7c". I don't understand the mean of this message. It some times resolve by reducing the size of the problem. secondly, some times during running process,It suddenly give wrong values to variables and stops the running. Note that my program contains some functions and many loops. Also I've used "goto" command in my functions. It is possible it caused by "goto" command?(mostly I've used "goto" after conditional statements such "if".) ANSWER: Amir, it is not caused by the goto instruction. It is caused by an error in the program. The program is probably trying to read or write memory which it doesn't own. It is impossible to say what the cause of the problem is without seeing the program. Even by looking at the program, it may be difficult to solve. Try running the program in a debugging session. Then when the program crashes, you may be able to look at the stack trace, and examine variables and memory to see the cause of the fault. It takes some skill to do so. Is your program short enough to post as a question ? ---------- FOLLOW-UP ---------- QUESTION: OK, I think I can solve the problem of taking values out of constraints in my program. But still one problem persistently exists. When I want the program to use one of my functions, it stops running and displays: "Thread stopped [my file name]: Fault: access violation at 0x4020d2:read of address 0x130a7c". What is that mean? You can see the message by running this program. Use of "chseq_d()" function causes this message: const int m=2,n=3,kl=3,hl=2; const int hj[n]={2,1,2}; const int kj[n]={1,2,3}; const int mac_ijh[m][n][hl]={{{1,1},{1,0},{1,0}}, {{0,1},{0,0},{1,1}}}; //machines can used for job j const float pt_ijh[m][n][hl]={{{0.5,0.4},{0.6,0 },{0.15,0 }}, {{0 ,0.3},{0 ,0 },{0.2 ,0.2}}}; const float dead_jk[n][kl]={{3,0,0},{1.5,3,0},{1,2,3}}; //Dead Line const float delay_c_j[n]={30,20,10}; const float hold_c_jh[n][hl]={{0.1,0.2},{0.1,0},{0.2,0.4}}; const float setup_c_ijh[m][n][hl]={{{1,1},{1,0},{1,1}}, {{2,2},{2,0},{2,2}}}; const float setup_t_ijh[m][n][hl]={{{0.1,0.1},{0.1,0},{0.1,0.1}}, {{0.2,0.2},{0.2,0},{0.2,0.2}}}; int i,j,k,h,q; float ofcalc(int asgn[n][kl][hl], int seq[n][kl][hl]); int* chasgn(int asgn[n][kl][hl]); int* chseq(int seq[n][kl][hl]); int* chseq_s(int asgn[n][kl][hl], int seq[n][kl][hl]); int* chseq_d(int asgn[n][kl][hl], int seq[n][kl][hl]); float* tfcalc(int asgn[n][kl][hl], int seq[n][kl][hl]); //------------------------------------------------------------------------------ int main() { float f0,f1,f00,df,ddf, r,rr, tc_seq, tc_asgn=50; int l_seq=0,l_asgn=0, counter_seq=0,counter_asgn=0; double pow,acpt; const int tf_seq=1,tf_asgn=1, l_max_seq=5,l_max_asgn=5, tc_seq_in=50; const float w_seq=0.2,w_asgn=0.1; int asgn0[n][kl][hl]={{{1,2},{0,0},{0,0}}, {{1,0},{1,0},{0,0}}, {{1,2},{2,2},{2,2}}}; int seq0[n][kl][hl]={{{1,2},{0,0},{0, 0}}, {{3,0},{4,0},{0, 0}}, {{5,6},{7,8},{9,10}}}; int asgn1[n][kl][hl]={0}, seq1[n][kl][hl]={0},asgn00[n][kl][hl],seq00[n][kl][hl]; int* asgn_1d; int* seq_1d; f0=ofcalc(asgn0,seq0); f00=f0; for(j=0;j for(h=0;h asgn00[j][k][h]=asgn0[j][k][h]; for(j=0;j for(h=0;h seq00[j][k][h]=seq0[j][k][h]; tc_seq=tc_seq_in; generate_seq1: // ()() Generate seq-1 ()() counter_seq+=1; seq_1d=chseq_d(asgn0,seq0); delete chseq_d(asgn0,seq0); q=0; for(j=0;j for(h=0;h f1=ofcalc(asgn0,seq1); df=f1-f0; if(df f0=f1; for(j=0;j for(h=0;h seq0[j][k][h]=seq1[j][k][h]; ddf=f1-f00; if(ddf for(k=0;k for(h=0;h asgn00[j][k][h]=asgn0[j][k][h]; for(j=0;j for(h=0;h seq00[j][k][h]=seq1[j][k][h]; } l_seq+=1; } else{ r=rand()%1000; rr=r/1000; pow=-(df/tc_seq); acpt=exp(pow); if(rr for(j=0;j for(h=0;h seq0[j][k][h]=seq1[j][k][h]; l_seq+=1; }} if(l_seq l_seq=0; if(tc_seq>=tf_seq) goto generate_seq1; } // ()() Generate asgn-1 ()() counter_asgn+=1; asgn_1d=chasgn(asgn0); delete chasgn(asgn0); q=0; for(j=0;j for(h=0;h f1=ofcalc(asgn1,seq0); df=f1-f0; if(df f0=f1; for(j=0;j for(h=0;h asgn0[j][k][h]=asgn1[j][k][h]; ddf=f1-f00; if(ddf for(k=0;k for(h=0;h asgn00[j][k][h]=asgn1[j][k][h]; for(j=0;j for(h=0;h seq00[j][k][h]=seq0[j][k][h]; } l_asgn+=1; } else{ r=rand()%1000; rr=r/1000; pow=-(df/tc_asgn); acpt=exp(pow); if(rr for(j=0;j for(h=0;h asgn0[j][k][h]=asgn1[j][k][h]; l_asgn+=1; } } if(l_asgn l_asgn=0; if(tc_asgn>=tf_asgn){ tc_seq=tc_seq_in; goto generate_seq1; } } cout cout }cout cout }cout cout getch(); return 0; } //******************************* Change Asignment ************************** int* chasgn(int asgn[n][kl][hl]) { int* result=new int[n*kl*hl]; int r,nr,ls,x=0,lmax=0, p,i0,j0,k0,h0,q0, mac_j0h0[m-1]={100}; for (j=0;j for(h=0;h } i0=asgn[j0][k0][h0]-1; for(i=0;i mac_j0h0[x]=i+1; x+=1; } if(x!=0){ r=rand()%1000; nr=1000/x; ls=0; for(p=0;p ls+=nr; if(ls>r){ q0=(j0*kl*hl)+(k0*hl)+h0; result[q0]=mac_j0h0[p]; }}} else goto asd; for(j=0;j for(h=0;h return result; } //************************* End of Change Assignment *********************** //************************* Objective function *********************** float ofcalc(int asgn[n][kl][hl], int seq[n][kl][hl]) { int l,lmax=0; int l0,i0,j0,k0,h0,l1,i1,j1,k1,h1,breaker; float ps_jkh[n][kl][hl]; float ts_jkh[n][kl][hl]; //Start Time of each job float tf_jkh[n][kl][hl]; //Finish Time of each job float t_mac[m]={0,0}; //idle time of each machine {initial value} long double drt_jk[n][kl]; float dt_jk[n][kl]={0}; long double rt_jk[n][kl]={0}; int z_jkh[n][kl][hl]; //=1 if Ojkh perform instantly after Ojk-1h on machine i float cost_d_jk[n][kl]; float cost_hfp_jk[n][kl]; float cost_hwip_jkh[n][kl][hl-1]; float cost_s_jkh[n][kl][hl]; float cost_d=0, cost_hfp=0, cost_hwip=0, cost_s=0; float total_cost; for (j=0;j for (h=0;h cout for (k=0;k for (h=0;h //z_jkh l0=lmax; search_l0: breaker=1; while(breaker){ for (j=n-1;j>=0;j--){ for (k=kj[j]-1;k>=0;k--){ for (h=hj[j]-1;h>=0;h--){ if(seq[j][k][h]==l0) {j0=j; k0=k; h0=h; i0=asgn[j][k][h]-1; breaker=0; } }}}} if(k0==0){ z_jkh[j0][k0][h0]=0; goto change_l0; } l1=l0-1; search_l1: breaker=1; while(breaker){ for (j=n-1;j>=0;j--){ for (k=kj[j]-1;k>=0;k--){ for (h=hj[j]-1;h>=0;h--){ if(seq[j][k][h]==l1) {j1=j; k1=k; h1=h; i1=asgn[j][k][h]-1; breaker=0; } }}}} if(i1==i0){ if(j1==j0&&h1==h0&&k1==k0-1) z_jkh[j0][k0][h0]=1; else z_jkh[j0][k0][h0]=0; goto change_l0; } else{ l1--; if(l1>=1) goto search_l1; else{ z_jkh[j0][k0][h0]=0; goto change_l0; } } change_l0: l0--; if(l0>0) goto search_l0; else goto end_z_jkh; end_z_jkh: cout //----- for (l=1;l for (h=0;h tf_jkh[j][k][h-1]) ts_jkh[j][k][h]=t_mac[i]; else ts_jkh[j][k][h]=tf_jkh[j][k][h-1]; } else ts_jkh[j][k][h]=t_mac[i]; tf_jkh[j][k][h]=ts_jkh[j][k][h]+ps_jkh[j][k][h]+(1-z_jkh[j][k][h])*setup_t_ijh[i][j][h]; t_mac[i]=tf_jkh[j][k][h]; } }}}} cout for (j=0;j for (h=0;h cout //----- //dt - rt for(j=0;j drt_jk[j][k]=dead_jk[j][k]-tf_jkh[j][k][hj[j]-1]; if(drt_jk[j][k]>0) rt_jk[j][k]=drt_jk[j][k]; else dt_jk[j][k]=-drt_jk[j][k]; }} //----- //obj-func for(j=0;j cost_d_jk[j][k]=dt_jk[j][k]*delay_c_j[j]; for(j=0;j cost_d+=cost_d_jk[j][k]; for(j=0;j cost_hfp_jk[j][k]=rt_jk[j][k]*hold_c_jh[j][hj[j]-1]; for(j=0;j for(k=0;k cost_hfp+=cost_hfp_jk[j][k]; for(j=0;j for(h=0;h for(j=0;j for(k=0;k for(h=0;h for(j=0;j for(h=0;h for(j=0;j for(k=0;k for(h=0;h total_cost = cost_d + cost_hfp + cost_hwip + cost_s; cout return total_cost; } //*************** END of Objective function ************************** //*************************** TF Calculator ************************* float* tfcalc(int asgn[n][kl][hl], int seq[n][kl][hl]) { int l0,i0,j0,k0,h0,l1,i1,j1,k1,h1,breaker; int l,lmax=0; float ps_jkh[n][kl][hl]; float ts_jkh[n][kl][hl]; //Start Time of each job float tf_jkh[n][kl][hl]; //Finish Time of each job float t_mac[m]={0,0}; //idle time of each machine {initial value} int z_jkh[n][kl][hl]; //=1 if Ojkh perform instantly after Ojk-1h on machine i float* result=new float[n*kl*hl]; for (j=0;j for (h=0;h }}}; cout for (k=0;k for (h=0;h //z_jkh l0=lmax; search_l0: breaker=1; while(breaker){ for (j=n-1;j>=0;j--){ for (k=kj[j]-1;k>=0;k--){ for (h=hj[j]-1;h>=0;h--){ if(seq[j][k][h]==l0) {j0=j; k0=k; h0=h; i0=asgn[j][k][h]-1; breaker=0; } }}}} if(k0==0){ z_jkh[j0][k0][h0]=0; goto change_l0; } l1=l0-1; search_l1: breaker=1; while(breaker){ for (j=n-1;j>=0;j--){ for (k=kj[j]-1;k>=0;k--){ for (h=hj[j]-1;h>=0;h--){ if(seq[j][k][h]==l1) {j1=j; k1=k; h1=h; i1=asgn[j][k][h]-1; breaker=0; } }}}} if(i1==i0){ if(j1==j0 && h1==h0 && k1==k0-1) z_jkh[j0][k0][h0]=1; else z_jkh[j0][k0][h0]=0; goto change_l0; } else{ l1--; if(l1>=1) goto search_l1; else{ z_jkh[j0][k0][h0]=0; goto change_l0; } } change_l0: l0--; if(l0>0) goto search_l0; else goto end_z_jkh; end_z_jkh: cout //----- for (l=1;l for (h=0;h tf_jkh[j][k][h-1]) ts_jkh[j][k][h]=t_mac[i]; else ts_jkh[j][k][h]=tf_jkh[j][k][h-1]; } else ts_jkh[j][k][h]=t_mac[i]; tf_jkh[j][k][h]=ts_jkh[j][k][h]+ps_jkh[j][k][h]+(1-z_jkh[j][k][h])*setup_t_ijh[i][j][h]; t_mac[i]=tf_jkh[j][k][h]; } }}}} cout for (j=0;j for (h=0;h cout for(j=0;j for(h=0;h return result; } //*************************** End of TF Calculator *********************** //*************************** Change Sequences DELAY ********************** int* chseq_d(int asgn[n][kl][hl], int seq[n][kl][hl]) { int* result=new int[n*kl*hl]; int j0,k0,h0,j1,k1,h1, l0,breaker, r; float ls,nr; float tf_jkh[n][kl][hl],tf_jk[n][kl],maxdelay=0; float* tf_jkh_1d; tf_jkh_1d=tfcalc(asgn,seq); delete tfcalc(asgn,seq); for(j=0;j for(h=0;h for(j=0;j tf_jk[j][k]=tf_jkh[j][k][hj[j]]; for(j=0;j if((tf_jk[j][k]-dead_jk[j][k])>maxdelay){ maxdelay=tf_jk[j][k]-dead_jk[j][k]; j0=j; k0=k; } cout if(maxdelay=0) goto end; choose_h0: ls=0; r=rand()%1000; cout h0=h; ls+=nr; } cout l0=seq[j0][k0][h0]; if(l0==1){ cout goto end; else{ goto end; }} breaker=1; while(breaker) for(j=0;j if(seq[j][k][h]==l0-1){ j1=j; k1=k; h1=h; breaker=0; }} cout if((j1==j0 && k1==k0)||(j1==j0 && h1==h0)){ cout 333) goto choose_h0; else goto end; } else{ seq[j0][k0][h0]=seq[j1][k1][h1]; seq[j1][k1][h1]=l0; } end: for(j=0;j for(h=0;h return result; } //*********************** End of change sequences DELAY ******************* ANSWER: Amir, my build of the program did not crash so I don't know what the problem is. I tried Microsoft and GNU compilers. Both are free. Perhaps it's time to get a new compiler. Yours is quite old and may have a bugs. You can get better free compilers with development environments and debuggers from http://www.microsoft.com/express/Downloads/#2008-Visual-CPP or http://qt.nokia.com/products You do have a misunderstanding of how to use delete. You have code like this seq_1d=chseq_d(asgn0,seq0); delete chseq_d(asgn0,seq0); The line seq_1d=chseq_d(asgn0,seq0); calls the function which allocates memory and returns it to seq_1d. The line delete chseq_d(asgn0,seq0); does the same thing, but deletes the result immediately. Is there a reason for the second call to chseq_d ? Is there some side-effect of the function that you want done ? As far as I can see, the memory created by the first call is never deleted. That would not cause your problem, but you should know that the proper way is to delete the pointer seq_1d, but only after it is no longer being used. Also, because you are using the array form of new, you should use the array form of delete which is delete [] pointer; Try taking out all the delete calls and see if the problem goes away. Another possibility is that the program is accessing an array using indexes that are out of the array's range. You should read over each loop carefully to check it. Do you know what I mean ? For example for (h=0;h cout } In the above loop, is hj[j] the correct upper bound for h ? What you really need is an array class which will do the bounds checking for you on each access. Maybe I can create such a class for you in the next couple of days. It might be interesting. There is a loop like this in ofcalc for (j=0;j My compilers did not accept the endl; by itself. Did you mean cout for(h=0;h for(j=0;j tf_jk[j][k]=tf_jkh[j][k][hj[j]]; for(j=0;j if((tf_jk[j][k]-dead_jk[j][k])>maxdelay){ maxdelay=tf_jk[j][k]-dead_jk[j][k]; j0=j; k0=k; } The line tf_jk[j][k]=tf_jkh[j][k][hj[j]]; has a problem tf_jkh is of dimension 3,3,2, but when j=0, hj[j] is 2 which is too large. When I replace that with hj[j]-1, the error checking system told me that not all paths cause j0 and h0 to be initialized. j0 and k0 are used as indexes so that could easily cause a crash. What does this program do ? Did you put it through an obfuscator so that I could not understand it ? ---------- FOLLOW-UP ---------- QUESTION: Excuse me Zlatko. I have another question. Why some times adding a "cout" command cause changing in result. For example in the last program that I sent you please do this changes: in line 71: change "chseq_d(asgn0,seq0);" to "chseq(seq0);" in line 154: add "getch();" Now run the program and remember "asgn 1" and j0,k0,h0 as the first result.(of course this result for "asgn 1" is wrong and I'm attempting to resolve it) Now add "cout<<' '<<"mac_j0h0: "<<mac_j0h0[0];" in line 415 and run again the program. Is the new result equal the last result? In my Compiler its not! Why?
-
Answer:
Calling functions causes changes in the program memory. A function call will use the stack (a region of memory) to store parameters, and the return address. There is a stack pointer that points to the next available memory on the stack. When a function returns, the stack pointer is reset to the original location but the changes to the stack memory are not erased. Errors in your program may be accessing memory, like stack memory, which are not valid. The changes in the results you see might be caused by reading different data placed on the stack by the cout call. Local arrays, like other local variables, are placed on the stack too. When you read outside the bounds of an array, you are reading other stack locations. The compiler has no way of warning you about that. The error I found yesterday was with the help of array classes which checked the indexes of each array access against the array bounds. It was a lot of work to find 1 bug, but it was the only way I could do it. I have no hope of understanding your program in such a short period of time. Anyway if you are interested, below (at the end) are my array classes. All the code goes into one file. You can call it array.h . The code uses templates, but only simple templates. I hope your compiler can handle them. To use the code, you include "array.h". It requires changes to your program. As I said, it is a lot of work. I did not replace all your C arrays with the new array classes yet. Also, I have more testing to do on the array classes. Here is a sample. Old code: float ofcalc(int asgn[n][kl][hl], int seq[n][kl][hl]) { float ps_jkh[n][kl][hl]; float t_mac[m]={0,0}; New code: float ofcalc(ThreeDimArray & asgn, ThreeDimArray seq) { ThreeDimArray ps_jkh(n,kl,hl); OneDimArray t_mac(m); // initialized to 0 automatically The use of the array is the same as the built in C array. In other words, you can do things like ps_jkh[1][2][3] = x; However, as I said, replacing all your arrays with array classes is a lot of work but there is benefit too because the array management details are hidden. I think your program can be made much easier to read, and that will make the bugs more obvious. You have too many loops doing simple operations and array management. Each loop is a potential source of error. The loops should be wrapped in reusable functions to reduce the redundant code and make the algorithm clearer. You have so many trees, you cannot see the forest. I am still experimenting with it. It is good to try the new compilers, especially because they work with the latest template standard, but it is good to keep your old compiler too. Trying different compilers can reveal more bugs. // Below is array.h ////////////////////////////////////////////////////////////////////////////////////////////////////// // DECLARATIONS ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// // OneDimArray ////////////////////////////////////////////////////////////////////////////////////////////////////// template class OneDimArray { public: OneDimArray(unsigned int dim); ~OneDimArray(); ArrayType& operator[](unsigned int ix); unsigned int Dim(void) const { return mDim;} private: // private for use by next higher dimension array template friend class TwoDimArray; OneDimArray(); OneDimArray(ArrayType *ptrArray, unsigned int dim); private: // unimplemented OneDimArray(const OneDimArray&); OneDimArray& operator=(const OneDimArray&); private: // data unsigned int mDim; ArrayType* mPtrArray; bool mFreePtrArray; }; ////////////////////////////////////////////////////////////////////////////////////////////////////// // TwoDimArray ////////////////////////////////////////////////////////////////////////////////////////////////////// template class TwoDimArray { public: TwoDimArray(unsigned int dim1, unsigned int dim2); ~TwoDimArray(); OneDimArray & operator[](unsigned int ix1); unsigned int Dim1(void) const { return mDim1;} unsigned int Dim2(void) const { return mDim2;} private: void initOneDimArrays(void); private: // private for use by next higher dimension array template friend class ThreeDimArray; TwoDimArray(); TwoDimArray(ArrayType *ptrArray, unsigned int dim1, unsigned int dim2); private: // unimplemented TwoDimArray(const TwoDimArray&); TwoDimArray& operator=(const TwoDimArray&); private: // data unsigned int mDim1; unsigned int mDim2; ArrayType *mPtrArray; bool mFreePtrArray; OneDimArray *mOneDimArrays; }; ////////////////////////////////////////////////////////////////////////////////////////////////////// // ThreeDimArray ////////////////////////////////////////////////////////////////////////////////////////////////////// template class ThreeDimArray { public: ThreeDimArray(unsigned int dim1, unsigned int dim2, unsigned int dim3); ThreeDimArray(const ThreeDimArray&); ~ThreeDimArray(); ThreeDimArray& operator=(const ThreeDimArray&); TwoDimArray & operator[](unsigned int ix1); unsigned int Dim1(void) const { return mDim1;} unsigned int Dim2(void) const { return mDim2;} unsigned int Dim3(void) const { return mDim3;} private: void destroy(void); void copy(const ThreeDimArray& other); void initTwoDimArrays(void); private: // private for use by next higher dimension array //template friend class ThreeDimArray; //ThreeDimArray(); //ThreeDimArray(ArrayType *ptrArray, unsigned int dim1, unsigned int dim2, unsigned int dim3); private: // data unsigned int mDim1; unsigned int mDim2; unsigned int mDim3; ArrayType *mPtrArray; bool mFreePtrArray; TwoDimArray *mTwoDimArrays; }; ////////////////////////////////////////////////////////////////////////////////////////////////////// // IMPLEMENTATIONS ////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// // OneDimArray ////////////////////////////////////////////////////////////////////////////////////////////////////// template OneDimArray ::OneDimArray(unsigned int dim) { mDim = dim; mPtrArray = new ArrayType[mDim]; memset(mPtrArray, 0, sizeof(ArrayType)*mDim); mFreePtrArray = true; } template OneDimArray ::OneDimArray() { mDim = 0; mPtrArray = NULL; mFreePtrArray = false; } template OneDimArray ::OneDimArray(ArrayType *ptrArray, unsigned int dim) { mDim = dim; mPtrArray = ptrArray; mFreePtrArray = false; } template OneDimArray ::~OneDimArray() { if (mFreePtrArray && mPtrArray != NULL) delete [] mPtrArray; } template ArrayType& OneDimArray ::operator[](unsigned int ix) { if (ix >= mDim) { char buf[64]; sprintf_s(buf, sizeof(buf), "OneDimArray bad index %d", ix); throw range_error(buf); } return mPtrArray[ix]; } ////////////////////////////////////////////////////////////////////////////////////////////////////// // TwoDimArray ////////////////////////////////////////////////////////////////////////////////////////////////////// template TwoDimArray ::TwoDimArray(unsigned int dim1, unsigned int dim2) { mDim1 = dim1; mDim2 = dim2; mPtrArray = new ArrayType[mDim1 * mDim2]; memset(mPtrArray, 0, sizeof(ArrayType)*mDim1*mDim2); mFreePtrArray = true; initOneDimArrays(); } template TwoDimArray ::TwoDimArray() { mDim1 = 0; mDim2 = 0; mPtrArray = NULL; mFreePtrArray = false; mOneDimArrays = NULL; } template TwoDimArray ::TwoDimArray(ArrayType *ptrArray, unsigned int dim1, unsigned int dim2) { mDim1 = dim1; mDim2 = dim2; mPtrArray = ptrArray; mFreePtrArray = false; initOneDimArrays(); } template void TwoDimArray ::initOneDimArrays(void) { mOneDimArrays = new OneDimArray [mDim1]; for(unsigned int ix = 0; ix (mPtrArray + ix * mDim2, mDim2); } } template TwoDimArray ::~TwoDimArray() { if (mOneDimArrays != NULL) delete [] mOneDimArrays; if (mFreePtrArray && mPtrArray != NULL) delete [] mPtrArray; } template OneDimArray & TwoDimArray ::operator[](unsigned int ix1) { if (ix1 >= mDim1) { char buf[64]; sprintf_s(buf, sizeof(buf), "TwoDimArray bad index %d", ix1); throw range_error(buf); } return mOneDimArrays[ix1]; } ////////////////////////////////////////////////////////////////////////////////////////////////////// // ThreeDimArray ////////////////////////////////////////////////////////////////////////////////////////////////////// template ThreeDimArray ::ThreeDimArray(unsigned int dim1, unsigned int dim2, unsigned int dim3) { mDim1 = dim1; mDim2 = dim2; mDim3 = dim3; mPtrArray = new ArrayType[mDim1 * mDim2 * mDim3]; memset(mPtrArray, 0, sizeof(ArrayType)*mDim1*mDim2*mDim3); mFreePtrArray = true; initTwoDimArrays(); } template ThreeDimArray ::ThreeDimArray(const ThreeDimArray& other) { copy(other); } template void ThreeDimArray ::destroy(void) { if (mTwoDimArrays != NULL) delete [] mTwoDimArrays; if (mFreePtrArray && mPtrArray != NULL) delete [] mPtrArray; } template void ThreeDimArray ::copy(const ThreeDimArray& other) { mDim1 = other.mDim1; mDim2 = other.mDim2; mDim3 = other.mDim3; mPtrArray = new ArrayType[mDim1 * mDim2 * mDim3]; memcpy(mPtrArray, other.mPtrArray, sizeof(ArrayType)*mDim1*mDim2*mDim3); mFreePtrArray = true; initTwoDimArrays(); } template void ThreeDimArray ::initTwoDimArrays(void) { mTwoDimArrays = new TwoDimArray [mDim1]; for(unsigned int ix = 0; ix (mPtrArray + ix * mDim2 * mDim3, mDim2, mDim3); } } template ThreeDimArray ::~ThreeDimArray() { destroy(); } template ThreeDimArray & ThreeDimArray ::operator=(const ThreeDimArray& other) { if (this != &other) { destroy(); copy(other); } return *this; } template TwoDimArray & ThreeDimArray ::operator[](unsigned int ix1) { if (ix1 >= mDim1) { char buf[64]; sprintf_s(buf, sizeof(buf), "ThreeDimArray bad index %d", ix1); throw range_error(buf); } return mTwoDimArrays[ix1]; }
Miningco.com Visit the source
Related Q & A:
- How to use an object created in Main class with another created in another class?Best solution by stackoverflow.com
- Where can I find the action bar to extend my question for another 4 days here?Best solution by Yahoo! Answers
- Another question about yahoo messenger?Best solution by Yahoo! Answers
- Another tax question. Am I still considered a full-time student?Best solution by Yahoo! Answers
- Another hello kitty question?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.