Results 1 to 2 of 2

Thread: delete all of these?

  1. #1
    Senior Member
    Join Date
    Jul 2001
    Posts
    280

    delete all of these?

    Ok, heres a situation:
    Code:
    // Create pointers for 50 strings
    char **strings = new char * [50];
    
    for (int i = 0; i < 50; ++i){
       // Create a string with 10 characters of room
       strings[i] = new char[10];
    }
    Now I have 700 bytes: 50 x 4-bytes in pointers and 50 x 10-bytes in strings. Which of the following scenarios is the proper way to delete these strings.

    A)
    Code:
    delete [] strings;
    B)
    Code:
    for (i = 0; i < 50; ++i)
       delete [] strings[i];
    delete [] strings;
    C) none of the above (please elaborate).

    Thanks for your help.

  2. #2
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811

    Re:delete all of these?

    I would go with B, eventho A would do the job aswell.

    But, since the order is something like, when using delete [] strings the pointers to the containers are freed, but the container itself isn't entirely freed, it is first, when some program is desperate for room the container it self will be freed, since no program is claiming it.

    Using the for() loop, will assure that every container is freed, befor freeing the pointers to them. This ensures the system will be aware of the available space, and not wait for rmap to investigate, when the system is running low on memory.

Similar Threads

  1. How to delete this for Java
    By tech291083 in forum Redhat / Fedora
    Replies: 7
    Last Post: 06-09-2007, 01:48 PM
  2. Can I delete these
    By mojo jojo in forum Linux - General Topics
    Replies: 2
    Last Post: 10-29-2003, 01:59 PM
  3. Please delete me.........................
    By in forum Announcements and Suggestions
    Replies: 8
    Last Post: 09-03-2002, 05:17 PM
  4. Please dont delete me
    By LiNuts in forum Linux - General Topics
    Replies: 10
    Last Post: 07-25-2002, 12:26 AM
  5. Please don't delete me....
    By Galik in forum General Chat
    Replies: 1
    Last Post: 07-24-2002, 06:34 PM

Bookmarks

Posting Permissions

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