PDA

Click to See Complete Forum and Search --> : Dynamic Allocation ??


PsyVision
Mar 20th, 2002, 04:17 PM
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:



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;



int amount = 10;
Connection *conn;

delete [] conn;
conn = new Connection[amount];



Should that work ? or is it all wrong ?

[Digital-X-Treme]
Mar 20th, 2002, 05:01 PM
Read up on this part of a chapter from Sams TYSC++ in 21 Days... http://newdata.box.sk/bx/c/htm/ch11.htm#Heading18

Wynd
Mar 20th, 2002, 06:05 PM
Almost right.


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.

Zaei
Mar 20th, 2002, 09:29 PM
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.

PsyVision
Mar 21st, 2002, 02:07 AM
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.

Zaei
Mar 21st, 2002, 07:58 AM
Ok =).

Z.