-
Dynamic Allocation ??
I dunno if thats what its called but, heres what i need to do, when i try it i keep getting erros to do with memory and stuff not working properly.
I have a class:
Code:
class Connection {
public:
int socket;
};
I then need to have an array of this which can be set at run time, ie i can have 10 connections or 2. this is what i tried;
Code:
int amount = 10;
Connection *conn;
delete [] conn;
conn = new Connection[amount];
Should that work ? or is it all wrong ?
-
Read up on this part of a chapter from Sams TYSC++ in 21 Days... http://newdata.box.sk/bx/c/htm/ch11.htm#Heading18
-
Almost right.
Code:
int amount;
Connection* conn;
cout << "Enter amount: ";
cin >> amount;
conn = new Connection[amount];
//...
delete [] conn; // VERY IMPORTANT - do not leave out or else you will have a memory leak.
-
I would suggest that you use vectors, but I know that this is going to be exported from a DLL, and its a pain in the arse to do that =).
I would go with the linked list approach, especially since it would be handy to add and remove sockets from the list. I suggest reading up on linked lists.
Z.
-
Thanks guys.
Zaei in this case its ok for this to be a static array, its not the actual socket data just the available slots for useres to connect on.
-