Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 299
  • Last Modified:

When I delete the code to print the vector I receive bad data in my next function.

HELP I'm only 2 weeks into relearning C++ and I'm having major problems!

 When I delete the code used to print the vector for testing I receive an error "segmentation fault core dumped". I'm passing the vector as a reference to another function. It's working fine as long as I leave the print vector code in the read_input function....  

This is how the code looks when I receive the error:

// int i,j;                                            
//        for(i = 0; i < num_proc;i++)    
//        {                                            
//                for(j =0; j < 3; j++)        
//                {
//                 printf("%5d",input_data[i][j]);
//                }
//           printf("\n");
//        }


Here is my entire code after headings without the error:


void read_input (int *num);
void fcfs (int *num_proc,vector <int*>& vec);
int main ()
{
  int num = 0;
  read_input(&num);
  return 0;
}

void read_input (int *num)
{
  ifstream processes ("processes.dat");
  int num_proc;
  int time_quantum;
  processes >> num_proc;
  *num = num_proc;

  processes >> time_quantum;
  vector<int*> input_data;
  int line_id = 0;

  while (! processes.eof() )
    {
      input_data.push_back (new int[3]);
      for (int a = 0; a <= 2; a++)
         processes >> input_data[line_id][a];
         line_id++;
    }

 int i,j;                                              //THIS IS THE PROBLEM
        for(i = 0; i < num_proc;i++)      // WHEN I COMMENT OUT THIS CODE
        {                                             // THE VECTOR DOESN"T PASS THE CORRECT
                for(j =0; j < 3; j++)         // VALUES to the FCFS function
                {
                 printf("%5d",input_data[i][j]);
                }
           printf("\n");
        }

    //Call FCFS function
        fcfs(&num_proc,input_data);
 }



void fcfs (int *num_proc,vector <int*>& vec)
 {

  int i,j;
       for(i = 0; i < *num_proc;i++)
      {
             for(j =0; j < 3; j++)
             {
             }
      }

   int swt = 0;
   float awt;
   int save_value = 0;
   int g_last_value = 0;
   int s = 0;
   int b = 0;

   int c,first_time = 1;
   int g[*num_proc];

   while (s != *num_proc )
    {
      if (first_time)
        {
          save_value = swt;
          first_time = 0;
        }
      else
        {
          g[s] = vec[b][c] + save_value;
          save_value = g[s];
          b++;
          s++;
        }
     } //endwhile


    g_last_value = *num_proc - 1;
    int sum = 0;
    s = 0;
    while (s != g_last_value)
       {
         sum = sum + g[s];
         s++;
       }

    awt = (float)sum / *num_proc;
    std::cout << "Algorithm: FCFS " << "\n";
    std::cout << "CPU-Gantt Chart: " << "\n";
    b = 0;
    c = 0;
    int first = 1;
    int zz;
    for(int z = 0; z < *num_proc;z++)
      {
       if (first)
         {
           first = 0;
           std::cout << "P" << vec[b][c] << ": 0-" << g[z] << "\n";
           zz = z;
           b++;
         }
       else
         {
           std::cout << "P" << vec[b][c] << ": " << g[zz] << "-" << g[z] << "\n";
           b++;
           zz = z;
         }

     }

    std::cout << "Average Waiting Time: " << awt << " time units" << "\n";
 }


0
shermaine_08
Asked:
shermaine_08
  • 5
  • 3
1 Solution
 
jkrCommented:
At least, there's nothing obvious. Try to run that code in a debugger to see on which line it faults exactly.
0
 
pb_indiaCommented:
try this

#include <iostream>
#include <fstream>

#include <vector>


using namespace std;

void read_input (int *num);
void fcfs (int *num_proc,vector <int*>& vec);
int main ()
{
  int num = 0;
  read_input(&num);
  return 0;
}

void read_input (int *num)
{
  ifstream processes ("processes.dat");
  int num_proc;
  int time_quantum;
  processes >> num_proc;
  *num = num_proc;

  processes >> time_quantum;
  vector<int*> input_data;
  int line_id = 0;

  while (! processes.eof() )
    {
      input_data.push_back (new int[3]);
      for (int a = 0; a <= 2; a++)
         processes >> input_data[line_id][a];
         line_id++;
    }

 int i,j;                                              //THIS IS THE PROBLEM
        for(i = 0; i < num_proc;i++)      // WHEN I COMMENT OUT THIS CODE
        {                                             // THE VECTOR DOESN"T PASS THE CORRECT
                for(j =0; j < 3; j++)         // VALUES to the FCFS function
                {
                 //Call FCFS function
                              fcfs(&num_proc,input_data);
                }
         
        }

   
 }



void fcfs (int *num_proc,vector <int*>& vec)
 {

  int i,j;
       for(i = 0; i < *num_proc;i++)
      {
             for(j =0; j < 3; j++)
             {
             }
      }

   int swt = 0;
   float awt;
   int save_value = 0;
   int g_last_value = 0;
   int s = 0;
   int b = 0;

   int c=0,first_time = 1;
   int g[100];

   while (s != *num_proc )
    {
      if (first_time)
        {
          save_value = swt;
          first_time = 0;
        }
      else
        {
          g[s] = vec[b][c] + save_value;
          save_value = g[s];
          b++;
          s++;
        }
     } //endwhile


    g_last_value = *num_proc - 1;
    int sum = 0;
    s = 0;
    while (s != g_last_value)
       {
         sum = sum + g[s];
         s++;
       }

    awt = (float)sum / *num_proc;
    std::cout << "Algorithm: FCFS " << "\n";
    std::cout << "CPU-Gantt Chart: " << "\n";
    b = 0;
    c = 0;
    int first = 1;
    int zz;
    for(int z = 0; z < *num_proc;z++)
      {
       if (first)
         {
           first = 0;
           std::cout << "P" << vec[b][c] << ": 0-" << g[z] << "\n";
           zz = z;
           b++;
         }
       else
         {
           std::cout << "P" << vec[b][c] << ": " << g[zz] << "-" << g[z] << "\n";
           b++;
           zz = z;
         }

     }

    std::cout << "Average Waiting Time: " << awt << " time units" << "\n";
 }
0
 
shermaine_08Author Commented:
Not sure how to reply. Do I use feedback or just use the post as I'm doing now?

TO: PB_India

Thanks for responding. I tried that suggestion but I'm still getting the error: Segmentation Fault (core dumped)


TO: JKR
I'm using telnet from home and the system doesn't offer any debugging tools that I know of. Can you suggest any free downloads?

Thanks guys. Any more suggestions?? I appreciate your input.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jkrCommented:
>>I'm using telnet from home and the system doesn't offer any debugging tools that I know of.

Well, 'gdb' still should work. Using it in text mode is cumbersome, but... BTW, if you posted all your code (and maybe a sample input file), I would give it a try here, but since it's just a fragment, this is not possible.
0
 
shermaine_08Author Commented:

I'm getting used to the vi editor so gdb sounds like tons of fun. I will look up some info on gdb later and try to figure out how to use it from home. I have to run to class but I will check the post tonight when I get home.

Here is the input file (processes.dat):

5 10
1 21 2
2 5 4
3 10 1
4 30 3
5 1 5


Here is the output I'm getting:

    1   21    2      // I'm trying to get rid of these 5 lines without the segmentation error
    2    5    4      // I was just trying to print the output for testing
    3   10    1
    4   30    3
    5    1    5
Algorithm: FCFS   // This is where my output should start
CPU-Gantt Chart:
P1: 0-21
P2: 21-26
P3: 26-36
P4: 36-66
P5: 66-67

OK, Here is the code:

  // reading a text file into a vector
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;

void read_input (int *num);
void fcfs (int *num_proc,vector <int*>& vec);

int main ()
{
  int num = 0;
  read_input(&num);
  return 0;
}


void read_input (int *num)
{
  ifstream processes ("processes.dat");

  int num_proc;
  int time_quantum;
  processes >> num_proc;

  *num = num_proc;

  processes >> time_quantum;

  vector<int*> input_data;

  int line_id = 0;
  while (! processes.eof() )
    {
      input_data.push_back (new int[3]);
      for (int a = 0; a <= 2; a++)
         processes >> input_data[line_id][a];
         line_id++;
    }

  int i,j;
        for(i = 0; i < num_proc;i++)
        {
                for(j =0; j < 3; j++)
                {
                 printf("%5d",input_data[i][j]);
                }
         printf("\n");
        }

    //Call FCFS function
        fcfs(&num_proc,input_data);
 }

void fcfs (int *num_proc,vector <int*>& input_data)
 {

  int i,j;
       for(i = 0; i < *num_proc;i++)
      {
             for(j =0; j < 3; j++)
             {
             }
      }

   int swt = 0;
   float awt;
   int save_value = 0;
   int g_last_value = 0;
   int s = 0;
   int b = 0;

   int c,first_time = 1;
   int g[*num_proc];

   while (s != *num_proc )
    {
      if (first_time)
        {
          save_value = swt;
          first_time = 0;
        }
      else
        {
          g[s] = input_data[b][c] + save_value;
          save_value = g[s];
          b++;
          s++;
        }
     } //endwhile

    g_last_value = *num_proc - 1;
    int sum = 0;
    s = 0;
    while (s != g_last_value)
       {
         sum = sum + g[s];
         s++;
       }

    awt = (float)sum / *num_proc;
    std::cout << "Algorithm: FCFS " << "\n";
    std::cout << "CPU-Gantt Chart: " << "\n";
    b = 0;
    c = 0;
    int first = 1;
    int zz;
    for(int z = 0; z < *num_proc;z++)
      {
       if (first)
         {
           first = 0;
           std::cout << "P" << input_data[b][c] << ": 0-" << g[z] << "\n";
           zz = z;
           b++;
         }
       else
         {
           std::cout << "P" << input_data[b][c] << ": " << g[zz] << "-" << g[z] << "\n";
           b++;
           zz = z;
         }

     }

    std::cout << "Average Waiting Time: " << awt << " time units" << "\n";
 }
0
 
jkrCommented:
AAah, here we have the culprit - look at

  int c,first_time = 1;
  int g[*num_proc];

  while (s != *num_proc )
   {
     if (first_time)
       {
         save_value = swt;
         first_time = 0;
       }
     else
       {
         g[s] = input_data[b][c] + save_value; // <---- crashes here, 'c' is uninitialized
         save_value = g[s];
         b++;
         s++;
       }
    } //endwhile

Make that read


  int c = 0,first_time = 1; // set 'c' to '0'
  int g[*num_proc];

  while (s != *num_proc )
   {
     if (first_time)
       {
         save_value = swt;
         first_time = 0;
       }
     else
       {
         g[s] = input_data[b][c] + save_value;
         save_value = g[s];
         b++;
         s++;
       }
    } //endwhile

and it will work fine. It seems to be coincidentioal that printing the values made that work before.
0
 
jkrCommented:
BTW, is

    1   21    2
    2    5    4
    3   10    1
    4   30    3
    5    1    5
Algorithm: FCFS
CPU-Gantt Chart:
P1: 0-1
P2: 1-3
P3: 3-6
P4: 6-10
P5: 10-15
Average Waiting Time: 4 time units

the output that you expected? :o)
0
 
shermaine_08Author Commented:
TO JKR

Thank you so much! I didn't have any errors when I removed the print vec lines. I can't belive that was the problem. It's Always something simple.

The out put is fine, except I had to initialize c = 1 to access the correct member of the vector. The Output below looks great:

Algorithm: FCFS
CPU-Gantt Chart:
P1: 0-21
P2: 21-26
P3: 26-36
P4: 36-66
P5: 66-67

You are awesome!
0
 
jkrCommented:
You're most welcome :o)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now