Segmentation error

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

Join Date: May 2008
Posts: 1
Reputation: rpamballa is an unknown quantity at this point 
Solved Threads: 0
rpamballa rpamballa is offline Offline
Newbie Poster

Segmentation error

 
0
  #1
Jun 12th, 2008
Can someone go through the following code whic generates shortest path for randomly generated processes. My code is generating a segmentation error when I run it, and I am unable to locate the bug.


//This program implements the O(n) algorithm for finding shortest path for randomly generated processing times

  1. #include<stdio.h>
  2. #include<iostream.h>
  3. #include<stdlib.h>
  4.  
  5. struct indexq{
  6. float *col;
  7. int head, rear, n;
  8. };
  9.  
  10. float myrand(int lower, int upper)
  11. {
  12.  
  13. return (lower + rand() % (upper-lower+1));
  14.  
  15. }
  16.  
  17.  
  18.  
  19. int ij_to_k(int i, int j, int n)
  20. {
  21. return i*n+j;
  22. }
  23.  
  24. float costij(float *arr, int k, int l, int n)// function to compute cost of path from i to j
  25. {
  26. int s=1;
  27. return((arr[ij_to_k(n, 2, 2)]-arr[ij_to_k(k, 2, 2)])*(s+arr[ij_to_k(l,1, 2)]-arr[ij_to_k(k, 1, 2)]));
  28. }
  29.  
  30. float compf(float *sum,int i, int n)// function to compute f(i)= (W[n]-W[i])
  31. {
  32. float k = (sum[ij_to_k(n, 2, 2)] - sum[ij_to_k(i, 2, 2)]);
  33. return(k);
  34. }
  35.  
  36. float compv( int k, int l, float *P, float *F)//function to compute v=(F(k)-F(l))/(P[l]-P[k])
  37. {
  38. return ( (F[ij_to_k(k, 1, 2)] - F[ij_to_k(l, 1, 2)])/(P[ij_to_k(l, 1, 2)]-P[ij_to_k(k, 1, 2)]));
  39.  
  40. }
  41.  
  42. void add(indexq &copq, int i, int n)
  43. {
  44. int t;
  45. t = (copq.rear+1)%n;
  46. if(t == copq.head)
  47. cout<<"\nQueue Overflow\n";
  48. else
  49. {
  50. copq.rear=t;
  51. copq.col[copq.rear]=i;
  52. }
  53. }
  54.  
  55.  
  56. int main()
  57. {
  58.  
  59.  
  60. indexq track;
  61. float *pw, *PS;
  62. int n, i=0, j=0;
  63. float *F;
  64. int upper, lower;
  65.  
  66. cout<<"\n Enter number of jobs";
  67. cin>>track.n;
  68. n = track.n;
  69. track.head=track.rear=0;
  70.  
  71. pw = new float[n*2+2];
  72. PS = new float[n*2+2];
  73. F = new float[n*2+2];
  74. // cout<<"\n Enter processing times and their corresponding weights";
  75. cout<<"\n Enter lower and upper value to generate random processing times";
  76. cin>>lower;
  77. cin>>upper;
  78.  
  79. pw[ij_to_k(0, 1, 2)]=0; pw[ij_to_k(0, 2, 2)]=0;
  80.  
  81. for(i=1; i<=n ;i++)
  82. { pw[ij_to_k(i, 1, 2)]= myrand(lower, upper);
  83. pw[ij_to_k(i, 2, 2)]=1;
  84.  
  85. }
  86.  
  87. cout<<"\n\nPartial sums of processing times and weights are:\n ";
  88. PS[ij_to_k(0, 1, 2)]= 0; PS[ij_to_k(0, 2,2)]=0;
  89.  
  90. for(i=1 ; i<=n ; i++)//calculating partial sums of weights and processing times
  91. {
  92.  
  93. PS[ij_to_k(i, 1, 2)]= PS[ij_to_k(i-1, 1, 2)]+pw[ij_to_k(i, 1, 2)]; PS[ij_to_k(i, 2, 2)] = PS[ij_to_k(i-1, 2, 2)]+pw[ij_to_k(i, 2, 2)];
  94. cout<<"\n"<<PS[ij_to_k(i, 1, 2)]<<"\t"<<PS[ij_to_k(i, 2, 2)];
  95. }
  96.  
  97. track.col[track.head]=track.col[track.rear]=n;
  98. F[ij_to_k(n, 1, 2)]=0; F[ij_to_k(n, 2, 2)]=0;
  99.  
  100.  
  101. cout<<"\n Minimum values in each row and column in which it is minimum";
  102. //code to find the shortest path
  103. //First while loop deletes head of the queue until it satisfies the condition: head(Q)!=tail(Q) and f(i)>=v(next(head(Q)), head(Q))
  104. //Second while loop deletes the tail of the queue if it satisfies the condition: head(Q)!=tail(Q) and v(i, tail(Q)) <= v(tail(Q), previous(tail(Q))
  105.  
  106. for( i = n-1; i>=0; i--)
  107. {
  108. int temp= track.head+1;
  109. while (track.col[track.head] != track.col[track.rear] && compf(PS, i, n) >= compv( track.col[temp], track.col[track.head], PS, F))
  110. track.head = (track.head + 1)%n;
  111.  
  112. float j = track.col[track.head];
  113. F[ij_to_k(i, 1, 2)] = costij(PS, i, j, n) + F[ij_to_k(j, 1, 2)];
  114. F[ij_to_k(i, 2, 2)] = j;
  115. int temp2 = track.rear-1;
  116. while(track.col[track.head] != track.col[track.rear] && compv(i, track.col[track.rear], PS, F) <= compv(track.col[track.rear], track.col[temp2], PS, F))
  117. track.rear = (track.rear-1)%n;
  118. add(track, i, n);
  119. cout<<"\n"<<F[ij_to_k(i, 1, 2)]<<"\t"<<F[ij_to_k(i, 2, 2)];
  120. }
  121. cout<<"\n The schedule is\n";
  122. i=0;
  123. while(i<n)
  124. {
  125. cout<<"\ts";
  126. j=i;
  127. while(j<F[ij_to_k(i, 2, 2)])
  128. {
  129. cout<<"\t"<<++j;
  130.  
  131. }
  132.  
  133. i=j;
  134. }
  135. }
Last edited by Ancient Dragon; Jun 12th, 2008 at 10:57 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,971
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 308
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Segmentation error

 
0
  #2
Jun 13th, 2008
What input are you using when you get the segmentationfault? Because I ran the code and it didn't crash...
Two things I did notice:
1.
  1. pw = new float[n*2+2];
  2. PS = new float[n*2+2];
  3. F = new float[n*2+2];
You never delete the memory you allocated => Memoryleak

2.
float compv( int k, int l, float *P, float *F)
If you call all your vars 'k','l','f' etc, it's almost impossible for other people to understand what you are trying to do. You should use meaningful names for variables; if P is an array of persons (for example), just call it float * PersonArray
Last edited by niek_e; Jun 13th, 2008 at 4:15 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Segmentation error

 
0
  #3
Jun 13th, 2008
If you using C++ (you are posting in C++ forum) you should take advantage of STL library which is part of language now. You should almost always use STL containers and avoid C-style arrays.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC