By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,035 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,035 IT Pros & Developers. It's quick & easy.

which delete line is correct?

P: 56
I have a similar kind of code as below. But I could not guess how to delete. Please help me.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. int main()
  5. {
  6.         char* items[5];
  7.         int i;
  8.         for(i=0;i<5;i++)
  9.         {
  10.                 char* temp = new char[11];
  11.                 strcpy(temp,"AB CD EFGH");
  12.                 items[i] = temp;
  13.                 printf("items is:%u\n",&items[i]);
  14.                 printf("strlen of items is:%d\n",strlen(items[i]));
  15.         }
  16.         items[5] = 0;
  17.         char** item_ptr = items;
  18.         while (*item_ptr != 0)
  19.         {
  20.                 printf("item_ptr is:%u\n",*item_ptr);
  21.                 delete *item_ptr;  //whether this is correct?
  22.                 //delete []*item_ptr;      or this is correct?
  23.                 item_ptr++;
  24.         }
  25.         return 0;
  26. }
  27.  
Please let me know how to use the delete here. Thanks in advance.
Aug 8 '07 #1
Share this Question
Share on Google+
1 Reply


P: 86
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
Expand|Select|Wrap|Line Numbers
  1.  for(i=0;i<5;i++)
  2. {
  3.      delete[] items[i];
  4. }
Aug 8 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.