What is the error... please Help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

What is the error... please Help!

 
0
  #1
Aug 30th, 2009
I was writing the program for revised simplex method in devc++ (windows) and it was working fine...

Now I am using ubuntu and trying to run the same program.......(with few changes)
i compiled successfully using g++

but when i run it run but showed error some what like:

rhohit@Trashbox:~$ g++ -o R RS.cpp
rhohit@Trashbox:~$ ./R
>Sample:>Maxmimize:
x1 x2 x3 x4 x5 x6 RHS
z -3.00 -2.00 0.00 0.00 0.00 0.00 � 0.00
x3 1.00 2.00 1.00 0.00 0.00 0.00 � 6.00
x4 2.00 1.00 0.00 1.00 0.00 0.00 � 8.00
x5 -1.00 1.00 0.00 0.00 1.00 0.00 � 1.00
*** glibc detected *** ./R: double free or corruption (out): 0xbfaaa878 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e5f604]
/lib/tls/i686/cmov/libc.so.6(cfree+0x96)[0xb7e615b6]
./R[0x804acdc]
./R[0x804b6ac]
./R[0x804b89e]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe5)[0xb7e06775]
./R[0x8048931]
======= Memory map: ========
08048000-0804d000 r-xp 00000000 08:11 2052665 /home/rhohit/R
0804d000-0804e000 r--p 00004000 08:11 2052665 /home/rhohit/R
0804e000-0804f000 rw-p 00005000 08:11 2052665 /home/rhohit/R
08eef000-08f10000 rw-p 08eef000 00:00 0 [heap]
b7c00000-b7c21000 rw-p b7c00000 00:00 0
b7c21000-b7d00000 ---p b7c21000 00:00 0
b7def000-b7df0000 rw-p b7def000 00:00 0
b7df0000-b7f4c000 r-xp 00000000 08:11 1205993 /lib/tls/i686/cmov/libc-2.9.so
b7f4c000-b7f4d000 ---p 0015c000 08:11 1205993 /lib/tls/i686/cmov/libc-2.9.so
b7f4d000-b7f4f000 r--p 0015c000 08:11 1205993 /lib/tls/i686/cmov/libc-2.9.so
b7f4f000-b7f50000 rw-p 0015e000 08:11 1205993 /lib/tls/i686/cmov/libc-2.9.so
b7f50000-b7f53000 rw-p b7f50000 00:00 0
b7f53000-b7f60000 r-xp 00000000 08:11 1172757 /lib/libgcc_s.so.1
b7f60000-b7f61000 r--p 0000c000 08:11 1172757 /lib/libgcc_s.so.1
b7f61000-b7f62000 rw-p 0000d000 08:11 1172757 /lib/libgcc_s.so.1
b7f62000-b7f63000 rw-p b7f62000 00:00 0
b7f63000-b7f87000 r-xp 00000000 08:11 1205997 /lib/tls/i686/cmov/libm-2.9.so
b7f87000-b7f88000 r--p 00023000 08:11 1205997 /lib/tls/i686/cmov/libm-2.9.so
b7f88000-b7f89000 rw-p 00024000 08:11 1205997 /lib/tls/i686/cmov/libm-2.9.so
b7f89000-b806d000 r-xp 00000000 08:11 488747 /usr/lib/libstdc++.so.6.0.10
b806d000-b8071000 r--p 000e3000 08:11 488747 /usr/lib/libstdc++.so.6.0.10
b8071000-b8072000 rw-p 000e7000 08:11 488747 /usr/lib/libstdc++.so.6.0.10
b8072000-b8078000 rw-p b8072000 00:00 0
b808c000-b808f000 rw-p b808c000 00:00 0
b808f000-b8090000 r-xp b808f000 00:00 0 [vdso]
b8090000-b80ac000 r-xp 00000000 08:11 1172748 /lib/ld-2.9.so
b80ac000-b80ad000 r--p 0001b000 08:11 1172748 /lib/ld-2.9.so
b80ad000-b80ae000 rw-p 0001c000 08:11 1172748 /lib/ld-2.9.so
bfa98000-bfaad000 rw-p bffeb000 00:00 0 [stack]
x6 0.00 1.00 0.00 0.00 0.00 1.00 � 2.00Aborted
I'm too confuse Please help!!

source code is as follows

  1. /*
  2.  Name: Resvised Simplex Method
  3.  Author: Rhohitman
  4.  Date: 24/08/09 20:46
  5. */
  6.  
  7. # define TestModePassed
  8. //# define CmdDis
  9.  
  10. # include <iostream>
  11. using namespace std;
  12.  
  13. #ifdef MSWin
  14. #include<conio.h>
  15. #define cls system("cls")
  16. #else
  17. #define cls system("clear")
  18.  
  19. #include <termios.h>
  20. #include <unistd.h>
  21.  
  22. int getch()
  23. {
  24. struct termios oldt,
  25. newt;
  26. int ch;
  27. tcgetattr(STDIN_FILENO, &oldt);
  28. newt= oldt;
  29. newt.c_lflag &= ~(ICANON| ECHO);
  30. tcsetattr(STDIN_FILENO, TCSANOW, &newt);
  31. ch=getchar();
  32. tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
  33. return ch;
  34. }
  35.  
  36. int getche()
  37. {
  38. int ch=getch();
  39. cout<<ch;
  40. return ch;
  41. }
  42. #endif
  43.  
  44. # include <cstdlib>
  45.  
  46. # include <cmath>
  47.  
  48. # ifdef CmdDis
  49. # include <windows.h>
  50. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  51.  
  52. void gotoxy(short,short);
  53. COORD findcursor(short *, short *);
  54.  
  55. # define TxtAttrib(x) SetConsoleTextAttribute(hConsole, x)
  56. # define TagColor 11
  57. /*
  58. # else
  59. # define TxtAttrib(x)
  60. # define TagColor
  61. */
  62. # endif
  63.  
  64. short x, y;
  65. # define loop_limit 5
  66.  
  67. # define dtab 7
  68. # define fix 2
  69.  
  70. char FRAC=0;
  71.  
  72. enum method { halt=0, maximize='1', minimize='2', dualmini='3', integer='9' };
  73.  
  74. typedef struct Table *Tptr;
  75. struct Table
  76. {
  77. unsigned short n, b, a;
  78. char* rTag; //attrib row tag
  79. float* C1; float* C2; float R;
  80. char* bvTag; float* A; float* BInv; float* r;
  81.  
  82. Table(short, short, short);
  83. Table(Tptr);
  84. void reveal();
  85. };
  86.  
  87. Table::Table(short N, short B, short Ar)
  88. {
  89. n=N; b=B; a=Ar;
  90. rTag=NULL;
  91. C1=NULL; C2=NULL;
  92. bvTag=NULL; A=NULL; BInv=NULL; r=NULL;
  93. }
  94.  
  95. Table::Table(Tptr scr)
  96. {
  97. n=scr->n; b=scr->b; a=scr->a;
  98.  
  99. rTag = (char*)calloc(sizeof(char), 2*(b+n+a)+1);
  100. for(x=0; x<2*(b+n+a); x++) *(rTag+x)=*(scr->rTag+x);
  101. *(rTag+x)='\0';
  102.  
  103. bvTag = (char*)calloc(sizeof(char), 2*b+1);
  104. for(x=0; x<2*b; x++) *(bvTag+x)=*(scr->bvTag+x);
  105. *(bvTag+x)='\0';
  106.  
  107. C1=NULL; C2=NULL;
  108. A=NULL; BInv=NULL; r=NULL;
  109. }
  110.  
  111. void Table::reveal()
  112. {
  113. cout<<"\nTable: "<<this;
  114. printf("\n\tn: %d b: %d a: %d", n, b, a);
  115. printf("\n\trTag: %p\tbvTag: %p", (void*)rTag, (void*)bvTag);
  116. cout<<"\n\tC1: "<<C1<<"\tC2: "<<C2<<" \tR: "<<R;
  117. cout<<"\n\tA: "<<A<<"\tBInv: "<<BInv<<"\tr: "<<r;
  118. }
  119.  
  120. # define can_i_stop \
  121. {\
  122. loop_count++;\
  123. if(loop_count>loop_limit)\
  124. {\
  125. printf("Get OFF !! off limit");\
  126. return 0;\
  127. }\
  128. }
  129.  
  130. class RSimplex
  131. {
  132. private:
  133. Tptr tb;
  134. short i, j;
  135. unsigned short pivot_i, pivot_j;
  136.  
  137. float pivot;
  138. float Temp1, Temp2;
  139.  
  140. float* Ex;
  141. float* Te;
  142. float* Cb;
  143. Tptr TTemp;
  144.  
  145. char loop_count;
  146.  
  147. public:
  148. void show(Tptr);
  149.  
  150. void next_table(Tptr, Tptr);
  151.  
  152. Tptr basicMax(Tptr);
  153. Tptr dualMin(Tptr);
  154. Tptr cutPlane(Tptr);
  155.  
  156. void menu(char);
  157. char Samples(char);
  158. void TM_sim_max();
  159. void TM_dual_min();
  160. void TM_cut_plane();
  161. };
  162.  
  163. int dtemp=1;
  164. int ret;
  165.  
  166. int Dec2Fra(float dec_part)
  167. {
  168. int int_part=(int)dec_part;
  169. dec_part-=int_part;
  170.  
  171. if(dec_part<0.005) return int_part;
  172.  
  173. ret=Dec2Fra(1/dec_part);
  174.  
  175. int_part*=ret;
  176. int_part+=dtemp;
  177.  
  178. dtemp=ret;
  179. return int_part;
  180. }
  181.  
  182. char pfrac[6];
  183. float diff;
  184. void pno(float no)
  185. {
  186. if(FRAC==1) goto kick;
  187. diff=no-(int)no;
  188. if(diff!=0 && fabs(diff)>.0005)
  189. {
  190. dtemp=1;
  191. if(no<0) sprintf(&pfrac[0], "-%d/%d", Dec2Fra(-no), dtemp);
  192. else sprintf(&pfrac[0], "%d/%d", Dec2Fra(no), dtemp);
  193. printf("%*s", dtab, pfrac);
  194. }
  195. else
  196. kick:
  197. printf("%*.*f", dtab, fix, no);
  198. }
  199.  
  200. float* matrixMul(float* M1, short I, short K, float* M2, short L, short J)
  201. {
  202. if(K!=L)
  203. {
  204. printf(" %d,%d\t%d,%d ",I,K,L,J);
  205. cout<<"Error!! Incompatible Matrix\n";
  206. getch();
  207. return NULL;
  208. }
  209.  
  210. float* M3=(float*)calloc(sizeof(float), I*J);
  211.  
  212. for(short i=0;i<I;i++) //{
  213. for(short j=0;j<J;j++) //{
  214. for(short m=0;m<K;m++)
  215. *(M3+i*J+j)+= *(M1+i*K+m) * (*(M2+m*J+j));
  216. //pno(*(M3+i*J+j)); } cout<<endl; }
  217. return M3;
  218. }
  219.  
  220. float* matrixAdd(float* M1, float* M2, short I, short J)
  221. {
  222. float* M3=(float*)calloc(sizeof(float), I*J);
  223. for(short i=0;i<I*J;i++) *(M3+i) = *(M1+i) + *(M2+i);
  224.  
  225. return M3;
  226. }
  227.  
  228. void RSimplex::show(Tptr T)
  229. {
  230. #ifdef TestMode
  231. T->reveal();
  232. #endif
  233.  
  234. if(T==NULL)
  235. {
  236. cout<<"\nError!! No Table Found... Null Pointer\n";
  237. return;
  238. }
  239.  
  240. printf("\n%*c", dtab,32);
  241.  
  242. # ifdef CmdDis
  243. TxtAttrib(TagColor);
  244. # endif
  245.  
  246. for(i=0; i< 2*((T->n)+(T->a)+(T->b)); i=i+2)
  247. printf("%c%*c", *(T->rTag+i), -dtab+1,*(T->rTag+i+1));
  248. printf("RHS\n z");
  249.  
  250. # ifdef CmdDis
  251. TxtAttrib(7);
  252. # endif
  253.  
  254. for(j=0; j<T->n; j++) pno(*(T->C1+j));
  255. for(j=0; j<T->b; j++) pno(*(T->C2+j));
  256. printf(" %c", 221);
  257. pno(T->R);
  258.  
  259. for(i=0; i<T->b; i++)
  260. {
  261. # ifdef CmdDis
  262. TxtAttrib(TagColor);
  263. # endif
  264. printf("\n%c%c", *(T->bvTag+i*2), *(T->bvTag+1+i*2));
  265. # ifdef CmdDis
  266. TxtAttrib(7);
  267. # endif
  268. for(j=0; j<T->n; j++) pno(*(T->A+i*T->n+j));
  269. for(j=0; j<T->b; j++) pno(*(T->BInv+i*T->b+j));
  270. printf(" %c", 221);
  271. pno(*(T->r+i));
  272. }
  273. # ifdef CmdDis
  274. findcursor(&x,&y);
  275. # endif
  276. }
  277.  
  278. Tptr RSimplex::cutPlane(Tptr T1)
  279. {
  280. T1->reveal();
  281.  
  282. cout<<"\n\nInteger Progam: ";
  283. Temp2=0;
  284.  
  285. for(i=0; i<T1->b; i++)
  286. {
  287. Temp1=*(T1->r+i)-(int)*(T1->r+i);
  288. if(Temp1>Temp2)
  289. {
  290. Temp2=Temp1;
  291. pivot_i=i;
  292. }
  293. }
  294.  
  295. if(Temp2==0.0 && Temp2 < .0005)
  296. {
  297. cout<<"\n>Solution is in approximate integer value";
  298. return T1;
  299. }
  300.  
  301. cout<<"\n\nNew Table Creation>";
  302. Tptr T2=new Table(T1->n,T1->b+1,0); //allocating memory as size of T1
  303.  
  304. T2->reveal();
  305.  
  306. T2->rTag = (char*)calloc(sizeof(char), 2*((T2->n)+(T2->b)+(T2->a))+1);
  307. T2->bvTag = (char*)calloc(sizeof(char), 2*(T2->b)+1);
  308.  
  309. T2->C2 = (float*)calloc(sizeof(float), (T2->b));
  310. T2->r = (float*)calloc(sizeof(float), (T2->b));
  311.  
  312. T2->R=T1->R;
  313.  
  314. char *marker=(char*)calloc(sizeof(char), (T2->n)+(T2->b)+(T2->a)+1);
  315.  
  316. T2->A = (float*)calloc(sizeof(float), (T2->n)*(T2->b));
  317. T2->BInv = (float*)calloc(sizeof(float), (T2->b)*((T2->b)+(T2->a)));
  318.  
  319. //Basic Variables
  320. x=0;
  321. for(i=0; i<(T1->n)+(T1->b)+(T1->a); i++)
  322. {
  323. *(marker+i)='N';
  324. if(x<T1->b)
  325. {
  326. for(j=0; j< (T1->b); j++)
  327. {
  328. if(*(T1->rTag+2*i+1)!=*(T1->bvTag+2*j+1)) continue;
  329.  
  330. *(marker+i)='F';
  331. *(T2->bvTag+2*x)=*(T1->bvTag+2*j);
  332. *(T2->bvTag+2*x+1)=*(T1->bvTag+2*j+1);
  333.  
  334. *(T2->r+x)=*(T1->r+j);
  335.  
  336. for(y=0; y<(T1->b); y++)
  337. {
  338. if(*(T1->rTag+2*i+1)<T1->n+49) *(T2->BInv+y*T2->b+x)=*(T1->A+y*T1->n+j);
  339. else *(T2->BInv+y*T2->b+x)=*(T1->BInv+y*T1->b+i-T1->n);
  340. }
  341.  
  342. if(*(T2->bvTag+2*x+1)-49<T1->n) *(T2->C2+x)=*(T1->C1+j+T1->n);
  343. else *(T2->C2+x)=*(T1->C2+j+T1->n);
  344.  
  345. *(T2->rTag+2*(T2->n+x))=*(T1->bvTag+2*j);
  346. *(T2->rTag+2*(T2->n+x)+1)=*(T1->bvTag+2*j+1);
  347.  
  348. *(T2->r+x)=*(T1->r+j);
  349.  
  350. x++;
  351. break;
  352. }
  353. }
  354. }
  355.  
  356. *(T2->C2+x)=0;
  357.  
  358. x*=2; *(T2->bvTag+x)='x';
  359. x++; *(T2->bvTag+x)=(T2->n)+(T2->b)+48;
  360. x++; *(T2->bvTag+x)='\0';
  361.  
  362. i*=2; *(T2->rTag+i)='x';
  363. i++; *(T2->rTag+i)=(T2->n)+(T2->b)+48;
  364. i++; *(T2->rTag+i)='\0';
  365.  
  366. T2->C1 = (float*)calloc(sizeof(float), (T2->n));
  367.  
  368. y=0;
  369. cout<<endl;
  370. int z;
  371. for(i=0; i<(T1->n)+(T1->b)+(T1->a); i++)
  372. {
  373. if(y>T1->n) break;
  374. if(*(marker+i)=='F') continue;
  375.  
  376. *(T2->rTag+2*+y)=*(T1->rTag+2*i);
  377. *(T2->rTag+2*+y+1)=*(T1->rTag+2*i+1);
  378.  
  379. //printf("\ni%d: j", i);
  380.  
  381. for(j=0; j<(T1->b); j++)
  382. {
  383. //printf(" %d", j);
  384. z=*(T1->bvTag+2*j+1)-49;
  385. cout<<" "<<z;
  386.  
  387. if(*(T1->rTag+2*i+1)<T1->n+49) *(T2->A+j*T2->n+y)=*(T1->A+z*T1->n+i);
  388. else *(T2->A+j*T2->n+y)=*(T1->BInv+z*T1->b+i-T1->n);
  389. }
  390.  
  391. if(*(T1->rTag+2*i+1)<T1->n+49) *(T2->C1+y)=*(T1->C1+i);
  392. else *(T2->C1+y)=*(T1->C2+i-T1->n);
  393.  
  394. y++;
  395. }
  396.  
  397. free(marker);
  398.  
  399. cout<<endl;
  400. RSimplex::show(T2);
  401.  
  402. return NULL;
  403. }
  404.  
  405. Tptr RSimplex::dualMin(Tptr T1)
  406. {
  407. loop_count=0;
  408.  
  409. Tptr T2;
  410.  
  411. free(Cb); Cb = (float*)calloc(sizeof(float), T1->b);
  412. for(i=0; i<T1->b; i++) *(Cb+i)= - *(T1->C2+i);
  413.  
  414. //Finding Leaving variable
  415. re: Temp1=*(T1->r);
  416. pivot_i=0;
  417. for(i=1; i<T1->b; i++)
  418. {
  419. if(Temp1 > *(T1->r+i))
  420. {
  421. Temp2=Temp1;
  422. pivot_i=i;
  423. }
  424. }
  425. if(Temp1>0) //rhs Values are +ve
  426. {
  427. if(loop_count!=1) free(T2);
  428. return T1;
  429. }
  430.  
  431. //Finding Entering Variable
  432. Temp2=*(T1->C1) / *(T1->C1+pivot_i*T1->n); if(Temp2<0) Temp2*=(-1.0);
  433. pivot_j=0;
  434. for(j=1; j<T1->n; j++)
  435. {
  436. Temp1= *(T1->C1+j) / *(T1->C1+pivot_i*(T1->n-1)+j);
  437.  
  438. if(Temp1<0) Temp1*=(-1.0);
  439. if(Temp2> Temp1)
  440. {
  441. Temp1=*(T1->C1+j);
  442. pivot_j=j;
  443. }
  444. }
  445.  
  446. if(loop_count==0) T2=new Table(T1);
  447.  
  448. RSimplex::next_table(T1,T2);
  449.  
  450. can_i_stop;
  451.  
  452. if(loop_count==1) //1st run only
  453. {
  454. T1=T2;
  455. T2=new Table(T1);
  456. goto re;
  457. }
  458.  
  459. TTemp=T2; T2=T1; T1=TTemp;
  460. goto re;
  461. }
  462.  
  463. Tptr RSimplex::basicMax(Tptr T1)
  464. {
  465. loop_count=0;
  466.  
  467. Tptr T2;
  468.  
  469. free(Cb); Cb = (float*)calloc(sizeof(float), T1->b);
  470. for(i=0; i<T1->b; i++) *(Cb+i)= - *(T1->C2+i);
  471.  
  472. //Finding Entering Variable
  473. re: Temp1=*(T1->C1);
  474. pivot_j=0;
  475. for(j=1; j<T1->n; j++)
  476. {
  477. if(Temp1 > *(T1->C1+j))
  478. {
  479. Temp1=*(T1->C1+j);
  480. pivot_j=j;
  481. }
  482. }
  483. if(Temp1>=0) goto returning; //get C1 -ve
  484.  
  485. //Finding Leaving variable
  486. i=0;
  487. reload:
  488. if(i>=T1->b) goto returning;
  489.  
  490. if(*(T1->A+i*T1->n+pivot_j)!=0)
  491. {
  492. Temp1=*(T1->r+i) / *(T1->A+i*T1->n+pivot_j);
  493. if(Temp1>0) goto ok;
  494. }
  495. i++;
  496. goto reload;
  497. ////////////////////Code Break////////////////////
  498. ok: pivot_i=i;
  499.  
  500. for(i++; i<T1->b; i++)
  501. {
  502. if(*(T1->r+i)<=0 || *(T1->A+i*T1->n+pivot_j)<0) continue;
  503. Temp2=*(T1->r+i) / *(T1->A+i*T1->n+pivot_j);
  504. if(Temp1> Temp2)
  505. {
  506. Temp1=Temp2;
  507. pivot_i=i;
  508. }
  509. }
  510.  
  511. if(loop_count==0) T2=new Table(T1);
  512.  
  513. RSimplex::next_table(T1,T2);
  514.  
  515. can_i_stop;
  516.  
  517. if(loop_count==1) //1st run only
  518. {
  519. T1=T2;
  520. T2=new Table(T1);
  521. goto re;
  522. }
  523.  
  524. TTemp=T2; T2=T1; T1=TTemp;
  525. goto re;
  526. returning:
  527. if(loop_count!=1) free(T2);
  528. return T1;
  529. }
  530.  
  531. void RSimplex::next_table(Tptr T1, Tptr T2)
  532. {
  533. pivot=*(T1->A+pivot_i*T1->n+pivot_j);
  534.  
  535. # ifdef CmdDis
  536. findcursor(&i,&j);
  537. gotoxy((pivot_j*dtab)+2, y-(T1->b)+pivot_i+1);
  538. TxtAttrib(10);
  539. pno(pivot);
  540. TxtAttrib(7);
  541. gotoxy(i,j);
  542. # else
  543. printf("\ni: %d j: %d %9.4f", pivot_i, pivot_j, pivot);
  544. # endif
  545.  
  546. *(T2->bvTag+2*pivot_i)=*(T1->rTag+2*pivot_j);
  547. *(T2->bvTag+2*pivot_i+1)=*(T1->rTag+2*pivot_j+1);
  548.  
  549. //Revised Simplex Method
  550. //Cb
  551. for(i=0; i<T1->b; i++)
  552. {
  553. x=*(T2->bvTag+2*i+1);
  554. for(j=0; j< (tb->n)+(tb->b)+(tb->a); j++)
  555. {
  556. if(x==*(T2->rTag+2*j+1))
  557. {
  558. x=x-49;
  559. if(x>tb->n) *(Cb+i) = - *(tb->C2+x);
  560. else *(Cb+i) = - *(tb->C1+x);
  561. }
  562. }
  563. //pno(*(Cb+i));
  564. }
  565.  
  566. //Exchange Matrix
  567. free(Ex);
  568. Ex = (float*)calloc(sizeof(float), T1->b*T1->b);
  569.  
  570. for(i=0; i<T1->b; i++)
  571. {
  572. for(j=0; j<T1->b; j++)
  573. {
  574. if(j==pivot_i) *(Ex+i*T1->b+j) =* (T1->A+i*T1->n+pivot_j)/pivot*-1.0;
  575. else if(i==j) *(Ex+i*T1->b+j)=1;
  576. else *(Ex+i*T1->b+j)=0;
  577. //pno(*(Ex+i*T1->b+j));
  578. }
  579. //cout<<endl;
  580. }
  581. *(Ex+pivot_i*T1->b+pivot_i) = 1.0 / pivot;
  582.  
  583. //BInverse Next
  584. free(T2->BInv); T2->BInv = matrixMul(Ex,T1->b,T1->b,T1->BInv,T1->b,T1->b);
  585.  
  586. //RHS
  587. free(T2->r);
  588. T2->r = matrixMul(T2->BInv,T1->b,T1->b,tb->r,T1->b,1);
  589.  
  590. //A
  591. free(T2->A);
  592. T2->A = matrixMul(T2->BInv,T1->b,T1->b,tb->A,tb->b,tb->n);
  593.  
  594. //Cb*BInv-C2
  595. free(Ex); Ex = matrixMul(Cb,1,T1->b,T2->BInv,T2->b,T2->b);
  596. free(T2->C2); T2->C2 = matrixAdd(Ex,tb->C2,1,T1->b);
  597.  
  598. //RHS
  599. T2->R = *(matrixMul(Ex,1,T1->b,tb->r,tb->b,1));
  600.  
  601. //Cb*BInv*A-C1
  602. free(Te); Te = matrixMul(Ex,1,T1->b,tb->A,tb->b,tb->n);
  603. free(T2->C1); T2->C1 = matrixAdd(Te,tb->C1,1,T2->n);
  604.  
  605. cout<<endl;
  606. RSimplex::show(T2);
  607. }
  608.  
  609. int main()
  610. {
  611. RSimplex r;
  612. r.menu(r.Samples('1'));
  613. return 0;
  614. }
  615.  
  616. # define dont_Kill_tb if(loop_count!=0) free(Solution); //avoid when solution=tb
  617.  
  618. void RSimplex::menu(char ch)
  619. {
  620. Tptr Solution=NULL;
  621. Tptr Zombie;
  622. if(ch!=0) goto ch_chk;
  623.  
  624. Menu_loop:
  625. cls;
  626. cout<<"Revised Simplex Method\n"
  627. <<"\n0. Fill the table\n"
  628. <<"\n1. Max Optimizition"
  629. <<"\n3. Min Dual Method"
  630. <<"\n9. Integer Progamming"
  631. <<"\n\n*. Move solution to reprocess"
  632. <<"\n\nEnter your choice: ";
  633.  
  634. def:ch=getch();
  635. cls;
  636.  
  637. ch_chk:
  638. switch(ch)
  639. {
  640. /*
  641. case '0':
  642. free(tb);
  643. tb=Simplex::insert();
  644. break;
  645. */
  646.  
  647. case '1':
  648. cout<<">Maxmimize:";
  649. RSimplex::show(tb);
  650. dont_Kill_tb;
  651. Solution=RSimplex::basicMax(tb);
  652. Zombie=Solution;
  653. goto extended_view_sol;
  654.  
  655. case '3':
  656. cout<<">Dual Method Minimize:";
  657. RSimplex::show(tb);
  658. dont_Kill_tb;
  659. Solution=RSimplex::dualMin(tb);
  660. Zombie=Solution;
  661. goto extended_view_sol;
  662.  
  663. case '9':
  664. cout<<">Integer Method:";
  665. RSimplex::show(tb);
  666. //dont_Kill_tb;
  667. //Solution=
  668. RSimplex::cutPlane(tb);
  669. goto extended_view_sol;
  670.  
  671. case '.':
  672. cout<<">Optimal Table:";
  673. Zombie=Solution;
  674. goto extended_view;
  675.  
  676. case '`':
  677. free(tb); //clear the place before new values
  678. ch=Samples(0);
  679. goto ch_chk;
  680.  
  681. case '*':
  682. if(loop_count==0) goto Menu_loop; //avoid free(tb) when solution=tb
  683. tb=Solution;
  684. cout<<"\a";
  685. Zombie=tb;
  686. goto extended_view;
  687.  
  688. case 13:
  689. cout<<">Orignal Table:";
  690. Zombie=tb;
  691. goto extended_view;
  692.  
  693. case 27: return;
  694.  
  695. case 'f':
  696. case 'F':
  697. FRAC=!FRAC;
  698. cout<<"\a";
  699.  
  700. default: //for blinking
  701. goto Menu_loop;
  702. //goto def;
  703. }
  704. getch();
  705. goto Menu_loop;
  706.  
  707. extended_view:
  708. RSimplex::show(Zombie);
  709.  
  710. extended_view_sol:
  711. switch(getch())
  712. {
  713. case '.':
  714. FRAC=!FRAC;
  715. cout<<"\a\n";
  716. goto extended_view;
  717. }
  718. goto Menu_loop;
  719. }
  720. # ifdef CmdDis
  721. void gotoxy(short x, short y)
  722. {
  723. COORD CursorPosition={x,y};
  724. SetConsoleCursorPosition(hConsole, CursorPosition);
  725. }
  726.  
  727. COORD findcursor(short *x, short *y)
  728. {
  729. CONSOLE_SCREEN_BUFFER_INFO window;
  730. GetConsoleScreenBufferInfo(hConsole, &window);
  731. *x=window.dwCursorPosition.X;
  732. *y=window.dwCursorPosition.Y;
  733. COORD CursorPosition = {*x,*y};
  734. return CursorPosition;
  735. }
  736. # endif
  737.  
  738. void RSimplex::TM_sim_max()
  739. {
  740. tb=new Table(2,4,0);
  741.  
  742. static char rTag[] ="x1x2x3x4x5x6"; tb->rTag = &rTag[0];
  743. static char bvTag[] ="x3x4x5x6"; tb->bvTag = &bvTag[0];
  744.  
  745. static float C1[]={-3,-2}; tb->C1 = &C1[0];
  746. static float C2[]={0,0,0,0}; tb->C2 = &C2[0];
  747. tb->R=0;
  748.  
  749. static float A[] =
  750. {
  751. 1, 2,
  752. 2, 1,
  753. -1, 1,
  754. 0, 1,
  755. };
  756.  
  757. tb->A = &A[0];
  758.  
  759. static float BInv[] =
  760. {
  761. 1, 0, 0, 0,
  762. 0, 1, 0, 0,
  763. 0, 0, 1, 0,
  764. 0, 0, 0, 1
  765. };
  766. tb->BInv=&BInv[0];
  767.  
  768. static float r[]={6, 8, 1, 2};
  769. tb->r=&r[0];
  770. }
  771.  
  772. void RSimplex::TM_dual_min()
  773. {
  774. tb=new Table(2,3,0);
  775.  
  776. static char rTag[] ="x1x2x3x4x5"; tb->rTag = &rTag[0];
  777. static char bvTag[] ="x3x4x5"; tb->bvTag = &bvTag[0];
  778.  
  779. static float C1[]={-3,-2}; tb->C1 = &C1[0];
  780. static float C2[]={0,0,0}; tb->C2 = &C2[0];
  781. tb->R=0;
  782.  
  783. static float A[] =
  784. {
  785. -3, -1,
  786. -4, -3,
  787. 1, 1,
  788. };
  789. tb->A = &A[0];
  790.  
  791. static float BInv[] =
  792. {
  793. 1, 0, 0,
  794. 0, 1, 0,
  795. 0, 0, 1
  796. };
  797.  
  798. tb->BInv=&BInv[0];
  799.  
  800. static float r[]={-3, -6, 3};
  801. tb->r=&r[0];
  802. }
  803.  
  804. void RSimplex::TM_cut_plane()
  805. {
  806. tb=new Table(2,2,0);
  807.  
  808. static char rTag[] ="x1x2x3x4"; tb->rTag = &rTag[0];
  809. static char bvTag[] ="x3x4"; tb->bvTag = &bvTag[0];
  810.  
  811. static float C1[]={-7,-9}; tb->C1 = &C1[0];
  812. static float C2[]={0,0}; tb->C2 = &C2[0];
  813. tb->R=0;
  814.  
  815. static float A[] =
  816. {
  817. -1, 3,
  818. 7, 1,
  819. };
  820. tb->A = &A[0];
  821.  
  822. static float BInv[] =
  823. {
  824. 1, 0,
  825. 0, 1
  826. };
  827.  
  828. tb->BInv=&BInv[0];
  829.  
  830. static float r[]={6,35};
  831. tb->r=&r[0];
  832. }
  833.  
  834. char RSimplex::Samples(char ch)
  835. {
  836. printf(">Sample:");
  837. if(ch==0) ch=getche();
  838.  
  839. switch(ch)
  840. {
  841. case '1':
  842. TM_sim_max();
  843. ch=maximize;
  844. break;
  845.  
  846. case '3':
  847. TM_dual_min();
  848. ch=dualmini;
  849. break;
  850.  
  851. case '6':
  852. TM_cut_plane();
  853. cout<<">Integer Method>Maxmimize:";
  854. RSimplex::show(tb);
  855. RSimplex::cutPlane(RSimplex::basicMax(tb));
  856. getch();
  857. ch=halt;
  858. break;
  859.  
  860. default: return halt;
  861. }
  862.  
  863. return ch;
  864. }
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What is the error... please Help!

 
0
  #2
Aug 30th, 2009
Well your class lacks both a constructor AND a destructor!

> free(Cb); Cb = (float*)calloc(sizeof(float), T1->b);
Cb is garbage on entry, and then you try to free it.

That's your
*** glibc detected *** ./R: double free or corruption (out): 0xbfaaa878 ***

It might not be that one, but your code is littered with OLD C code.
Not to mention abuse of goto horrors.

And how did you manage to write nearly 1000 lines before finding this out?
This would have blown up as soon as you'd finished writing the function, and then tried to test it.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 68
Reputation: Rhohitman is an unknown quantity at this point 
Solved Threads: 4
Rhohitman's Avatar
Rhohitman Rhohitman is offline Offline
Junior Poster in Training

Re: What is the error... please Help!

 
0
  #3
Aug 30th, 2009
i don't get it, constructor and destructor, since i am only using one objects its should not be the problem... Pardon me if i am wrong.

Thanx but the problem was
i was not able to allocate the memory for array using 'new' without the constant variable. since lack of time i basically littered the OLD C out.

i manage it in wxDevC++ you should try it out sometime...
Chazing Dreams ;'P
Shhhh.......ZZzzzzzzzzzzzzzzzzzzzz.....
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: What is the error... please Help!

 
0
  #4
Aug 30th, 2009
Run it in the debugger.
Put a breakpoint on all the free() calls.
When you hit a breakpoint, examine the pointer.
If it isn't NULL, then it is garbage, and it's bye bye program.

> since i am only using one objects its should not be the problem
It still needs initialising though.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC