struct - Error Showing Access Violation in C++ -


struct root {     struct qgroup     {         struct qpost         {             struct qcomment             {                 struct qcomment *next;                 int likes;                 int address;             } qcomment[100];             int likes;             int comments;             int address;         } qpost[100];         int address;         int posts;         int users;     }qgroup[8]; }*root = (struct root *)malloc(sizeof(struct root *)); 

getting access violation error @ following line.

for(int i=0;i<5;i++) root->qgroup[i].address = i*128+1024*1024; 

please me out of this? tried both static allocation , dynamic allocation, both failed in reading data given in above loop. error starting of execution after main()

root = malloc(sizeof(struct root *)); 

this has correct follows:

root = (struct root *)malloc(sizeof(struct root )); 

no need cast struct root * since malloc returns void pointer , can assign other type in c. in case of c++ need cast did.

in case of c++ better use new , delete instead malloc , free. can usage follows:

 root = new root ; 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -