c - Array of nested structures -


i have huge arrays of nested structures makes impossible allot kind of space , forces me use heap. facing difficulties using malloc. gist of problem below.

struct year_of_joining {     struct district     {         struct colleges         {             struct departments             {                 struct sections                 {                     struct students                     {                         int sex;                     }student[100];                 }section_no[8];             }department_no[17];         }college[153];     }dist[13]; }; 

if use

int main() {     int i=0;         struct year_of_joining** year;     year = malloc(100 * sizeof(struct year_of_joining));     (i = 0; < 100; i++)     {         year[i] = malloc(sizeof(struct year_of_joining));     }      year[1]->dist[0].college[0].department_no[0].section_no[0].student[8].sex = 1;//works fine     printf("%d", year[1]->dist[0].college[0].department_no[0].section_no[0].student[8].sex);//prints 1     free(year);     return 0; } 

it works fine when create pointer pointer dist year_of_joining , use indirection operator not compile:

year[1]->dist[2]->college[0].department_no[0].section_no[0].student[8].sex = 9;//error c2039: 'dist' : not member of 'year_of_joining'  

how solve this? on right track?

i think way off track here.

note single struct year_of_joining approximately 100 mib of data. array of 100 such structures requires approximately 10 gib of data (and recording sex of students — no other information @ all).

struct year_of_joining** year; year = malloc(100 * sizeof(struct year_of_joining)); 

this memory allocation allocates enough space millions of pointers. intended use:

struct year_of_joining *year = malloc(100 * sizeof(struct year_of_joining));  struct year_of_joining *year = malloc(100 * sizeof(*year)); 

this allocates 100 years worth of structure.

however, seems improbable have 13 districts, each of has 153 colleges, each college having 17 departments, each of has 8 sections, each section having 100 students. corresponds on 25 million students every year!

you going need vastly more flexible arrangement, each of structures contains pointer list of nested structures, can have bigger sections smaller colleges, etc. need work more along lines of:

struct students {     char name[32];     int sex;     // ... , other data ... };  struct sections {     char name[32];     // ... , other data ...     int n_students;     struct students *students; };  struct departments {     char name[32];     int n_sections;     struct sections *sections; }  struct colleges {     char name[32];     // ... , other data ...     int n_departments;     struct departments *departments; };  struct district {     char name[32];     // ... , other data ..     int n_colleges;     struct college *colleges; };  struct year_of_joining {     int  year;     // ... , other data ...     int  n_districts;     struct district *districts; }; 

even feels not entirely correct, better way of organizing data original, if because if department has 1 section , enrolls ten students (because minority-interest department), allocates enough space 1 section , ten students, rather allocating space 800 students , 8 sections.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -