C Program use LARGE STRUCTURE & ARRAY

kARTechnology

Sony " VA" "IO"
Hi
completed C subject in my Engineering:-D...
Today I developed a code, I'm getting array is too large and structure is too large.but i need large structures, or is there any alternative way?

the aim is to STORE log records(roll,time) from a fingerprint-based time attendance soft which saves data daywise in plain text format..
and compute the salary per day, if he's late/early going/no outpunch etc


error is in

Code:
struct year
{	struct month
		{
		struct day
			{
			struct details
				{
						char	name[25];
						int	hour[40];
						int	min[40];
						int	mins[40];
						int	rec;
						int	totmins;
						int	totsal;
						char	status;
						int	temp;
				}data[100];
			}day[31];
		}mon[12];
}yr[2];

actual full code of my program[in development...
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <time.h>

void dummy(float *a) 
{
    float b=*a; //perform some floating access
    dummy (&b); //calling a floating point function
}


main()
{

  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

/*--------- data+details file structure --------*/
struct year
{	struct month
		{
		struct day
			{
			struct details
				{
						char	name[25];
						int	hour[40];
						int	min[40];
						int	mins[40];
						int	rec;
						int	totmins;
						int	totsal;
						char	status;
						int	temp;
				}data[100];
			}day[31];
		}mon[12];
}yr[2];



/*--------- data file structure --------*/
struct details
{ 	int empnr;
	char name[25];
	int salary;
}person[100];




int y1,m1,d1,y2,m2,d2,y,m,d,y3,y4, i=0,noofemp=0,t1,t2,t3;
char fname[20];
clrscr();


printf("Edit \"empdetails.txt\" for adding new or changing roll no, name, salary per day.");

printf("Enter begining date: MM DD YY:");
scanf("%d%d%d",&d1,&m1,&y1);
printf("Enter ending date: MM DD YY:");
scanf("%d%d%d",&d2,&m2,&y2);
if(y1-y2>1)
printf("Cant input greater than years");
printf("Reading Data.");
d=d1;m=m1;y=y1;
//if(y1==y2)
//{y3=0;y4=0;}
//else
//{y3=0;y4=1;}



	FILE *emp_file;
	FILE *calcemp_file;
	emp_file = fopen("emp.txt","r");
	calcemp_file = fopen("calcweek.txt","w");

if(emp_file==NULL)
{
	printf("\nERROR Accessing source file for computing salary.\n\nPlease use a text document with the following info:\n Roll	Name	   Salary/Day\n\n\n\nFilename of the text document MUST BE \"empdetails\"\ (without the quotes\) ");
	getch();
	exit(0);
}

/*--------- get data from file --------*/
while(fscanf(emp_file,"%d%s%d", &person[i].empnr, person[i].name,&person[i].salary)==3)
{
i++ ;
noofemp++;
printf(".");
}
fclose(emp_file);


/*--------- get details from file --------*/
for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{
	sprintf(fname,"%02d-%02d-%02d.dat",d,m,y);
	emp_file = fopen(fname,"r");
if(emp_file==NULL)
{	printf("\nERROR Accessing %s for computing salary",fname);
	getch();
	exit(0);
}
	fclose(emp_file);
	printf(".");
}
}
}

getch();
}
 

SaiyanGoku

kamehameha!!
what compiler/IDE are you using?
Total size in memory is over 20 million bytes for the structure and it will cause unexpected termination.
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
what compiler/IDE are you using?
Total size in memory is over 20 million bytes for the structure and it will cause unexpected termination.

will run the compiled final exe in a windows 8.1 32bit 2gb ram
currently using dosbox (set ram as 64M) with TC++ v3.0 Borland
 
That struct is just too large. What you should do is use buffering technique - fetch a few hundred lined from file, process then, delete them from memory and fetch new lines. Don;t load the whole file into memory at once.
 

layzee

■■■■■■
That struct is just too large. What you should do is use buffering technique - fetch a few hundred lined from file, process then, delete them from memory and fetch new lines. Don;t load the whole file into memory at once.

+1
A divide-and-conquer(ish) approach seems to be the best solution here when there is a constraint on memory availability.
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
+1
A divide-and-conquer(ish) approach seems to be the best solution here when there is a constraint on memory availability.

could you please elaborate, like a eg for the code...do you mean malloc?
the final output will be like this in a text file, for each employee

Code:
Roll no:_1_ name:___test1______salary per day:_____100_____
---------------------------------------------------------------------------------------------------------------
date        | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | total duration | status | salary
21-01-14
22-01-14
23-01-14
                                                                                                                                              total salary______

Roll no:_2_ name:___test2_____salary per day:_____200_____
---------------------------------------------------------------------------------------------------------------
date        | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | total duration | status | salary
21-01-14
22-01-14
23-01-14
                                                                                                                                              total salary______
where in means the time at which he came inside, out means when he went outside.
 

Vyom

The Power of x480
Staff member
Admin
The way the structure is declared seems to be the culprit to me. The problem with your approach seems to be that you are trying to create n numbers of objects within the same program and all of those are nested.

Assuming that each variable takes following size in memory:
Code:
char name[25]; (25 bytes)
int	hour[40]; (40*2 = 80 bytes)
int	min[40]; (40*2 = 80 bytes)
int	mins[40]; (40*2 = 80 bytes)
int	rec; (2 bytes)
int	totmins; (2 bytes)
int	totsal; (2 bytes)
char	status; (1 byte)
int	temp; (2 bytes)

The total number of memory you are trying to allocate in an object of data = 274 bytes;
data[100] = 27400 bytes.
day[31] = data[100] * 31 = 849400 bytes
mon[12] = day[31] * 12 = 10192800 bytes
year[2] = mon[12] * 2 = 20385600 bytes = 19.44 MB (!)

As per this page, it seems your structure is very memory intensive, hence crashing.

I think you need to re-think the architecture of the program.
Instead of making multi-level structure you can try following method:

Code:
struct date
{
	int day, month, year;
};

struct Employee_Info
{
	char	name[25];
	int	hour[40];
	int	min[40];
	int	mins[40];
	int	rec;
	int	totmins;
	int	totsal;
	char	status;
	int	temp;
	date	date_obj;
};

struct Employee_Details
{
	int empnr;
	char name[25];
	int salary;
	Employee_Info empinfo;
};


Then you can use the object of Employee_Details like:
Code:
Employee_Details Emp1, Emp2;
Or make an array:
Code:
Employee_Details Emp[100];

Now you can access a particular employees details as:
Code:
Emp1.empnr;
Emp1.name;
Emp1.salary;
Emp1.empinfo.hour;
...
Emp1.empinfo.date_obj.day;
Emp1.empinfo.date_obj.month;
Emp1.empinfo.date_obj.year;
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
The way the structure is declared seems to be the culprit to me. The problem with your approach seems to be that you are trying to create n numbers of objects within the same program and all of those are nested.

Assuming that each variable takes following size in memory:
Code:
char name[25]; (25 bytes)
int	hour[40]; (40*2 = 80 bytes)
int	min[40]; (40*2 = 80 bytes)
int	mins[40]; (40*2 = 80 bytes)
int	rec; (2 bytes)
int	totmins; (2 bytes)
int	totsal; (2 bytes)
char	status; (1 byte)
int	temp; (2 bytes)

The total number of memory you are trying to allocate in an object of data = 274 bytes;
data[100] = 27400 bytes.
day[31] = data[100] * 31 = 849400 bytes
mon[12] = day[31] * 12 = 10192800 bytes
year[2] = mon[12] * 2 = 20385600 bytes = 19.44 MB (!)

As per this page, it seems your structure is very memory intensive, hence crashing.

I think you need to re-think the architecture of the program.
Instead of making multi-level structure you can try following method:

Code:
struct date
{
	int day, month, year;
};

struct Employee_Info
{
	char	name[25];
	int	hour[40];
	int	min[40];
	int	mins[40];
	int	rec;
	int	totmins;
	int	totsal;
	char	status;
	int	temp;
	date	date_obj;
};

struct Employee_Details
{
	int empnr;
	char name[25];
	int salary;
	Employee_Info empinfo;
};


Then you can use the object of Employee_Details like:
Code:
Employee_Details Emp1, Emp2;
Or make an array:
Code:
Employee_Details Emp[100];

Now you can access a particular employees details as:
Code:
Emp1.empnr;
Emp1.name;
Emp1.salary;
Emp1.empinfo.hour;
...
Emp1.empinfo.date_obj.day;
Emp1.empinfo.date_obj.month;
Emp1.empinfo.date_obj.year;

you almost came close,

so for example to get the data of an employee such
the time he came in
---
to get the 1st hour/1st fingerprint given time
hours(the time of the day, here hours represent 19:15) in 24-01-14 date of roll no 2,
Code:
year[14].month[1].day[24].emp[2].hours[0][I]
or this way of structure also ok[/I]
emp[2].year[14].month[1].day[24].min[0]
---

to get the 1st minutes/1st fingerprint given time
minutes(the time of the day, here minutes represent 19:15) in 24-01-14 date of roll no 2,
Code:
year[14].month[1].day[24].emp[2].min[0][I]
or this way of structure also ok[/I]
emp[2].year[14].month[1].day[24].min[0]
---
 

aaruni

The Linux Guy
since [MENTION=77264]Vyom[/MENTION] defined his structure as Employee_Details -> Employee_Info -> date ( -> means containing, in this case), you will have to use the structure objects in that order. So,

Code:
Emp[i].empinfo.date_obj.day

Another thing I want to question : why on earth are you doing it on an old, outdated Borland compiler ?
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
since [MENTION=77264]Vyom[/MENTION] defined his structure as Employee_Details -> Employee_Info -> date ( -> means containing, in this case), you will have to use the structure objects in that order. So,

Code:
Emp[i].empinfo.date_obj.day

Another thing I want to question : why on earth are you doing it on an old, outdated Borland compiler ?
I know only c & dos currently, what should i use?


got the code working: didnt complete the final fprintf part still,,,but one bug pls help with this,
:pullhair::pullhair::pullhair:
*i58.tinypic.com/2cdbsqr.jpg
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <time.h>
#include<string.h>

void dummy(float *a)
{
float b=*a; //perform some floating access
dummy (&b); //calling a floating point function
}


/*--------- data+details file structure --------*/

struct day
{
int hour[40];
int min[40];
int mins[40];
int rec;
int totmins;
int totsal;
char status;
int temp;
};
struct month
{ struct day *da;};

struct year
{ struct month *mn;};

struct roll
{
inr rn;
char name[30];
int salary;
struct year *yr;
};



main()
{

time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );




int t1=100,t2=25,t3=12,t4=31, y1,m1,d1, y2,m2,d2, y,m,d, y3,y4, i,j,noofemp=0;
char fname[20],t5[30],t6,t7;
clrscr();

struct roll *rol;
struct year *yr;
struct month *mn;
struct day *da;
rol=(struct roll*)malloc(t1*sizeof(struct roll));
if( rol== NULL )
{printf("Couldn't able to allocate requested memory\n");exit(0);}
yr=(struct year*)malloc(t2*sizeof(struct year));
if( yr== NULL )
{printf("Couldn't able to allocate requested memory\n");exit(0);}
mn=(struct month*)malloc(t3*sizeof(struct month));
if( mn== NULL )
{printf("Couldn't able to allocate requested memory\n");exit(0);}
da=(struct day*)malloc(t4*sizeof(struct day));
if( da== NULL )
{printf("Couldn't able to allocate requested memory\n");exit(0);}


printf("Edit \"empdetails.txt\" for adding new or changing roll no, name, salary per day.");

printf("Enter begining date: MM DD YY:");
if ((scanf("%d%d%d",&d1,&m1,&y1)!=3))
{printf("Invalid input");}

printf("\nEnter ending date: MM DD YY:");
if ((scanf("%d%d%d",&d2,&m2,&y2)!=3))
{printf("Invalid input"); }

if(y1-y2>1)
printf("\nCant input greater than 1 year");

d=d1;m=m1;y=y1;
//if(y1==y2)
//{y3=0;y4=0;}
//else
//{y3=0;y4=1;}



FILE *emp_file;
FILE *calcsal_file;
emp_file = fopen("emp.txt","r");
calcsal_file = fopen("calcsalary.txt","w");

if(emp_file==NULL)
{
printf("\nERROR Accessing source file for computing salary.\n\nPlease use a text document with the following info:\n Roll Name Salary/Day\n\n\n\nFilename of the text document MUST BE \"empdetails\"\ (without the quotes\) ");
getch();
exit(0);
}

/*--------- get data from file --------*/
printf("\nReading Employee Details.");
while(fscanf(emp_file,"%d%s%d",&t1,&t5,&t3)!=EOF)
{

printf("\nRead %d\t\%s\t%d",t1,t5,t3);
strcpy(rol[t1].name,t5);
rol[t1].salary=t3;
printf("\nRead %d\t\%s\t%d",t1,rol[t1].name,rol[t1].salary);

if(noofemp<t1)
noofemp=t1;

}
printf("\nNumber of employees: %d",noofemp);
for(i=1;i<=noofemp;i++)
printf("\n%d\t\%s\t%d",i,rol.name,rol.salary);
getch();
fclose(emp_file);

/*--------- get details from file --------*/


for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{
sprintf(fname,"%02d-%02d-%02d.dat",d,m,y);
emp_file = fopen(fname,"r");

if(emp_file==NULL)
{ printf("\nERROR Accessing %s for computing salary",fname);
getch();
exit(0);
}
printf("\nReading %s\t",fname);
getch();

for(i=1;i<=100;i++)
{
rol.rn=i;
rol.yr[y].mn[m].da[d].rec=0;------------------------>not working as expected, rec is used to denote the no if times in a day he used the fingerprint/put his finger

printf("\n rec of %d is %d %d",rol.rn,rol.yr[y].mn[m].da[d].rec);
}
getch();

while(fscanf(emp_file,"%d%d%d%d",&t1,&t2,&t3,&t4)!=EOF)
{
printf("\n rec of %d is %d %d",t1,rol[t1].yr[y].mn[m].da[d].rec);

rol[t1].yr[y].mn[m].da[d].rec++;

rol[t1].yr[y].mn[m].da[d].hour[rol[t1].yr[y].mn[m].da[d].rec]=t2;
rol[t1].yr[y].mn[m].da[d].min[rol[t1].yr[y].mn[m].da[d].rec]=t3;
rol[t1].yr[y].mn[m].da[d].mins[rol[t1].yr[y].mn[m].da[d].rec]=t4;

printf("\nRead %d %d:%d %d",t1,t2,t3,t4);
printf("\nRead(m) %d %d:%d %d %d %s %d",t1,rol[t1].yr[y].mn[m].da[d].hour[j],rol[t1].yr[y].mn[m].da[d].min[j],rol[t1].yr[y].mn[m].da[d].mins[j],rol[t1].salary,rol[t1].name,rol[t1].yr[y].mn[m].da[d].rec);


}
getch();

fclose(emp_file);
printf(".");




}
}
}
fprintf(calcsal_file,"\n name of company");
fprintf(calcsal_file,"\n Salary Calculator v3.0 Run on: ");
fprintf(calcsal_file,"\n Report From %d-%d-%d to %d-%d-%d",d1,m1,y1,d2,m2,y2);
for(i=1;i<=noofemp;i++)
{
fprintf(calcsal_file,"\n\n Roll: %d Name:%s Salary/Day:%d",i,rol.name,rol.salary);
fprintf(calcsal_file,"\n -----------------------------------------------------------------------------------------------------------------------------------");
fprintf(calcsal_file,"\n Date In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | In Out | Total Hours | Status | Salary / Day | Salary /Hour ");
for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{

fprintf(calcsal_file,"\n %d-%d-%d",d,m,y);
for(j=1;j<=rol.yr[y].mn[m].da[d].rec;j++)
{fprintf(calcsal_file,"\n %02d:%02d ",rol.yr[y].mn[m].da[d].hour[j],rol.yr[y].mn[m].da[d].min[j]);}

}
}
}

}

free(rol);
free(yr);
free(mn);
free(da);

getch();
}
 
Last edited:

Vyom

The Power of x480
Staff member
Admin
I couldn't understand what output window is showing. However I would like to suggest something.

Since you are reading file till you encounter EOF, I suggest you to enter a blank line in the end of the source file. Without a blank line, your program may not be able to read the last line.
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
Re: C Program use LARGE STRUCTURE &amp; ARRAY

I couldn't understand what output window is showing. However I would like to suggest something.

Since you are reading file till you encounter EOF, I suggest you to enter a blank line in the end of the source file. Without a blank line, your program may not be able to read the last line.

i think now you can...
*s10.postimg.org/7o12ju9o7/bug.png

- - - Updated - - -

guys?

the thing is if i increment one member, for eg

rollnum=1;y=15;m=1;d=17
roll[1].yr[y].mn[m].da[d].rec++;

it is incrementing all other members too...
i mean it is doing

roll[2].yr[y].mn[m].da[d].rec++;
roll[3].yr[y].mn[m].da[d].rec++;
roll[4].yr[y].mn[m].da[d].rec++;
roll[5].yr[y].mn[m].da[d].rec++;

why?
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
Re: C Program use LARGE STRUCTURE &amp; ARRAY

bump...anyone???
modified it but getting couldn't allocate memory...
Code:
	for (i = 0; i < 100; i++) {
	  rol[i] = (struct roll * ) malloc(100 * sizeof(struct roll));
	  if (rol[i] == NULL) {
	    printf("%d Couldn't able to allocate requested memory for roll\n", i);
	    getch();
	    exit(0);
	  }
	  for (j = 0; j < 25; j++) {
	    rol[i] - > yr[j] = (struct year * ) malloc(25 * sizeof(struct year));
	    if (rol[i] - > yr[j] == NULL) {
	      printf("%d Couldn't able to allocate requested memory for year\n", i);
	      getch();
	      exit(0);
	    }

	    for (k = 0; k < 12; k++) {
	      rol[i] - > yr[j] - > mn[k] = (struct month * ) malloc(12 * sizeof(struct month));
	      if (rol[i] - > yr[j] - > mn[k] == NULL) {
	        printf("%d Couldn't able to allocate requested memory for month\n", i);
	        getch();
	        exit(0);
	      }

	      for (l = 0; l < 31; l++) {
	        rol[i] - > yr[j] - > mn[k] - > da[l] = (struct day * ) malloc(31 * sizeof(struct day));
	        if (rol[i] - > yr[j] - > mn[k] - > da[l] == NULL) {
	          printf("%d Couldn't able to allocate requested memory for day\n", l);
	          getch();
	          exit(0);
	        }
	      }

	    }

	  }
	}
 

RCuber

The Mighty Unkel!!!
Staff member
sorry couldnt go through the full code/thread, but why have you declared a int array for hours/mins etc?

EDIT: And why are you not using classes? were you specifically asked to use Structures?
 
Last edited:

Vyom

The Power of x480
Staff member
Admin
sorry couldnt go through the full code/thread, but why have you declared a int array for hours/mins etc?

EDIT: And why are you not using classes? were you specifically asked to use Structures?

Cause he's using "C". And I already suggested using a better structure of structures.
This loop inside loop thingy is never memory efficient and consequently error prone.
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
sorry couldnt go through the full code/thread, but why have you declared a int array for hours/mins etc?

EDIT: And why are you not using classes? were you specifically asked to use Structures?

I was taught upto structures...
this is my personal project.
you see fingerprint biometric devices in offices and some places to monitor employee status...the bundled software is crap and doesn't have what i need.
I get a log file daywise from the device like this:
roll, hours:mins, min <----now you understand...sample file looks like...min is nothing but the hours:mins in minute format

filename: 20-01-15.dat
Code:
01	08	01	00538	
23	08	01	00538	
08	08	01	00538	
12	08	01	00538	
06	08	01	00538	
19	08	01	00538	
04	08	01	00538	
27	08	01	00538	
02	08	01	00538	
11	09	01	00544	
05	09	01	00544	
13	09	01	00563	
03	10	01	00623	
11	10	01	00632	
11	10	01	00643	
12	11	01	00691	
12	11	01	00705	
13	12	01	00767	
23	12	01	00770	
08	12	01	00771	
12	12	01	00773	
13	13	01	00780	
27	13	01	00783	
19	13	01	00802	
23	13	01	00821	
02	14	01	00841	
12	14	01	00843	
11	14	01	00847

i can do the remaining math myself if i get the structure to store data correctly in respective variables....anyway tell me how you'd use "classes"
or C++ vectors also ok...
 
Last edited:

RCuber

The Mighty Unkel!!!
Staff member
Offtopic: I haven't worked on C since college, and that's almost 13 years ago!!. may be I worked a little on embedded C but only for a little time. I'm still not sure how people manage coding without classes!! :D

On topic: take a look at this. design - How do you implement a class in C? - Stack Overflow and *www.careercup.com/question?id=14184694

and for the love of god please use functions.
 
OP
kARTechnology

kARTechnology

Sony " VA" "IO"
Offtopic: I haven't worked on C since college, and that's almost 13 years ago!!. may be I worked a little on embedded C but only for a little time. I'm still not sure how people manage coding without classes!! :D

On topic: take a look at this. design - How do you implement a class in C? - Stack Overflow and *www.careercup.com/question?id=14184694

and for the love of god please use functions.

i don't like passing values to functions...will do that after whole program is finished
 

RCuber

The Mighty Unkel!!!
Staff member
i don't like passing values to functions...will do that after whole program is finished

hehe, you gotta start liking functions if you want to be in software development :) everything has to be in function. Which sem are you in anyway?
 
Top Bottom