Doubt relating to delete operator :(

This is a discussion on Doubt relating to delete operator :( within the C++ Programming forums, part of the General Programming Boards category; Hello, i am little confused while i use delete operator. I am posting my code :- Code: #include"iostream.h" #include"string.h" class ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Doubt relating to delete operator :(

    Hello,
    i am little confused while i use delete operator. I am posting my code :-

    Code:
    #include"iostream.h"
    #include"string.h"
    
    class str
    {
       char *p;
       public:
    	  str(char *t)
    	  {
    		  p=new char[strlen(t)+1];
    		  strcpy(p,t);
    	  }
    	  ~str()
    	  {
    		  delete []p;
    	  }
    	  void putdata()
    	  {
    		  cout<<p;
    	  }
    };
    
    int main(void)
    {
     str t("Hello World");
     t.putdata();
     cin.ignore();
     cin.get();
     return 0;
    }
    In the above code i am confused as to whether i should use delete []p or i should use only p??None of them gives any syntactical error but i am confused i feel i should be delete []p but in Balagurusamy's book its given delete p.

    Also please try to explain in a clear cut manner like when i use double dimension array then what i should i write in delete???Please help me?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,449
    when you have new[] you use delete[]

    so in this case delete[] will be appropriate

    Code:
    #include"iostream.h"
    #include"string.h"
    should be
    Code:
    #include <iostream>
    #include <cstring>
    also you need to use std namespace - your compiler is outdated

    Your class as working with dynamic memory allocations has missing copy-constructor and operator=
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok now please tell me how to deallocate memory for two dimensional array?Is this the correct way of doing it?

    Code:
    for(int i=0;i<rows;i++)
        delete []a[i];
    where a is the 2 dimensional array and rows is the total number of rows!??

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    And you'll need to delete[] a after that.

    However, unless you are required to do it this way, you should use std::string (for strings) and std::vector (for dynamic arrays), as they will take care of memory management automatically, and your class probably won't need a user-defined destructor, copy constructor and assignment operator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  2. private overloading of new & delete
    By Aidman in forum C++ Programming
    Replies: 12
    Last Post: 04-28-2005, 01:11 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Replies: 3
    Last Post: 12-06-2002, 09:02 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21