Declared a structure variable which is a dynamic array of another structure. Why is the following C++ program being crashed?

I have declared a structure variable which is a dynamic array of another structure but the program crashes every time. Where am I doing wrong? What are the necessary steps to be taken? I am using DEVC++.

#include<iostream>
#include<cstdlib>

using namespace std;

struct Project{
    int pid;
    string name;
};

struct employee{
    int eid;
    string name;
    Project *project_list;
};

int main(){
   struct employee e;
   e.eid = 123;
   e.name = "789";
   e.project_list = (Project *)malloc(2 * sizeof(Project));

   e.project_list[0].pid = 100;
   e.project_list[0].name = "Game";
}

Source: Windows Questions C++

LEAVE A COMMENT