• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

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_count;ctr++) {
               flist->directories[ctr]=new char[strlen("some directory")];
               strcpy(flist->directories[ctr],"some directory");
     }

     for (ctr=0;ctr<flist->dir_count;ctr++) {
               delete flist->directories[ctr]; // <------------- ERROR!
     }

}


please help, thanks!
0
icedeocampo
Asked:
icedeocampo
1 Solution
 
chris-Commented:
I'm not quite sure, but I think you forgot the brackets in the second code snippet:

delete flist->directories[ctr]; // <------------- ERROR!
delete [] flist->directories[ctr]; // <--- no error?
0
 
Sean_DohertyCommented:
The following code work with the +1 added to the the following line:

----------------------------------------------------
flist->directories[ctr]=new 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]=new 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_count;ctr++) {
         delete flist->directories[ctr]; // <------------- ERROR!
          cout << "test";
    }


}
0
 
Sean_DohertyCommented:
The following code work with the +1 added to the the following line:

----------------------------------------------------
flist->directories[ctr]=new 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]=new 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_count;ctr++) {
         delete flist->directories[ctr]; // <------------- ERROR!
          cout << "test";
    }


}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Sean_DohertyCommented:
The following code work with the +1 added to the the following line:

----------------------------------------------------
flist->directories[ctr]=new 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]=new 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_count;ctr++) {
         delete flist->directories[ctr]; // <------------- ERROR!
          cout << "test";
    }


}
0
 
KocilCommented:
  ...
   for (ctr=0;ctr<flist->dir_count;ctr++) {
       delete [] flist->directories[ctr];
   }
   delete flist->directories;
   ...
0
 
yukapapaCommented:
#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_count;ctr++) {
[ 8]              flist->directories[ctr]=new char[strlen("some directory")];
[ 9]              strcpy(flist->directories[ctr],"some directory");
[10]    }
[11]
[12]    for (ctr=0;ctr<flist->dir_count;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.
0
 
Sean_DohertyCommented:
The following code has been corrected in 3 places and exits with out any error.

--------------------------------------------------
// c_test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"   // Coniguration Dependant.
#include <iostream.h>
#include <conio.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]=new char[strlen("some directory")+1];  // Proper allocation
         strcpy(flist->directories[ctr],"some directory");
   }

    for (ctr=0;ctr < flist->dir_count;ctr++) {
         char *temp;
         temp = flist->directories[ctr];
   }

    for (ctr=0;ctr<flist->dir_count;ctr++) {
        delete [] flist->directories[ctr]; // String Deleted
     }
     delete flist->directories;  // Dynamic Array Deleted

     getch();
}
0
 
icedeocampoAuthor Commented:
man! you rule! and you did post a lot of times.. i'll give you 200 for your excellent help!

thanks to everyone!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now