How to create unknown no of objects at run time in C++, I am reading data from a text file and don't want to waste any memory i.e No extra objects and this has to be done in a function.
Player* g_data()
{
system("cls");
char name[40];int level;
fstream file;
file.open("data.txt",ios::app|ios::in|ios::out);
Player data[40],*ptr[100];
int i=0;
while(!file.eof()&&i<100)
{
file >>name>>level;
strcpy(data[i].name,name);
data[i].level=level;
data[i].id=i;
ptr[i]=&data[i];
cout<<"Address-"<<ptr[i]<<"data"<<ptr[i]->name<<"id"<<ptr[i]->id<<endl;
i++;
}
system("pause");
return ptr[i-1];
}
The thing is I need access to the memory location after I return the object and I don't want that memory to fade away(as is the case with stack memory), Now how can I allocate memory and access the memory throughout the program without wasting any.