Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using delete operator

using delete operator

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structurestutorial
19 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N namaskaaram

    let me explain with an example:

    BYTE* pbyte1 = new BYTE();
    delete [] pbyte1;

    The second line is suppsed to be just :

    delete pbyte1;

    So herez my question: is there anything wrong with using the '[]' alongside teh delete operator even though the dynamically created variable is not of array type?

    L Offline
    L Offline
    Laxman Auti
    wrote on last edited by
    #4

    delete [] calls the constructer for each object of the array. here you have created only one object of the BYTE. so prefer second one. Knock out 't' from can't, You can if you think you can :cool:

    1 Reply Last reply
    0
    • G Ganesh_T

      When you use [] you are deleting the array. and using the just delete pbyte you are deleting the pointer and dont forget to set the pinter to NULL Cheers "Peace of mind through Technology" -- modified at 7:14 Tuesday 13th June, 2006

      N Offline
      N Offline
      namaskaaram
      wrote on last edited by
      #5

      thanx everybody for that quick reply!... but the question i have asked still remains unanswered! is there any problem(like mem leak, or unexpected error)when i use 'delete []' for an object that is not created as array?????

      _ 1 Reply Last reply
      0
      • N namaskaaram

        thanx everybody for that quick reply!... but the question i have asked still remains unanswered! is there any problem(like mem leak, or unexpected error)when i use 'delete []' for an object that is not created as array?????

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #6

        See my post for the thread. delete x ; //works for a single object delete[] x; // deletes the complete array Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

        N D 2 Replies Last reply
        0
        • _ _AnsHUMAN_

          See my post for the thread. delete x ; //works for a single object delete[] x; // deletes the complete array Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

          N Offline
          N Offline
          namaskaaram
          wrote on last edited by
          #7

          dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!

          L _ C 3 Replies Last reply
          0
          • N namaskaaram

            dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!

            L Offline
            L Offline
            Laxman Auti
            wrote on last edited by
            #8

            namaskaaram wrote:

            My question is :whether there is a prob when 'delete []' is used to non-array type objects??

            Do you got any problem while using delete [] ??? if so what is the problem?? Knock out 't' from can't, You can if you think you can :cool:

            N 1 Reply Last reply
            0
            • N namaskaaram

              let me explain with an example:

              BYTE* pbyte1 = new BYTE();
              delete [] pbyte1;

              The second line is suppsed to be just :

              delete pbyte1;

              So herez my question: is there anything wrong with using the '[]' alongside teh delete operator even though the dynamically created variable is not of array type?

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #9

              namaskaaram wrote:

              BYTE* pbyte1 = new BYTE(); delete [] pbyte1;

              IMHO, i better use Auto_ptr to deal with these problem!

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

              cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

              1 Reply Last reply
              0
              • N namaskaaram

                dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!

                _ Offline
                _ Offline
                _AnsHUMAN_
                wrote on last edited by
                #10

                a class’ destructor, doesn’t know what the array’s size is; (Initially delete had two arguments also.) it only knew that its sole argument is a pointer to an array. Furthermore, a programmer might mistakenly pass the wrong size of the array to delete[]. C++ creators realized this and decided to eliminate the size argument. However, they have kept the distinction between delete and delete[]. So when you use delete[] to delete the single object using delete[] p only one object gets destroyed so no problems with that else if p is an array the whole array gets destroyed. Does this answer your question now? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                N 1 Reply Last reply
                0
                • L Laxman Auti

                  namaskaaram wrote:

                  My question is :whether there is a prob when 'delete []' is used to non-array type objects??

                  Do you got any problem while using delete [] ??? if so what is the problem?? Knock out 't' from can't, You can if you think you can :cool:

                  N Offline
                  N Offline
                  namaskaaram
                  wrote on last edited by
                  #11

                  well just want to know if its handled by the delete operator !!!.. just inquisitive! ;)

                  1 Reply Last reply
                  0
                  • N namaskaaram

                    dude!!!!!:laugh: u havent read my question! ;o) i havent asked which to use. My question is :whether there is a prob when 'delete []' is used to non-array type objects?? please do read the question !!!!

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #12

                    Quick answer: yes you'll have problems. new and new[] are not doing the same thing and if you mismatch that with delete and delete[], you'll be in troubles.


                    Cédric Moonen Software developer
                    Charting control

                    N _ 2 Replies Last reply
                    0
                    • _ _AnsHUMAN_

                      a class’ destructor, doesn’t know what the array’s size is; (Initially delete had two arguments also.) it only knew that its sole argument is a pointer to an array. Furthermore, a programmer might mistakenly pass the wrong size of the array to delete[]. C++ creators realized this and decided to eliminate the size argument. However, they have kept the distinction between delete and delete[]. So when you use delete[] to delete the single object using delete[] p only one object gets destroyed so no problems with that else if p is an array the whole array gets destroyed. Does this answer your question now? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                      N Offline
                      N Offline
                      namaskaaram
                      wrote on last edited by
                      #13

                      finally! ...hehe..just kidding! ;) nice answer bro!.... cheerz!

                      1 Reply Last reply
                      0
                      • C Cedric Moonen

                        Quick answer: yes you'll have problems. new and new[] are not doing the same thing and if you mismatch that with delete and delete[], you'll be in troubles.


                        Cédric Moonen Software developer
                        Charting control

                        N Offline
                        N Offline
                        namaskaaram
                        wrote on last edited by
                        #14

                        r u sure cedric?..coz i have got a contradictory answer from an above post! ...hmm... actually i did debug!....didnt find any probz1...but i just aint quite sure if itz ok or not!.... hmmm.. :confused: -- modified at 7:43 Tuesday 13th June, 2006

                        1 Reply Last reply
                        0
                        • N namaskaaram

                          let me explain with an example:

                          BYTE* pbyte1 = new BYTE();
                          delete [] pbyte1;

                          The second line is suppsed to be just :

                          delete pbyte1;

                          So herez my question: is there anything wrong with using the '[]' alongside teh delete operator even though the dynamically created variable is not of array type?

                          V Offline
                          V Offline
                          Viorel
                          wrote on last edited by
                          #15

                          When delete [] is called, the system organizes a loop according to the number of allocated items. Within this loop, the system calls the destructor, if any. The number of iterations is got from internal values made by new operator. We cannot rely on the idea that the new operator, used in non-array manner, stores all of internal values needed by delete []. It depends on implementations. For instance, in my tests (VS 2003), the delete [] statement fails if is executed for a single object allocated with new, and that object has a virtual destructor. In case of objects with no destructor, like BYTE, the system does not need to known the number of iterations, therefore the delete [] acts like delete. I think delete [] must be only used with arrays. Even if in some implementations the internal information allows using of delete [] for non-array objects, where delete is expected, it will be slower, because of the loop actually executed a single time.

                          1 Reply Last reply
                          0
                          • C Cedric Moonen

                            Quick answer: yes you'll have problems. new and new[] are not doing the same thing and if you mismatch that with delete and delete[], you'll be in troubles.


                            Cédric Moonen Software developer
                            Charting control

                            _ Offline
                            _ Offline
                            _AnsHUMAN_
                            wrote on last edited by
                            #16

                            hi Cedric, Can you just brief me on what problem would be there if I use delete[] to delete a single object? I just read that using the delete[] method is the best way to delete an object as it deletes the memory associated with the array or the object completely Thanks anyways I got the answer in Viorel's post Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_ -- modified at 7:50 Tuesday 13th June, 2006

                            C 1 Reply Last reply
                            0
                            • _ _AnsHUMAN_

                              hi Cedric, Can you just brief me on what problem would be there if I use delete[] to delete a single object? I just read that using the delete[] method is the best way to delete an object as it deletes the memory associated with the array or the object completely Thanks anyways I got the answer in Viorel's post Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_ -- modified at 7:50 Tuesday 13th June, 2006

                              C Offline
                              C Offline
                              Cedric Moonen
                              wrote on last edited by
                              #17

                              Because delete[] expects the number of items in your array to be just before the address of the first element. This value is not present when you use new. Thus, you will have some problems because you first try to read the number of items where you shouldn't.


                              Cédric Moonen Software developer
                              Charting control

                              1 Reply Last reply
                              0
                              • _ _AnsHUMAN_

                                See my post for the thread. delete x ; //works for a single object delete[] x; // deletes the complete array Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                                D Offline
                                D Offline
                                David Crow
                                wrote on last edited by
                                #18

                                _AnShUmAn_ wrote:

                                delete x ; //works for a single object delete[] x; // deletes the complete array

                                He's already indicated he knows the difference. Read his original question again. :rolleyes:


                                "The largest fire starts but with the smallest spark." - David Crow

                                "Judge not by the eye but by the heart." - Native American Proverb

                                _ 1 Reply Last reply
                                0
                                • D David Crow

                                  _AnShUmAn_ wrote:

                                  delete x ; //works for a single object delete[] x; // deletes the complete array

                                  He's already indicated he knows the difference. Read his original question again. :rolleyes:


                                  "The largest fire starts but with the smallest spark." - David Crow

                                  "Judge not by the eye but by the heart." - Native American Proverb

                                  _ Offline
                                  _ Offline
                                  _AnsHUMAN_
                                  wrote on last edited by
                                  #19

                                  and you guess what, you didn't follow what the rest of the posts were for ? Somethings seem HARD to do, until we know how to do them. ;-) _AnShUmAn_

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups