?
Solved

How to delete a pointer?

Posted on 2003-03-09
8
Medium Priority
?
224 Views
Last Modified: 2010-04-01
#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
Comment
Question by:icedeocampo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 

Expert Comment

by:chris-
ID: 8098992
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
 
LVL 1

Expert Comment

by:Sean_Doherty
ID: 8099173
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
 
LVL 1

Expert Comment

by:Sean_Doherty
ID: 8099229
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Expert Comment

by:Sean_Doherty
ID: 8099245
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
 
LVL 5

Expert Comment

by:Kocil
ID: 8099287
  ...
   for (ctr=0;ctr<flist->dir_count;ctr++) {
       delete [] flist->directories[ctr];
   }
   delete flist->directories;
   ...
0
 
LVL 1

Expert Comment

by:yukapapa
ID: 8099510
#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
 
LVL 1

Accepted Solution

by:
Sean_Doherty earned 800 total points
ID: 8099695
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
 

Author Comment

by:icedeocampo
ID: 8100680
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question