Thread: delete

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    4

    delete

    hey guys!
    here is a doubt.
    is using delete [] really worthy.
    when i used these functions my prog didnt work.but after removing these statements prog workd so nicely.
    I am attaching my c++ code.

    It is to ask user for input string of infinite length with given string termination character,and also allocating only as much space is required.

    please help me out in use of delete [].

    Thank you guys.
    Attached Files Attached Files

  2. #2
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by anantmittal View Post
    hey guys!
    here is a doubt.
    is using delete [] really worthy.
    when i used these functions my prog didnt work.but after removing these statements prog workd so nicely.
    I am attaching my c++ code.

    It is to ask user for input string of infinite length with given string termination character,and also allocating only as much space is required.

    please help me out in use of delete [].

    Thank you guys.
    You really shouldn't be using "delete" directly. It's error-prone (you forget to set your pointers to NULL after the delete call, for example), exception-unsafe, and generally just a bad idea altogether. Read up on RAII and smart-pointers. Anyway, why not use an std::string? Also, your code is a horrible mess of C, C++, and non-standard IO API's. Mixing them invariably leads to all sorts of undefined behaviour, so just pick one and stick with it. Oh, and don't forget to initialize your pointers to NULL at the point of instantiation. Otherwise, bugs like these will surely proliferate...

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    thank u gardhr! i hav just switchd to c++ so it is all like this.btw thanks for useful reply.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I have answered this elsewhere too.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    after placing null after delete [] and after declaring, again same problem is arising.actually sometimes its working nw bt most of the times it stops execution in between.

  6. #6
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by anantmittal View Post
    after placing null after delete [] and after declaring, again same problem is arising.actually sometimes its working nw bt most of the times it stops execution in between.
    You should post your modified code then.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    4
    after making delete [] comments only, its working. mylib1.cpp

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Is there a problem with actually posting this short code sample using standard code tags instead of posting as an attachment?
    Code:
    #include"start.cpp"
    char* _str(int,char);   //(buffer size,terminating character)
    int main(){
        char *str;
        str = _str(10,'$');  
        puts(str);
        cout<<strlen(str);
        getch();
        return 0;
    }
    char* _str(int buffer,char tc){  //tc:- terminating character
         int b=buffer;
         char temp,*str,*rstr,*tstr;
         int i,bt=b;
         tstr=NULL;
         rstr=NULL;
         tstr=new char[b];          //no use of this.This is for line 45 to execute for 1st iteration of while.
         rstr=new char[b];          //same as above but for line 48
         
         while(1){
                  
             str=NULL;
             str=new char[buffer];   
                   
             for(i=0;i<buffer;i++){
                 
                 temp=getche();
                 
                 if(temp==tc){
                     cout<<endl;
                     str[i]='\0';
                     b=i;        
                     break;             
                 }
                 else if(temp==13){          //ascii value of ENTER key (lc i.e. line carriage)is 13
                     str[i]=10;              //ascii value of newline (lf i.e. line feed) is 10
                     cout<<endl;
                 }
                 else{
                     str[i]=temp;    
                 }
             }
             
             for(i=0;i<b;i++){
                 tstr[bt-buffer+i]=str[i];        //line 45
             }
             
             //delete [] rstr;                 //line 48
             rstr=NULL;
             char *rstr;
             rstr=NULL;
             rstr=new char[bt];
             
             for(i=0;i<(bt-buffer+b);i++){
                 rstr[i]=tstr[i];
             }
                              
            // delete [] tstr;
             //tstr=NULL;
             
             if(b!=buffer){
                 //delete [] str;
                 str=NULL;
                 rstr[bt-buffer+b]='\0';
                 return rstr;
             }
             
             char *tstr;
             tstr=NULL;
             tstr=new char[bt+buffer];
             
             
             for(i=0;i<bt;i++){
                 tstr[i]=rstr[i];
             }
              
             bt = bt+buffer;
             
             //delete [] str;
             
         }
    
    }
    You should not be including source files with #include. Additional source files should instead be compiled/linked together as part of a project.

    You are using non-standard I/O function calls (getche) and mixing this with C++ I/O (cout) which has already been mentioned is a no-no.

    Your char array str has the potential of not being null-terminated within the allocated bounds.

    You are redeclaring rstr and tstr within the loop and you allocate memory to both sets of these variables and reference different ones at different times within the loop.

    You've commented out all you deallocation code so your hemorrhaging memory all over the place.



    I'm stopping there, it's too painful to look at this code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Concerning delete/delete[] at program exit
    By laserlight in forum C++ Programming
    Replies: 58
    Last Post: 01-09-2008, 01:40 PM
  2. Replies: 17
    Last Post: 11-16-2006, 09:06 PM
  3. using delete
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2005, 11:49 AM
  4. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  5. using delete to delete an array
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 03:53 PM