icedeocampo
asked on
How to delete a pointer?
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main() {
char **test;
int ctr;
test=new char*[32000];
for (ctr=0;ctr<32000;ctr++) {
test[ctr]=new char[10000];
sprintf(test[ctr],"String %d\n",ctr);
}
for (ctr=0;ctr<32000;ctr++) {
delete [] test[ctr];
}
delete test;
}
========================== ========== ========== ========== ========== ========== ========== ===
The program above works.... but the program below does not, i can't figure out why =(
========================== ========== ========== ========== ========== ========== ========== ===
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories=new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr<flist->dir_coun t;ctr++) {
flist->directories[ctr]=ne w char[strlen("some directory")];
strcpy(flist->directories[ ctr],"some directory");
}
for (ctr=0;ctr<flist->dir_coun t;ctr++) {
delete flist->directories[ctr]; // <------------- ERROR!
}
}
please help, thanks!
#include<string.h>
#include<conio.h>
void main() {
char **test;
int ctr;
test=new char*[32000];
for (ctr=0;ctr<32000;ctr++) {
test[ctr]=new char[10000];
sprintf(test[ctr],"String %d\n",ctr);
}
for (ctr=0;ctr<32000;ctr++) {
delete [] test[ctr];
}
delete test;
}
==========================
The program above works.... but the program below does not, i can't figure out why =(
==========================
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories=new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr<flist->dir_coun
flist->directories[ctr]=ne
strcpy(flist->directories[
}
for (ctr=0;ctr<flist->dir_coun
delete flist->directories[ctr]; // <------------- ERROR!
}
}
please help, thanks!
The following code work with the +1 added to the the following line:
-------------------------- ---------- ---------- ------
flist->directories[ctr]=ne w char[strlen("some directory")+1];
-------------------------- ---------- ---------- -------
You need the +1 for a string becuase of the '\0' at the end of a string or char array.
-------------------------- ---------- ---------- ----
// c_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories = new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr < flist->dir_count;ctr++) {
flist->directories[ctr]=ne w char[strlen("some directory")+1];
strcpy(flist->directories[ ctr],"some directory");
}
for (ctr=0;ctr < flist->dir_count;ctr++) {
char *temp;
temp = flist->directories[ctr];
cout << temp << '\n'; // <------------- ERROR!
}
for (ctr=0;ctr<flist->dir_coun t;ctr++) {
delete flist->directories[ctr]; // <------------- ERROR!
cout << "test";
}
}
--------------------------
flist->directories[ctr]=ne
--------------------------
You need the +1 for a string becuase of the '\0' at the end of a string or char array.
--------------------------
// c_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories = new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr < flist->dir_count;ctr++) {
flist->directories[ctr]=ne
strcpy(flist->directories[
}
for (ctr=0;ctr < flist->dir_count;ctr++) {
char *temp;
temp = flist->directories[ctr];
cout << temp << '\n'; // <------------- ERROR!
}
for (ctr=0;ctr<flist->dir_coun
delete flist->directories[ctr]; // <------------- ERROR!
cout << "test";
}
}
The following code work with the +1 added to the the following line:
-------------------------- ---------- ---------- ------
flist->directories[ctr]=ne w char[strlen("some directory")+1];
-------------------------- ---------- ---------- -------
You need the +1 for a string becuase of the '\0' at the end of a string or char array.
-------------------------- ---------- ---------- ----
// c_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories = new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr < flist->dir_count;ctr++) {
flist->directories[ctr]=ne w char[strlen("some directory")+1];
strcpy(flist->directories[ ctr],"some directory");
}
for (ctr=0;ctr < flist->dir_count;ctr++) {
char *temp;
temp = flist->directories[ctr];
cout << temp << '\n'; // <------------- ERROR!
}
for (ctr=0;ctr<flist->dir_coun t;ctr++) {
delete flist->directories[ctr]; // <------------- ERROR!
cout << "test";
}
}
--------------------------
flist->directories[ctr]=ne
--------------------------
You need the +1 for a string becuase of the '\0' at the end of a string or char array.
--------------------------
// c_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories = new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr < flist->dir_count;ctr++) {
flist->directories[ctr]=ne
strcpy(flist->directories[
}
for (ctr=0;ctr < flist->dir_count;ctr++) {
char *temp;
temp = flist->directories[ctr];
cout << temp << '\n'; // <------------- ERROR!
}
for (ctr=0;ctr<flist->dir_coun
delete flist->directories[ctr]; // <------------- ERROR!
cout << "test";
}
}
The following code work with the +1 added to the the following line:
-------------------------- ---------- ---------- ------
flist->directories[ctr]=ne w char[strlen("some directory")+1];
-------------------------- ---------- ---------- -------
You need the +1 for a string becuase of the '\0' at the end of a string or char array.
-------------------------- ---------- ---------- ----
// c_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories = new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr < flist->dir_count;ctr++) {
flist->directories[ctr]=ne w char[strlen("some directory")+1];
strcpy(flist->directories[ ctr],"some directory");
}
for (ctr=0;ctr < flist->dir_count;ctr++) {
char *temp;
temp = flist->directories[ctr];
cout << temp << '\n'; // <------------- ERROR!
}
for (ctr=0;ctr<flist->dir_coun t;ctr++) {
delete flist->directories[ctr]; // <------------- ERROR!
cout << "test";
}
}
--------------------------
flist->directories[ctr]=ne
--------------------------
You need the +1 for a string becuase of the '\0' at the end of a string or char array.
--------------------------
// c_test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
void main() {
struct file_list *flist=new file_list;
flist->file_count=100;
flist->dir_count=100;
flist->directories = new char*[flist->dir_count];
int ctr;
for (ctr=0;ctr < flist->dir_count;ctr++) {
flist->directories[ctr]=ne
strcpy(flist->directories[
}
for (ctr=0;ctr < flist->dir_count;ctr++) {
char *temp;
temp = flist->directories[ctr];
cout << temp << '\n'; // <------------- ERROR!
}
for (ctr=0;ctr<flist->dir_coun
delete flist->directories[ctr]; // <------------- ERROR!
cout << "test";
}
}
...
for (ctr=0;ctr<flist->dir_coun t;ctr++) {
delete [] flist->directories[ctr];
}
delete flist->directories;
...
for (ctr=0;ctr<flist->dir_coun
delete [] flist->directories[ctr];
}
delete flist->directories;
...
#include<string.h>
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
[ 1]void main() {
[ 2] struct file_list *flist=new file_list;
[ 3] flist->file_count=100;
[ 4] flist->dir_count=100;
[ 5] flist->directories=new char*[flist->dir_count];
[ 6] int ctr;
[ 7] for (ctr=0;ctr<flist->dir_coun t;ctr++) {
[ 8] flist->directories[ctr]=ne w char[strlen("some directory")];
[ 9] strcpy(flist->directories[ ctr],"some directory");
[10] }
[11]
[12] for (ctr=0;ctr<flist->dir_coun t;ctr++) {
[13] delete flist->directories[ctr]; // <------------- ERROR!
[14] }
[15]
[16]}
I think the code has 2 problems.
At first the operand of the delete at line [13] using normal form of delete operator. The variable flist->directories[ctr] points to array of char and it is allocated by array new operator at line [8], so it is deleted by array form of delete:
delete[] flist->directories[ctr];
Second, the size of the array allocated at line [8] have to be
strlen("some directory") + 1
instead of
strlen("some directory").
The C style string is composed by visible normal characters and a invisible terminating null character. The strlen function counts the normal characters in the string but does not counts the terminating null character and the strcpy function copies string with the terminating null character. Therefore the size of the array for the string "some directory" have to be at least strlen("some directory") + 1.
I hope this will be some helps for you.
struct file_list {
char **directories;
unsigned short file_count;
unsigned short dir_count;
};
[ 1]void main() {
[ 2] struct file_list *flist=new file_list;
[ 3] flist->file_count=100;
[ 4] flist->dir_count=100;
[ 5] flist->directories=new char*[flist->dir_count];
[ 6] int ctr;
[ 7] for (ctr=0;ctr<flist->dir_coun
[ 8] flist->directories[ctr]=ne
[ 9] strcpy(flist->directories[
[10] }
[11]
[12] for (ctr=0;ctr<flist->dir_coun
[13] delete flist->directories[ctr]; // <------------- ERROR!
[14] }
[15]
[16]}
I think the code has 2 problems.
At first the operand of the delete at line [13] using normal form of delete operator. The variable flist->directories[ctr] points to array of char and it is allocated by array new operator at line [8], so it is deleted by array form of delete:
delete[] flist->directories[ctr];
Second, the size of the array allocated at line [8] have to be
strlen("some directory") + 1
instead of
strlen("some directory").
The C style string is composed by visible normal characters and a invisible terminating null character. The strlen function counts the normal characters in the string but does not counts the terminating null character and the strcpy function copies string with the terminating null character. Therefore the size of the array for the string "some directory" have to be at least strlen("some directory") + 1.
I hope this will be some helps for you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
man! you rule! and you did post a lot of times.. i'll give you 200 for your excellent help!
thanks to everyone!
thanks to everyone!
delete flist->directories[ctr]; // <------------- ERROR!
delete [] flist->directories[ctr]; // <--- no error?