vector looping problem

I'm writing a program to demonstrate static memory partitioning. The program takes user input for 5 partition sizes and then user input for any number of job sizes (I usually use 6 or 7). The logic is as follows:

1) store job sizes in vector job_size
2) store partition sizes in vector part_size
3)if job size <= partition size, then store partition size and job size in a 2D vector called results and then delete the partition so it can't be used again.
4) If there is no spot for the job (all partitions are full or all partitions are too small, then store job size and "-1" in results vector to indicate the job is waiting.

I will post the entire code, and then the section that's giving me trouble.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
int main()
{
    vector<int> job_size; // vector to hold job sizes
    vector<int> part_size; // vector to hold partition sizes
    vector< pair<int,int> > result; // 2D vector to hold results
    int size;
    int counter = 0;
    int count = 0;
    bool finished = false;
    vector<int>::iterator psize;
    while (count < 5)  // enter 5 partition sizes
    {
        cout << "Please enter the size for partition " << count + 1 << ": ";
        cin >> size;
        part_size.push_back (size);
        count++;
    }
    
    while (!finished) // get sizes of jobs
    {
        cout << "Please enter the size of a job (9999 to stop): ";
        cin >> size;
        if (size != 9999)
        {
            job_size.push_back (size);
        }
        else{finished=true;}
    }
    vector<int>::iterator js;
    vector<int>::iterator ps;
    //bool found = false;
    bool done = false;


    for (js = job_size.begin();  // loop through job sizes
              js != job_size.end();
              js++)
    {
        for (ps = part_size.begin();  // loop through partitions for jobs
                  ps != part_size.end();
                  )
        {
            if (*js <= *ps)  // check if job size <= partition size
            {
                result.push_back(make_pair(*js,*ps));  // if js <= ps, store in results array
                cout << "Job of size " << result[counter].first << 
                        " in partition of size "
                        << result[counter].second << endl;
                ps = part_size.erase(ps); 

                counter++;
                break;                                 
            }

                else if (ps != part_size.end() )  // if no partition found, keep looping
                    {                     // through partitions until there are no more.
                        *ps++;
                        //counter++;
                    }
                else
                {
                        result.push_back(make_pair(*js,*ps));  // if no partitions, job waits
                        cout << "Job of size " << result[counter].first << " waits." << endl;
                        counter++;
                        *ps++;
                        *js++;
                        break;
                }


            }     
        }
        
      }


If I use "!done" in place of "ps != part_size.end();" on line 40 everything works fine except if there are more than 5 jobs. That's where the segfault comes in.


1
2
3
for (ps = part_size.begin();  // loop through partitions for jobs
                  !done;
                  )




When I use "ps != part_size.end();" I think the loop is ending before it gets to the last "else" on line 60.

Any help is appreciated!
Well, I figured it out. I added a couple if statements to the beginning of the loop instead of at the end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
int main()
{
    vector<int> job_size; // vector to hold job sizes
    vector<int> part_size; // vector to hold partition sizes
    vector< pair<int,int> > result; // 2D vector to hold results
    int size;
    int counter = 0;
    int count = 0;
    bool finished = false;
    vector<int>::iterator psize;
    while (count < 5)  // enter 5 partition sizes
    {
        cout << "Please enter the size for partition " << count + 1 << ": ";
        cin >> size;
        part_size.push_back (size);
        count++;
    }
    
    while (!finished) // get sizes of jobs
    {
        cout << "Please enter the size of a job (9999 to stop): ";
        cin >> size;
        if (size != 9999)
        {
            job_size.push_back (size);
        }
        else{finished=true;}
    }    
    vector<int>::iterator js;
    vector<int>::iterator ps;
    for (js = job_size.begin();  // loop through job sizes
              js != job_size.end();
              js++)
    {
        if (part_size.empty())
            {
                result.push_back(make_pair(*js,*ps));  // if no partitions left, job waits.
                cout << "Job of size " << result[counter].first << " waits." << endl;
                counter++;
                *ps++;
            }
         
        for (ps = part_size.begin();  // loop through partitions for jobs
                  ps != part_size.end();
                  )
        {
            if (*js > *(max_element(part_size.begin(), part_size.end())))
            {
                result.push_back(make_pair(*js,*ps));  // if job size over max partition size, job waits.
                cout << "Job of size " << result[counter].first << " waits." << endl;
                counter++;
                *ps++;
                break;
            }
            
           
            if (*js <= *ps)  // check if job size <= partition size
            {
                result.push_back(make_pair(*js,*ps));  // if js <= ps, store in results array
                cout << "Job of size " << result[counter].first << 
                        " in partition of size "
                        << result[counter].second << endl;
                ps = part_size.erase(ps); 

                counter++;
                break;                                 
            }

            else if (ps != part_size.end() )  // if no partition found, keep looping
                {                     // through partitions until there are no more.
                        *ps++;
                }
              
            }     
        }
        
      }
Last edited on
Topic archived. No new replies allowed.