473,320 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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.
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
1 1504
Darryl
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

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

Similar topics

19
by: deko | last post by:
I'm kind of lost on this one - I need to modify 2 files based on user input: $data_array = file($data_file); $counter_array = file($counter_file); // There is a line-for-line relationship...
4
by: Scotter | last post by:
Hi folx - Here is the code I'm trying (but it errors out with "Item cannot be found in the collection corresponding to the requested name or ordinal."): ...
5
by: Jona Schuman | last post by:
Hi, there was a memory leak in a programming assignment that I'm working on. The problem seems to have been a syntax error in a delete statment: delete V1, V2; //where V1 & V2 are pointers to...
9
by: Venn Syii | last post by:
I have the following line of code: if (MyData) { delete MyData; MyData= NULL; } Below is MyData: SomeDataStructure *MyData;
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
6
by: tomtown.net | last post by:
Hello I'm trying to get a single line removed from a text file using a search pattern (pretty simple: if line contains "NODE1") -> remove line). To achieve this I's like to operate with only the...
6
by: jafarikia | last post by:
Hi, I am using pointer in my program. I write them like: *DIST=new int ; and when I want to make the memory free, I write: delete DIST; just before the "return 0". I don't know if the...
20
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.