Problem accessing the struct object.

  • Thread starter Peon666
  • Start date
In summary: In particular, you don't assign any values to the memory that these pointers point to. As a result, the values in this memory are garbage values. You could have used calloc instead of malloc to set all of the values to 0.
  • #1
Peon666
108
0
I'm having some problem accessing the struct object in multiple functions. Here's the code:

Code:
#include<stdio.h>
#include <malloc.h>

struct os
{
	char process ;
	int arrival_time;
	int *burst;
	int *CPU_Bursts;
	int *IO_Bursts;
	int l;
	int num_of_ps;
	int total;
};

struct Process
{
	int Response_time;
	int Waiting_time;
	int Turnaround_time;
};

void Get_File ();
void Calculate ();

void Get_File ()
{
	char a;
	int b=0;
	int j;
	FILE *far;
	FILE *f;
	FILE *far2;
	struct os pp;
	pp.num_of_ps=0;
	pp.total=0;
////////////////////////////
	int m;
	char s[500];
	f=fopen ("umer.txt","r");
	if (!f)
		printf ("Could not open the file.\n");
	while (fgets (s,1000,f)!=NULL)
	{
		if (s[m] == 'P')
		{
			pp.num_of_ps++;
		}
		//printf ("%s", s);
	}
	printf ("processes are: %d\n", pp.num_of_ps);
	printf ("\n");
	fclose(f);
	struct os *fast;
	fast=malloc(pp.num_of_ps*sizeof(struct os));

///////////////////////////
	far=fopen("umer.txt","r");
	int i;
	for(i=0;i<pp.num_of_ps;i++)
	{
		fast[i].l=0;
		fscanf(far,"%c",&fast[i].process);
		fscanf(far,"%c",&a);
		fscanf(far,"%d",&fast[i].arrival_time);
		//printf ("Arrival time of process %d is %d\n", i+1, fast[i].arrival_time);         	
		fscanf(far,"%c",&a);
		while (a!='\n')
		{
			fscanf(far,"%c",&a);
		}

		fscanf(far,"%c",&a);
		while (a!='\n')
		{
			if (a>='0')
			{
				b++;
			}
			else if (a=' ')
			{
				b=0;
			}
			if (a!=' '&& b==1)
			{
				pp.total++;
				fast[i].l++;
			}
			fscanf(far,"%c",&a);
		}
		//printf("Bursts for the process %d are %d\n",i+1,fast[i].l);
		fast[i].burst=malloc(fast[i].l*sizeof(int)); 
	}
	fclose(far);
	int q,zo;
	char on;
	far2=fopen("umer.txt","r");
	for (q=0; q<pp.num_of_ps; q++)
	{
		fscanf(far2,"%c",&on);
		while (on!='\n')
		{
			fscanf(far2,"%c",&on);
		}
		fscanf(far2,"%c",&on);
		for (zo=0; zo<fast[q].l; zo++)
		{
			fscanf (far2,"%d",&fast[q].burst[zo]);
			//printf ("%d ", fast[q].burst[zo]);
		}
		printf ("\n");
	}
	//printf ("%d ", pp.total);
}

///////////////I can't access the struct object in this function////////////////

/*void Calculate ()
{
	Get_File();
	struct os obj1;
	struct os *ptr1;
	struct Process obj2;
	struct Process *ptr2;
	int *CPU;
	int *IO;
	int size;
	int i,zo;
	printf ("%d ", obj1.total);      // The value should be 44 but it gives 0. That's the problem
	size=((obj1.total)/2)+1;
	CPU=malloc(size*sizeof(int));
	IO=malloc(size*sizeof(int));
	ptr1=malloc(obj1.num_of_ps*sizeof(struct os));
}*/

int main()
{
	//Get_File ();
	Calculate ();
	return 0;
}

What's the problem?
 
Physics news on Phys.org
  • #2
Specifically, you refer obj1 which is an uninitialized auto variable. However, from a brief look through the code it appears to be somewhat in a mess, so you will probably have more problems.

More generally, I recommend that you take a step back and learn how to work with pointers, function parameters and programming in general in C as this language gives you ample room to mess up in surprising ways if you don't understand it.
 
  • #3
I second what Filip said about getting more knowledgeable with pointers, function parameters, and general programming.

Here are some comments about the Calculate function.
Code:
void Calculate ()
{
	Get_File();
	struct os obj1;
	struct os *ptr1;
	struct Process obj2;
	struct Process *ptr2;
	int *CPU;
	int *IO;
	int size;
	int i,zo;
	printf ("%d ", obj1.total);      // The value should be 44 but it gives 0. That's the problem
	size=((obj1.total)/2)+1;
	CPU=malloc(size*sizeof(int));
	IO=malloc(size*sizeof(int));
	ptr1=malloc(obj1.num_of_ps*sizeof(struct os));
}
  1. obj1 is a local variable that is never initialized, so all of its fields are garbage values; i.e., whatever values happen to be sitting in memory at those locations. ptr1 is not initialized until the last line of code in this function, where you are using it to point to memory allocated by malloc. In the call to malloc, you are using obj1.num_of_ps to determine the number of bytes of memory to allocate, but num_of_ps is not set with a predictable value.
    Likewise, obj2 and ptr2 are never initialized.
  2. i and zo are never initialized, and they aren't used. If a variable isn't used, it shouldn't be there, since it just clutters up your function.
  3. The call to printf displays the value in obj1.total. obj1 has not been initialized, so obj1.total is going to be whatever garbage value happens to be in memory at that time. Why do you think the value there should be 44? You didn't do anything to cause this value to be placed there.
  4. obj1.total is also a garbage value, so size gets set using whatever value happens to be in obj1.total. You should never assign a garbage value to a variable.
  5. CPU and IO are set with addresses pointing to blocks of memory, but you don't do anything with these pointers.
 

1. What does "Problem accessing the struct object" mean?

"Problem accessing the struct object" is an error message commonly encountered in programming languages like C and Java. It indicates that there is an issue with accessing or retrieving data from a structured object, such as a struct or class.

2. What could cause a "Problem accessing the struct object" error?

There are several possible reasons for this error, including incorrect syntax or misspelling when accessing the object, attempting to access a private member of the struct, or attempting to access a struct that has not been properly instantiated.

3. How can I fix a "Problem accessing the struct object" error?

The first step in fixing this error is to carefully review your code and check for any syntax errors or misspellings. If the issue is not immediately apparent, try debugging your code and stepping through it to identify where the error is occurring. You may also need to check the access modifiers of the struct and ensure that it is properly instantiated before attempting to access its members.

4. Can a "Problem accessing the struct object" error be caused by memory issues?

While it is possible for memory issues to cause this error, it is not a common cause. Typically, this error is the result of a coding mistake or issue with accessing a struct object.

5. Are there any specific languages or environments where "Problem accessing the struct object" errors are more common?

This error can occur in any programming language that uses structured objects, but it is more commonly encountered in lower-level languages like C and C++ rather than higher-level languages like Python or JavaScript. It can also be more common in environments where memory management is critical, such as embedded systems or real-time applications.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
999
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
9K
  • Programming and Computer Science
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top