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. whats the result of this ...

whats the result of this ...

Scheduled Pinned Locked Moved C / C++ / MFC
questionperformance
14 Posts 8 Posters 1 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.
  • D dharani

    Hi all I have piece of code like this ... char* str; str= new char[30]; str = new char[20]; .... delete str; Here what will happen ? To try it , with VS2003 I did not face any memory leaks (Or did I fail to notice it ?) My question is : When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ? Will "new" make sure the first 30 bytes are freed ? I noticed one thing . After the first "new" the location of str was 0x60878776 . After the second "new" the str was pointing to 20 bytes starting from 0x83422323 . Does it mean memory of 0x60878776 is free ?

    redindian

    M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #2

    Let me check where the data is... But before that, I have to say that your syntax is wrong. It should be: char* str; str= new char[30]; str = new char[20]; // .... delete **[]** str;


    Maxwell Chen

    M 1 Reply Last reply
    0
    • M Maxwell Chen

      Let me check where the data is... But before that, I have to say that your syntax is wrong. It should be: char* str; str= new char[30]; str = new char[20]; // .... delete **[]** str;


      Maxwell Chen

      M Offline
      M Offline
      Maxwell Chen
      wrote on last edited by
      #3

      Visual C++ 2005 reuses the first address of str where it was allocated.


      Maxwell Chen

      D 1 Reply Last reply
      0
      • D dharani

        Hi all I have piece of code like this ... char* str; str= new char[30]; str = new char[20]; .... delete str; Here what will happen ? To try it , with VS2003 I did not face any memory leaks (Or did I fail to notice it ?) My question is : When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ? Will "new" make sure the first 30 bytes are freed ? I noticed one thing . After the first "new" the location of str was 0x60878776 . After the second "new" the str was pointing to 20 bytes starting from 0x83422323 . Does it mean memory of 0x60878776 is free ?

        redindian

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #4

        dharani wrote:

        When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ?

        The allocated memeory will become inaccessible (and thus un-deletable), because you do no longer have a 'handle' to it.


        Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
        George Orwell, "Keep the Aspidistra Flying", Opening words

        D 1 Reply Last reply
        0
        • M Maxwell Chen

          Visual C++ 2005 reuses the first address of str where it was allocated.


          Maxwell Chen

          D Offline
          D Offline
          dharani
          wrote on last edited by
          #5

          So you mean to say in VS2003 or VC++ 6 the first memory block will be still occupied ?

          redindian

          1 1 Reply Last reply
          0
          • J jhwurmbach

            dharani wrote:

            When a "new" is called second time to allocate 20 bytes what will happen to the first 30 bytes allocated ?

            The allocated memeory will become inaccessible (and thus un-deletable), because you do no longer have a 'handle' to it.


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            D Offline
            D Offline
            dharani
            wrote on last edited by
            #6

            So how to avoid such a scenario ? I am trying to find answer ...:(

            redindian

            J P M T 4 Replies Last reply
            0
            • D dharani

              So how to avoid such a scenario ? I am trying to find answer ...:(

              redindian

              J Offline
              J Offline
              jhwurmbach
              wrote on last edited by
              #7

              dharani wrote:

              So how to avoid such a scenario ?

              delete[] the old memoryadress before reallocating. Hold the new address in another variable. Use a string class (e.g. std::string or CString), which handles the memory for you.


              Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
              George Orwell, "Keep the Aspidistra Flying", Opening words

              1 Reply Last reply
              0
              • D dharani

                So how to avoid such a scenario ? I am trying to find answer ...:(

                redindian

                P Offline
                P Offline
                Paresh Chitte
                wrote on last edited by
                #8

                Use realloc. Regards, Paresh.

                S 1 Reply Last reply
                0
                • D dharani

                  So how to avoid such a scenario ? I am trying to find answer ...:(

                  redindian

                  M Offline
                  M Offline
                  markkuk
                  wrote on last edited by
                  #9

                  Use std::string instead of char arrays.

                  1 Reply Last reply
                  0
                  • P Paresh Chitte

                    Use realloc. Regards, Paresh.

                    S Offline
                    S Offline
                    sw thi
                    wrote on last edited by
                    #10

                    You shoudln't mix new and realloc.

                    "What's on your mind, if you will allow the overstatement?"

                    P T 2 Replies Last reply
                    0
                    • S sw thi

                      You shoudln't mix new and realloc.

                      "What's on your mind, if you will allow the overstatement?"

                      P Offline
                      P Offline
                      Paresh Chitte
                      wrote on last edited by
                      #11

                      Yes, you are right. I was thinking in malloc context. Regards, Paresh.

                      1 Reply Last reply
                      0
                      • D dharani

                        So you mean to say in VS2003 or VC++ 6 the first memory block will be still occupied ?

                        redindian

                        1 Offline
                        1 Offline
                        1slipperyfish
                        wrote on last edited by
                        #12

                        i would have thought so as you haven't freed it explicitly, or your compiler has done it for you, why don't you put cout << "destructor\n"; in your destructor and see? paul

                        if ignorance is bliss then knock the smile off my face!!!

                        1 Reply Last reply
                        0
                        • S sw thi

                          You shoudln't mix new and realloc.

                          "What's on your mind, if you will allow the overstatement?"

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

                          swathee wrote:

                          You shoudln't mix new and realloc.

                          offcouse..

                          "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

                          1 Reply Last reply
                          0
                          • D dharani

                            So how to avoid such a scenario ? I am trying to find answer ...:(

                            redindian

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

                            dharani wrote:

                            I am trying to find answer ...

                            CString is much better optimized in this case!

                            "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

                            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