which delete line is correct?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rasmidas
    New Member
    • Jun 2007
    • 56

    which delete line is correct?

    I have a similar kind of code as below. But I could not guess how to delete. Please help me.
    [code=c]
    #include<stdio. h>
    #include<string .h>
    #include<stdlib .h>
    int main()
    {
    char* items[5];
    int i;
    for(i=0;i<5;i++ )
    {
    char* temp = new char[11];
    strcpy(temp,"AB CD EFGH");
    items[i] = temp;
    printf("items is:%u\n",&items[i]);
    printf("strlen of items is:%d\n",strlen (items[i]));
    }
    items[5] = 0;
    char** item_ptr = items;
    while (*item_ptr != 0)
    {
    printf("item_pt r is:%u\n",*item_ ptr);
    delete *item_ptr; //whether this is correct?
    //delete []*item_ptr; or this is correct?
    item_ptr++;
    }
    return 0;
    }
    [/code]
    Please let me know how to use the delete here. Thanks in advance.
    Last edited by sicarie; Aug 8 '07, 01:11 PM. Reason: Code tags are to the right when you create or edit a post. Please use them.
  • Darryl
    New Member
    • May 2007
    • 86

    #2
    The second one since you are deleting the result from char* temp = new char[11];

    use delete[ ] with new [ ]
    and delete with new

    however, you didn't need to make char** item_ptr = items;

    You could just use items directly
    Code:
     for(i=0;i<5;i++)
    {
         delete[] items[i];
    }

    Comment

    Working...