Results 1 to 6 of 6

Thread: Dynamic Allocation ??

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341

    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 ?

  2. #2
    Fanatic Member
    Join Date
    Sep 2000
    Location
    UK.
    Posts
    728
    Read up on this part of a chapter from Sams TYSC++ in 21 Days... http://newdata.box.sk/bx/c/htm/ch11.htm#Heading18
    Digital-X-Treme
    Contact me on MSN Messenger: [email protected]

    [VBCODE]Debug.Print Round(((1097) - ((55 ^ 5 + 311 ^ 3 - 11 ^ 3) _
    / (68 ^ 5))) ^ (1 / 7), 13)[/VBCODE]

  3. #3
    Fanatic Member Wynd's Avatar
    Join Date
    Dec 2000
    Location
    In a bar frequented by colossal death robots
    Posts
    772
    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.
    Alcohol & calculus don't mix.
    Never drink & derive.

  4. #4
    Zaei
    Guest
    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.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    England, Buckingham
    Posts
    1,341
    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.

  6. #6
    Zaei
    Guest
    Ok =).

    Z.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width