Array pointer issues

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1087 Views - Last Post: 09 March 2010 - 10:06 PM Rate Topic: -----

#1 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Array pointer issues

Posted 09 March 2010 - 05:51 PM

int *process;
ifstream in(filename);
...
...
...//start going through text file, getting values for n and m 
process=new int[n,(m+1),40];
for(i=0;i<n;i++)
{//loop for processes
	in.ignore(100,' ');
	in>>d;
	in.ignore(100,'\n');
	in.ignore(100,' ');
	in>>c;
	process[i,0,0]=d;//inputs value 5 in
	process[i,1,0]=c;//inputs value 3 in
	cout<<process[i,0,0]<<" "<<process[i,1,0]<<endl;//couts 3 and 3
}//end for



This part of the code is a shortened version of an function that goes through an input file and puts numbers into this 3D array.

I have to use pointers to create the process array after the fact because I don't know what n and m are until I actually get into the file (the first two numbers in the file give me those values).

Now, I test the entire code with a test input. And in said input, it works out so that d and c equal 5 and 3 respectively. When I actually debug the code (using Visual Studio 2008), it shows that d and c equal 5 and 3. When I cout d and c, it shows 5 and 3. But for some reason when I cout process[i,0,0] and process[i,1,0] it gives me 3 and 3.

I have a feeling it has something to do with the pointer only pointing to one value..? But I don't know what to do to fix it.

Any help is appreciated. If you want to see the whole program, I'll give it to you.

Is This A Good Question/Topic? 0
  • +

Replies To: Array pointer issues

#2 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 06:23 PM

I'm wondering... do I need to put asterisks before "process" so that it will access the individual memory location in the matrix? It seems that the issue is that it's treating it like it's a single variable rather than a matrix.
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Array pointer issues

Posted 09 March 2010 - 06:43 PM

process[i,0,0]

That doesn't work with arrays.

You need brackets for everything
process[i][0][0]
http://cplusplus.com...utorial/arrays/

You can't allocate new array using that method either:
You have to allocate the array row by row.
http://www.codeproje...rayDinamic.aspx
http://stackoverflow...f-integers-in-c
Was This Post Helpful? 1
  • +
  • -

#4 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 06:49 PM

View Posteker676, on 09 March 2010 - 05:43 PM, said:

process[i,0,0]

That doesn't work with arrays.

You need brackets for everything
process[i][0][0]
http://cplusplus.com...utorial/arrays/

You can't allocate new array using that method either:
You have to allocate the array row by row.
http://www.codeproje...rayDinamic.aspx
http://stackoverflow...f-integers-in-c


So I'm assuming I would have to go through and allocate arrays using a for loop or something to that effect?

Edit:

int **max;
max=new int*[n];
for(i=0;i<n;i++)
{
	max[n][i]=new int[m]
}//end for



So something like this for example if I want to create an n by m matrix?

This post has been edited by agentkirb: 09 March 2010 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Array pointer issues

Posted 09 March 2010 - 06:52 PM

Yeah,

2 dimensional arrays only require one for loop.
I've never used a 3d dynamically allocated array but from the example it looks like you need 3 for loops.

Edit: Okay, they used three because they wanted to initialize the array as well.
Merely allocating only requires nrOfDimensions - 1 for loops.

// Start by allocating an array for array of arrays
int*** myArray = new int**[X_MAXIMUM];

// Allocate an array for each element of the first array
for(int x = 0; x < X_MAXIMUM; ++x)
{
    myArray[x] = new int*[Y_MAXIMUM];

    // Allocate an array of integers for each element of this array
    for(int y = 0; y < Y_MAXIMUM; ++y)
    {
        myArray[x][y] = new int[Z_MAXIMUM];

        // Specify an initial value (if desired)
        for(int z = 0; z < Z_MAXIMUM; ++z)
        {
            myArray[x][y][z] = -1;
        }
    }
}

This post has been edited by eker676: 09 March 2010 - 07:06 PM

Was This Post Helpful? 1
  • +
  • -

#6 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 07:11 PM

ifstream in(filename);
in>>m;//number of resources
in.ignore(100,'\n');
in>>n;//number of processes
available=new int[m];
work=new int[m];
max=new int*[n];
for(i=0;i<n;i++)
{
	max[n][i]=new int[m];//quoted error happens right here
}//end for

need=new int*[n];
for(i=0;i<n;i++)
{
	need[n][i]=new int[m];
}//end for
allocated=new int*[n];
for(i=0;i<n;i++)
{
	allocated[n][i]=new int[m];
}//end for
finish=new int[n];
need=max;
for(i=0;i<n;i++)
{
	for(j=0;j<m;j++)
	{
		allocated[i][j]=0;
	}//end j for
}//end i for
for(i=0;i<m;i++)
{
	in.ignore(100,'=');
	in>>available[i];
	cout<<available[i]<<endl;
}//end for
for(i=0;i<n;i++)
{
	for(j=0;j<m;j++)
	{
		in.ignore(100,'=');
		in>>max[n][m];
		cout<<max[n][m]<<" ";
	}//end j for
	cout<<endl;
}//end i for


I get a lot of--

e:\os1\os1\main.cpp(42) : error C2440: '=' : cannot convert from 'int *' to 'int'

--errors when I run the above code. I commented where said error was. But the same error appears another 2-3 times when I try the same thing later on. I looked at the example you just quoted and tried to implement that... am I not doing it right?

I initialize it at the beginning as

int **max;
, and similar for other variables.

This post has been edited by agentkirb: 09 March 2010 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#7 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 07:20 PM

Hmmm... I do the same thing for process, and I get no errors (basically copy and paste from the example and changing the variables) and I get no errors. So I did something wrong. Let me see if I can find it.
Was This Post Helpful? 0
  • +
  • -

#8 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Array pointer issues

Posted 09 March 2010 - 07:23 PM

Okay, for two dimensional arrays (max) you initialize it like so:

int **max = new int*[n];

for(int i = 0; i < n; i++)
{
  max[i] = new int[m];
}


Could you post your full code? It's easier to look at/fix/compile/test.

Also, remember, when you allocate multi dimensional arrays like that you need to delete them using for loops as well or you will have some big memory leaks. You can't get away with a single delete[] call.

Deleting max:
for(int i = 0; i < n; i++)
{
  delete[] max[i];
}
delete[] max;

This post has been edited by eker676: 09 March 2010 - 07:23 PM

Was This Post Helpful? 1
  • +
  • -

#9 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 07:27 PM

Here's the whole code:

It's a process handling/deadlock detection simulator. I'm supposed to simulate Shortest Job First or Earliest Deadline First for the scheduling algorithm and Bankers Algorithm for the deadlock detection. Just in case you were curious what you were looking at.

I havn't corrected the end, so after allocating everything, the rest I havn't messed with and it's wrong right now.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
char filename[20];
int m=0,n=0,i=0,j=0,k=0,l=0,a=1,d=0,c=0,control=0,control1=0,min,minproc,loopend=0,time=0,stopped,notsafe=1;
char cha[7]; 
int *available;
int **max;
int **need;
int **allocated;
int ***process;
int *status;
int *finish;
int *work;
cout<<"Enter name of input file:  ";
cin>>filename;
cout<<endl;
cout<<"Information in arrays 0- means request, 1- means release."<<endl;
//Input part of the program
/*
I make various arrays/marticies to store the all of the information in the input file
The first two are obvious (max and available) are basically the same as what you would think. 
n and m are the same n and m from the input file.
The process matrix has 3 dimensions, the first dimension is for processes
 The "40" dimension, is to store the request/release commands. I'm assuming there is less than 40 per process. 
 The m+1 dimension is to store the single commands. 
 The command is m numbers long, plus a number to determine if it's a request or release. 
*/
ifstream in(filename);
in>>m;//number of resources
in.ignore(100,'\n');
in>>n;//number of processes
available=new int[m];
work=new int[m];
max=new int*[n];
for(i=0;i<n;i++)
{
	max[n][i]=new int[m];
}//end for

need=new int*[n];
for(i=0;i<n;i++)
{
	need[n][i]=new int[m];
}//end for
allocated=new int*[n];
for(i=0;i<n;i++)
{
	allocated[n][i]=new int[m];
}//end for
finish=new int[n];
need=max;
for(i=0;i<n;i++)
{
	for(j=0;j<m;j++)
	{
		allocated[i][j]=0;
	}//end j for
}//end i for
for(i=0;i<m;i++)
{
	in.ignore(100,'=');
	in>>available[i];
	cout<<available[i]<<endl;
}//end for
for(i=0;i<n;i++)
{
	for(j=0;j<m;j++)
	{
		in.ignore(100,'=');
		in>>max[n][m];
		cout<<max[n][m]<<" ";
	}//end j for
	cout<<endl;
}//end i for



process = new int**[n];

// Allocate an array for each element of the first array
for(i = 0; i < n; ++i)
{
    process[i] = new int*[m+1];

    // Allocate an array of integers for each element of this array
    for(j=0; j < m+1; ++j)
    {
        process[i][m+1] = new int[40];
    }//end j for
}//end i for




process=new int[n,(m+1),40];
for(i=0;i<n;i++)
{//loop for processes
	in.ignore(100,' ');
	in>>d;
	in.ignore(100,'\n');
	in.ignore(100,' ');
	in>>c;
	if(d<c)cout<<"Error d<c"<<endl;
	if(c>39)cout<<"Not enough space to store requests/releases"<<endl;
	process[i,0,0]=d;
	process[i,1,0]=c;
	cout<<process[i][0][0]<<" "<<process[i][1][0]<<endl;
	for(j=1;j<(c+1);j++)
	{
		in>>cha[0];
		in>>cha[1];
		in>>cha[2];
		in>>cha[3];
		in>>cha[4];
		in>>cha[5];
		in>>cha[6];
		if(cha[6]=='t')//last letter in request is t
		{
			process[i,0,j]=0;
			cout<<"0-";
		}//end if
		else if(cha[6]=='e')//last letter in release is e
		{
			process[i,0,j]=1;
			cout<<"1-";
		}//end else if
		else cout<<"Error reading request/release"<<endl;
		in.ignore(100,'(');
		for(k=0;k<m;k++)
		{
			if(k!=0)in.ignore(100,',');
			in>>a;
			process[i,k+1,j]=a;
			cout<<process[i,k+1,j]<<" ";
		}//end k for
		in.ignore(10,')');
		cout<<endl;
	}//end j for

}//end i for

// Done with input
/*
Now this part of the algorithm is to do the process requesting and deadlock detection

*/
cout<<"Now time for process handling"<<endl;
status=new int[n];
/*status is an array that tells the status of each process 0 means hasn't been touched,
1 through c means it's pending and the that number request
100 means it's complete*/
for(i=0;i<n;i++)status[i]=0;//create new array and set all elements to 0
while(control<1||control>2)//don't want them to press anything other than 1 or 2
{
cout<<"SJF(press 1) or EDF(press 2) :";
cin>>control;
cout<<endl;
}//end while
cout<<process[0,1,0];
while(loopend!=1)
{

//Process requesting Algo
switch(control)
{
case 1:
	//Shortest Job First, go through all processes with status !=100 and find the one with the lowest c
	min=100;//initialize to some high number that we know is greater than all c's
	for(i=0;i<n;i++)
	{
		if(status[i]!=100){
			if(process[i,1,0]<min) {
				minproc=i;
				min=process[i,1,0];
			}//end inner if
		}//end if
	}//end for
	cout<<"Shortest process "<<minproc+1<<" has computation time "<<min<<endl;
	break;
case 2:
	//Earliest Deadline First- same thing as SJF but instead of min comp time its min deadline
	min=100;
	for(i=0;i<n;i++)
	{
		if(status[i]!=100){
			if(process[i,0,0]<min) {
				minproc=i;
				min=process[i,0,0];
			}//end inner if
		}//end if
	}//end for
	cout<<"Earliest deadline "<<minproc+1<<" has deadline "<<min<<endl;
	break;
}//end switch

//Execute Process & Deadlock detection algortihm
for(i=status[minproc];i<min;i++)//this for loop is for each request/release
{
	if(process[minproc,0,i]==0)//means its a request, for loop for request (Bankers Algo from book)
		//because the algorithm is in the book, I'm going to assume you know it and not comment on the psudocode taken from book
	{
		for(j=0;j<m;j++)//this for loop is for each resource requested within the command
		{
			if(process[minproc,j+1,i] > need[minproc,j]) cout<<"error at "<<j<<endl;
			else if(process[minproc,j+1,i] > available[j]){
				status[minproc]=i;
				stopped=j;
				cout<<"stop at command"<<i<<"resource "<<j<<endl;
				i=min;
				j=m+1;
			}//end else if
			else{//pretend
				available[j]=available[j]-process[minproc,j+1,i];
				allocated[minproc,j]=allocated[minproc,j]+process[minproc,j+1,i];
				need[minproc,j]=need[minproc,j]-process[minproc,j+1,i];//I add +1 to j every time because in process matrix the first entry in that dimension is the request/release flag
				work[j]=available[j];
			for(k=0;k<n;k++)
			{
				finish[k]=1;
			}//end k for
			while(control1!=1)
				{
					control1=1;//this means you stop if the if statement condition isn't met
				for(k=0;k<n;k++)
				{
				
					if(finish[k]=1&&need[k,j]<=work[j])
					{
						work[j]=work[j]+allocated[k,j];
						finish[k]=0;
						control1=2;//keep going on while if you find something
					}//end if
				}//end k for
			}//end while
			notsafe=1;//set it to "safe" before checking if it's not safe
			for(k=0;k<n;k++)
			{
				if(finish[k]=1)notsafe=0;
			}//end k for	
			if(notsafe=0)
			{
				status[minproc]=i;
				stopped=j;
				cout<<"stop at command"<<i<<"resource "<<j<<" because system not safe"<<endl;
				i=min;
				j=m+1;
			}//end if
			}//end else
		}//end j for
		if(j==(m+1))
		{
			for(j=0;j<stopped;j++)
			{//add and subtract all the values if request is stopped halfway through because the request didn't go through
				available[j]=available[j]+process[minproc,j+1,i];
				allocated[minproc,j]=allocated[minproc,j]-process[minproc,j+1,i];
				need[minproc,j]=need[minproc,j]+process[minproc,j+1,i];
			}//end j for
		}//end if
		
	}//end if
	else//means its a release
	{
		for(j=0;j<m;j++)//this for loop is for each resource requested within the command
		{
			available[j]=available[j]+process[minproc,j+1,i];
			allocated[minproc,j]=allocated[minproc,j]-process[minproc,j+1,i];
			need[minproc,j]=need[minproc,j]+process[minproc,j+1,i];
		}//end j for
	}//end else
	time++;//incriment time no matter what
	if(j==m-1)
	{
	status[minproc]=100;//this means it's completed process
	cout<<"Process "<<minproc+1<<" completed at time "<<time<<endl;
	cout<<"Deadline was "<<process[minproc,0,0]<<endl;
	if(process[minproc,0,0]>time) cout<<"Deadline missed: process "<<minproc<<endl;
	for(k=0;k<m;k++)//to release the rest of the resources
	{
		need[minproc,k]=max[minproc,k];
		allocated[minproc,k]=0;
	}//end k for
	cout<<"Allocated"<<endl;
	for(k=0;k<n;k++)
	{
		for(l=0;l<m;k++)
		{
			cout<<allocated[k,l]<<" ";
		}//end l for
		cout<<endl;
	}//end k for
		cout<<"Need"<<endl;
	for(k=0;k<n;k++)
	{
		for(l=0;l<m;k++)
		{
			cout<<need[k,l]<<" ";
		}//end l for
		cout<<endl;
	}//end k for
		cout<<"Available"<<endl;
	for(k=0;k<n;k++)
	{
	
	cout<<available[k]<<" ";
	}//end k for
	cout<<endl;
	}//end if
}//end i for





//Check if done, if all processes complete, the for loop will keep loopend at 1
loopend=1;
for(i=0;i<n;i++)
{
	if(status[n]!=100)loopend=0;
}//end for
}//end while
system("pause");


}//end main


Edit: What you posted works. Now I'm going through and fixing the rest of it and so far no issues. Hopefully once I fix that I'm done.

This post has been edited by agentkirb: 09 March 2010 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#10 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Array pointer issues

Posted 09 March 2010 - 07:35 PM

Here's a 3D array taken (and modified) from Multi-Dimensional Arrays:

#include "stdafx.h"

#define HEIGHT 1
#define WIDTH 3
#define DEPTH 20

int _tmain(int argc, _TCHAR* argv[])
{
	int ***p2DArray;

	// Allocate memory
	p2DArray = new int**[HEIGHT];

	for (int i = 0; i < HEIGHT; ++i) {
		p2DArray[i] = new int*[WIDTH];

		for (int j = 0; j < WIDTH; ++j)
			p2DArray[i][j] = new int[DEPTH];
	}

	// Initialize array
	for (int i = 0; i < HEIGHT; ++i)
		for (int j = 0; j < WIDTH; ++j)
			for (int k = 0; k < DEPTH; k++)
				p2DArray[i][j][k] = 0;

	// Assign values
	p2DArray[0][0][0] = 5;
	p2DArray[0][2][0] = 3;

	for (int i = 0; i < HEIGHT; ++i) {
		for (int j = 0; j < WIDTH; ++j) {
			for (int k = 0; k < DEPTH; k++)
				cout << p2DArray[i][j][k] << " ";

			cout << endl;
		}
	}

	// De-Allocate memory to prevent memory leak
	for (int i = 0; i < HEIGHT; ++i) {
		for (int j = 0; j < WIDTH; ++j)
			delete [] p2DArray[i][j];

		delete [] p2DArray[i];
	}
	delete [] p2DArray;

	cin.sync();
	cin.get();
	return 0;
}



Output:

Quote

5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Was This Post Helpful? 0
  • +
  • -

#11 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Array pointer issues

Posted 09 March 2010 - 07:38 PM

Here is the compilable code. I have taken all the syntax errors out of it but there may be logic errors. I'll leave you to determine those as that code looks like h*ll. No offense but you need to use descriptive comments. Not "for", "end for", etc.

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int main()
{
char filename[20];
int m=0,n=0,i=0,j=0,k=0,l=0,a=1,d=0,c=0,control=0,control1=0,min,minproc,loopend=0,time=0,stopped,notsafe=1;
char cha[7]; 
int *available;
int **max;
int **need;
int **allocated;
int ***process;
int *status;
int *finish;
int *work;
cout<<"Enter name of input file:  ";
cin>>filename;
cout<<endl;
cout<<"Information in arrays 0- means request, 1- means release."<<endl;
//Input part of the program
/*
I make various arrays/marticies to store the all of the information in the input file
The first two are obvious (max and available) are basically the same as what you would think. 
n and m are the same n and m from the input file.
The process matrix has 3 dimensions, the first dimension is for processes
 The "40" dimension, is to store the request/release commands. I'm assuming there is less than 40 per process. 
 The m+1 dimension is to store the single commands. 
 The command is m numbers long, plus a number to determine if it's a request or release. 
*/
ifstream in(filename);
in>>m;//number of resources
in.ignore(100,'\n');
in>>n;//number of processes
available=new int[m];
work=new int[m];
max=new int*[n];
for(i=0;i<n;i++)
{
        max[i] = new int[m];
}//end for

need=new int*[n];
for(i=0;i<n;i++)
{
        need[i] = new int[m];
}//end for
allocated=new int*[n];
for(i=0;i<n;i++)
{
        allocated[i] = new int[m];
}//end for
finish=new int[n];
need=max;
for(i=0;i<n;i++)
{
        for(j=0;j<m;j++)
        {
                allocated[i][j]=0;
        }//end j for
}//end i for
for(i=0;i<m;i++)
{
        in.ignore(100,'=');
        in>>available[i];
        cout<<available[i]<<endl;
}//end for
for(i=0;i<n;i++)
{
        for(j=0;j<m;j++)
        {
                in.ignore(100,'=');
                in>>max[n][m];
                cout<<max[n][m]<<" ";
        }//end j for
        cout<<endl;
}//end i for



process = new int**[n];

// Allocate an array for each element of the first array
for(i = 0; i < n; ++i)
{
    process[i] = new int*[m+1];

    // Allocate an array of integers for each element of this array
    for(j=0; j < m+1; ++j)
    {
        process[i][j] = new int[40];
    }//end j for
}//end i for




//process=new int[n,(m+1),40];

for(i=0;i<n;i++)
{//loop for processes
        in.ignore(100,' ');
        in>>d;
        in.ignore(100,'\n');
        in.ignore(100,' ');
        in>>c;
        if(d<c)cout<<"Error d<c"<<endl;
        if(c>39)cout<<"Not enough space to store requests/releases"<<endl;
        process[i][0][0]=d;
        process[i][1][0]=c;
        cout<<process[i][0][0]<<" "<<process[i][1][0]<<endl;
        for(j=1;j<(c+1);j++)
        {
                in>>cha[0];
                in>>cha[1];
                in>>cha[2];
                in>>cha[3];
                in>>cha[4];
                in>>cha[5];
                in>>cha[6];
                if(cha[6]=='t')//last letter in request is t
                {
                        process[i][0][j]=0;
                        cout<<"0-";
                }//end if
                else if(cha[6]=='e')//last letter in release is e
                {
                        process[i][0][j]=1;
                        cout<<"1-";
                }//end else if
                else cout<<"Error reading request/release"<<endl;
                in.ignore(100,'(');
                for(k=0;k<m;k++)
                {
                        if(k!=0)in.ignore(100,',');
                        in>>a;
                        process[i][k+1][j]=a;
                        cout<<process[i][k+1][j]<<" ";
                }//end k for
                in.ignore(10,')');
                cout<<endl;
        }//end j for

}//end i for

// Done with input
/*
Now this part of the algorithm is to do the process requesting and deadlock detection

*/
cout<<"Now time for process handling"<<endl;
status=new int[n];
/*status is an array that tells the status of each process 0 means hasn't been touched,
1 through c means it's pending and the that number request
100 means it's complete*/
for(i=0;i<n;i++)status[i]=0;//create new array and set all elements to 0
while(control<1||control>2)//don't want them to press anything other than 1 or 2
{
cout<<"SJF(press 1) or EDF(press 2) :";
cin>>control;
cout<<endl;
}//end while
cout<<process[0][1][0];
while(loopend!=1)
{

//Process requesting Algo
switch(control)
{
case 1:
        //Shortest Job First, go through all processes with status !=100 and find the one with the lowest c
        min=100;//initialize to some high number that we know is greater than all c's
        for(i=0;i<n;i++)
        {
                if(status[i]!=100){
                        if(process[i][1][0]<min) {
                                minproc=i;
                                min=process[i][1][0];
                        }//end inner if
                }//end if
        }//end for
        cout<<"Shortest process "<<minproc+1<<" has computation time "<<min<<endl;
        break;
case 2:
        //Earliest Deadline First- same thing as SJF but instead of min comp time its min deadline
        min=100;
        for(i=0;i<n;i++)
        {
                if(status[i]!=100){
                        if(process[i][0][0]<min) {
                                minproc=i;
                                min=process[i][0][0];
                        }//end inner if
                }//end if
        }//end for
        cout<<"Earliest deadline "<<minproc+1<<" has deadline "<<min<<endl;
        break;
}//end switch

//Execute Process & Deadlock detection algortihm
for(i=status[minproc];i<min;i++)//this for loop is for each request/release
{
        if(process[minproc][0][i]==0)//means its a request, for loop for request (Bankers Algo from book)
                //because the algorithm is in the book, I'm going to assume you know it and not comment on the psudocode taken from book
        {
                for(j=0;j<m;j++)//this for loop is for each resource requested within the command
                {
                        if(process[minproc][j+1][i] > need[minproc][j]) cout<<"error at "<<j<<endl;
                        else if(process[minproc][j+1][i] > available[j]){
                                status[minproc]=i;
                                stopped=j;
                                cout<<"stop at command"<<i<<"resource "<<j<<endl;
                                i=min;
                                j=m+1;
                        }//end else if
                        else{//pretend
                                available[j]=available[j]-process[minproc][j+1][i];
                                allocated[minproc][j]=allocated[minproc][j]+process[minproc][j+1][i];
                                need[minproc][j]=need[minproc][j]-process[minproc][j+1][i];//I add +1 to j every time because in process matrix the first entry in that dimension is the request/release flag
                                work[j]=available[j];
                        for(k=0;k<n;k++)
                        {
                                finish[k]=1;
                        }//end k for
                        while(control1!=1)
                                {
                                        control1=1;//this means you stop if the if statement condition isn't met
                                for(k=0;k<n;k++)
                                {
                                
                                        if(finish[k]==1&&need[k][j]<=work[j])
                                        {
                                                work[j]=work[j]+allocated[k][j];
                                                finish[k]=0;
                                                control1=2;//keep going on while if you find something
                                        }//end if
                                }//end k for
                        }//end while
                        notsafe=1;//set it to "safe" before checking if it's not safe
                        for(k=0;k<n;k++)
                        {
                                if(finish[k]==1)notsafe=0;
                        }//end k for    
                        if(notsafe==0)
                        {
                                status[minproc]=i;
                                stopped=j;
                                cout<<"stop at command"<<i<<"resource "<<j<<" because system not safe"<<endl;
                                i=min;
                                j=m+1;
                        }//end if
                        }//end else
                }//end j for
                if(j==(m+1))
                {
                        for(j=0;j<stopped;j++)
                        {//add and subtract all the values if request is stopped halfway through because the request didn't go through
                                available[j]=available[j]+process[minproc][j+1][i];
                                allocated[minproc][j]=allocated[minproc][j]-process[minproc][j+1][i];
                                need[minproc][j]=need[minproc][j]+process[minproc][j+1][i];
                        }//end j for
                }//end if
                
        }//end if
        else//means its a release
        {
                for(j=0;j<m;j++)//this for loop is for each resource requested within the command
                {
                        available[j]=available[j]+process[minproc][j+1][i];
                        allocated[minproc][j]=allocated[minproc][j]-process[minproc][j+1][i];
                        need[minproc][j]=need[minproc][j]+process[minproc][j+1][i];
                }//end j for
        }//end else
        time++;//incriment time no matter what
        if(j==m-1)
        {
        status[minproc]=100;//this means it's completed process
        cout<<"Process "<<minproc+1<<" completed at time "<<time<<endl;
        cout<<"Deadline was "<<process[minproc][0][0]<<endl;
        if(process[minproc][0][0]>time) cout<<"Deadline missed: process "<<minproc<<endl;
        for(k=0;k<m;k++)//to release the rest of the resources
        {
                need[minproc][k]=max[minproc][k];
                allocated[minproc][k]=0;
        }//end k for
        cout<<"Allocated"<<endl;
        for(k=0;k<n;k++)
        {
                for(l=0;l<m;k++)
                {
                        cout<<allocated[k][l]<<" ";
                }//end l for
                cout<<endl;
        }//end k for
                cout<<"Need"<<endl;
        for(k=0;k<n;k++)
        {
                for(l=0;l<m;k++)
                {
                        cout<<need[k][l]<<" ";
                }//end l for
                cout<<endl;
        }//end k for
                cout<<"Available"<<endl;
        for(k=0;k<n;k++)
        {
        
        cout<<available[k]<<" ";
        }//end k for
        cout<<endl;
        }//end if
}//end i for





//Check if done, if all processes complete, the for loop will keep loopend at 1
loopend=1;
for(i=0;i<n;i++)
{
        if(status[n]!=100)loopend=0;
}//end for
}//end while
system("pause");


}//end main

Was This Post Helpful? 1
  • +
  • -

#12 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 07:45 PM

Thank you so much for your help.

Yeah, I know my code looks pretty bad. At least I indented this time, I used to have a really bad habit of not doing that. And I probably should've made a few functions to clean up the main() a little.
Was This Post Helpful? 0
  • +
  • -

#13 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 07:50 PM

for(i=0;i<n;i++)
{//loop for processes
	in.ignore(100,' ');
	in>>d;
	in.ignore(100,'\n');
	in.ignore(100,' ');
	in>>c;
	if(d<c)cout<<"Error d<c"<<endl;
	if(c>39)cout<<"Not enough space to store requests/releases"<<endl;
	process[i][0][0]=d;//exception:Access violation writing location 0xcdcdcdcd.
	process[i][1][0]=c;
	cout<<process[i][0][0]<<" "<<process[i][1][0]<<endl;
...//end for loop a ways down



It's giving me an exception at that line when I debug. Is that the correct way to access that array?

Edit: This means that up to that point is correct. The code before that couts numbers are outputting the correct numbers. So at least thats good news.

This post has been edited by agentkirb: 09 March 2010 - 07:56 PM

Was This Post Helpful? 0
  • +
  • -

#14 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Array pointer issues

Posted 09 March 2010 - 07:51 PM

Quote

At least I indented this time


Nice choice, It may have deterred me slightly if it wasn't indented. ;)

A few functions would help as well.

Edit: Yeah that's correct. I'll look at it again.

Okay I don't have a test input file. If you could attach a copy of a sample test file, I'll try and help.

This post has been edited by eker676: 09 March 2010 - 07:59 PM

Was This Post Helpful? 1
  • +
  • -

#15 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Array pointer issues

Posted 09 March 2010 - 08:08 PM

View Posteker676, on 09 March 2010 - 06:51 PM, said:

Quote

At least I indented this time


Nice choice, It may have deterred me slightly if it wasn't indented. ;)

A few functions would help as well.

Edit: Yeah that's correct. I'll look at it again.

Okay I don't have a test input file. If you could attach a copy of a sample test file, I'll try and help.


3 /* number of resources*/
4 /* number of processes*/
available[1] = 5
available[2] = 5
available[3] = 5
max[1,1] = 4
max[1,2] = 2
max[1,3] = 4
max[2,1] = 4
max[2,2] = 2
max[2,3] = 3
max[3,1] = 2
max[3,2] = 3
max[3,3] = 3
max[4,3] = 5
max[4,3] = 2
max[4,3] = 5
process_1:
deadline_1 5
computation_time_1 3
request(2,1,2)
request(2,1,2)
release(4,2,4)
end
process_2:
deadline_2 9
computation_time_2 5
request(1,0,1)
request(1,0,0)
request(2,2,2)
release(3,1,2)
release(1,1,1)
end
process_3:
deadline_3 7
computation_time_3 4
request(1,1,1)
request(0,1,1)
request(1,1,1)
release(2,3,3)
end
process_4:
deadline_4 10
computation_time_4 6
request(2,1,2)
request(2,1,2)
request(1,0,1)
release(5,2,5)
request(1,0,0)
request(1,1,1)
end

I noticed that when I used the version you posted it gave me an exception back when I first tried to assign something to max[0][0].

The version I debugged that gave me the error initialized the pointers right before the for loop that created them (like in the example), rather than all at the beginning like I had done at first.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2