Game Development Community

freeing up memory question..

by Matthew Shapiro · in Technical Issues · 07/27/2002 (9:44 am) · 8 replies

Sorry, this answer is escaping my mind as of this moment. If I serv_info *servers; then later on I servers = new serv_info[25]; then I want to delete say the 15th server, can i just do delete server[14];?

--KallDrexx

#1
07/27/2002 (12:26 pm)
I think the best way to do this would be just set the 15th server to NULL, then when you are done with all the servers you "delete [] servers;"
#2
07/27/2002 (2:07 pm)
Sounds like you want:

serv_info **servers;
servers = new serv_info* [25];
memset(servers,0,sizeof(serv_info*)*25);

//to create server 16
servers[15]= new serv_info;

//to delete server 16
delete servers[15];
servers[15]=NULL;

//to delete server array

for (int i=0;i<25;i++)
if (servers[i])
delete servers[i];

delete [] servers;


-J
#3
07/27/2002 (4:07 pm)
Joshua is on the right track!
#4
07/27/2002 (5:39 pm)
uh memset() does what exactly?

--KallDrexx
#5
07/27/2002 (5:42 pm)
Quote:memset() does what exactly?
Initializes the memory. It basically sets the memory at location of argument 1 to the value of argument 2 which is of size argument 3.
#6
07/27/2002 (5:51 pm)
Quote://to delete server 16
delete servers[15];
servers[15]=NULL

so basically the answer to my question is yes. But... 1 why did you initialize it by:

serv_info **servers;

Why should I use 2 *'s? btw serv_info is a struct.

thanks

--KallDrexx
#7
07/27/2002 (5:58 pm)
Quote:why did you initialize it by:

serv_info **servers;

Why should I use 2 *'s?
To create an array of pointers that point to type serv_info.
#8
07/27/2002 (6:53 pm)
doh! heh didn't see that thanks =)

--KallDrexx