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. Memory allocation

Memory allocation

Scheduled Pinned Locked Moved C / C++ / MFC
performancehelpquestion
10 Posts 5 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.
  • D Offline
    D Offline
    Dennis L
    wrote on last edited by
    #1

    Hi every one! Can any one explain me why is this happening struct SOME { char s0[1]; char s1[2]; char s2[1]; }; int main() { SOME *sm = new SOME; delete sm; return 0; } before allocation (with new) 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } after allocation 0x00332e98 { s0=0x00332e98 " π­Ί««««««««ξώξώ" s1=0x00332e99 "π­Ί««««««««ξώξώ" s2=0x00332e9b "Ί««««««««ξώξώ" } after deallocation (with delete) 0x00332e98 <----- Is that a problem? { s0=0x00332e98 "" <----- s1=0x00332e99 "3" <----- s2=0x00332e9b "" <----- } thanks!

    D R S B 4 Replies Last reply
    0
    • D Dennis L

      Hi every one! Can any one explain me why is this happening struct SOME { char s0[1]; char s1[2]; char s2[1]; }; int main() { SOME *sm = new SOME; delete sm; return 0; } before allocation (with new) 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } after allocation 0x00332e98 { s0=0x00332e98 " π­Ί««««««««ξώξώ" s1=0x00332e99 "π­Ί««««««««ξώξώ" s2=0x00332e9b "Ί««««««««ξώξώ" } after deallocation (with delete) 0x00332e98 <----- Is that a problem? { s0=0x00332e98 "" <----- s1=0x00332e99 "3" <----- s2=0x00332e9b "" <----- } thanks!

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

      I don't see a problem. What specifically are you asking?

      "Love people and use things, not love things and use people." - Unknown

      "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

      D 1 Reply Last reply
      0
      • D David Crow

        I don't see a problem. What specifically are you asking?

        "Love people and use things, not love things and use people." - Unknown

        "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

        D Offline
        D Offline
        Dennis L
        wrote on last edited by
        #3

        first: in that below, why the memory addresses of s0, s1, s2 are different and has these values 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } second: after deallocation the memory addresses are still assigned to s0, s1, s2 0x00332e98 { s0=0x00332e98 "" s1=0x00332e99 "3" is that a problem s2=0x00332e9b "" }

        D 1 Reply Last reply
        0
        • D Dennis L

          first: in that below, why the memory addresses of s0, s1, s2 are different and has these values 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } second: after deallocation the memory addresses are still assigned to s0, s1, s2 0x00332e98 { s0=0x00332e98 "" s1=0x00332e99 "3" is that a problem s2=0x00332e9b "" }

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

          Dennis L wrote:

          in that below, why the memory addresses of s0, s1, s2 are different...

          As opposed to all being the same?

          Dennis L wrote:

          after deallocation the memory addresses are still assigned to s0, s1, s2

          And what would you expect them to be? All delete does is tell the memory manager that address 0x00332e98 is available for use again.

          "Love people and use things, not love things and use people." - Unknown

          "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

          D 1 Reply Last reply
          0
          • D Dennis L

            Hi every one! Can any one explain me why is this happening struct SOME { char s0[1]; char s1[2]; char s2[1]; }; int main() { SOME *sm = new SOME; delete sm; return 0; } before allocation (with new) 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } after allocation 0x00332e98 { s0=0x00332e98 " π­Ί««««««««ξώξώ" s1=0x00332e99 "π­Ί««««««««ξώξώ" s2=0x00332e9b "Ί««««««««ξώξώ" } after deallocation (with delete) 0x00332e98 <----- Is that a problem? { s0=0x00332e98 "" <----- s1=0x00332e99 "3" <----- s2=0x00332e9b "" <----- } thanks!

            R Offline
            R Offline
            Roger Stoltz
            wrote on last edited by
            #5

            Dennis L wrote:

            Is that a problem?

            In short: no, unless you're trying to use it. The fact that you have released the memory you previously allocated does not make the pointer to point somewhere else. The memory should not be used after being deallocated since it may contain invalid data as you've instructed the memory manager that you don't need it any longer by deallocating it. This is why it's considered best practice to assign NULL to pointers you've deallocated to be able to test on the pointer to see if you can use the memory it points to.

            "It's supposed to be hard, otherwise anybody could do it!" - selfquote
            "High speed never compensates for wrong direction!" - unknown

            1 Reply Last reply
            0
            • D Dennis L

              Hi every one! Can any one explain me why is this happening struct SOME { char s0[1]; char s1[2]; char s2[1]; }; int main() { SOME *sm = new SOME; delete sm; return 0; } before allocation (with new) 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } after allocation 0x00332e98 { s0=0x00332e98 " π­Ί««««««««ξώξώ" s1=0x00332e99 "π­Ί««««««««ξώξώ" s2=0x00332e9b "Ί««««««««ξώξώ" } after deallocation (with delete) 0x00332e98 <----- Is that a problem? { s0=0x00332e98 "" <----- s1=0x00332e99 "3" <----- s2=0x00332e9b "" <----- } thanks!

              S Offline
              S Offline
              sashoalm
              wrote on last edited by
              #6

              Dennis L wrote:

              Can any one explain me why is this happening ... before allocation (with new) 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf }

              It's for debugging purposes I think - this way it's easier to detect if you're using your data before you've initialized it.

              There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition. Blaise Pascal

              1 Reply Last reply
              0
              • D David Crow

                Dennis L wrote:

                in that below, why the memory addresses of s0, s1, s2 are different...

                As opposed to all being the same?

                Dennis L wrote:

                after deallocation the memory addresses are still assigned to s0, s1, s2

                And what would you expect them to be? All delete does is tell the memory manager that address 0x00332e98 is available for use again.

                "Love people and use things, not love things and use people." - Unknown

                "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                D Offline
                D Offline
                Dennis L
                wrote on last edited by
                #7

                first I have read that all local variables that are not initialized explicitly are being intialiazed to 0xcccccccc with the Microsoft compilers. The first OK but the other two? second what happens with this address if the program exits or is this void func1() { SOME *sm = new SOME; delete sm; } void func2() { func1(); // What happents here? }

                D 1 Reply Last reply
                0
                • D Dennis L

                  first I have read that all local variables that are not initialized explicitly are being intialiazed to 0xcccccccc with the Microsoft compilers. The first OK but the other two? second what happens with this address if the program exits or is this void func1() { SOME *sm = new SOME; delete sm; } void func2() { func1(); // What happents here? }

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

                  Dennis L wrote:

                  what happens with this address if the program exits

                  It is reclaimed by the memory manager.

                  Dennis L wrote:

                  // What happents here?

                  Nothing.

                  "Love people and use things, not love things and use people." - Unknown

                  "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                  D 1 Reply Last reply
                  0
                  • D David Crow

                    Dennis L wrote:

                    what happens with this address if the program exits

                    It is reclaimed by the memory manager.

                    Dennis L wrote:

                    // What happents here?

                    Nothing.

                    "Love people and use things, not love things and use people." - Unknown

                    "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

                    D Offline
                    D Offline
                    Dennis L
                    wrote on last edited by
                    #9

                    I think i got it now Thanks for your reply!

                    1 Reply Last reply
                    0
                    • D Dennis L

                      Hi every one! Can any one explain me why is this happening struct SOME { char s0[1]; char s1[2]; char s2[1]; }; int main() { SOME *sm = new SOME; delete sm; return 0; } before allocation (with new) 0xcccccccc { s0=0xcccccccc s1=0xcccccccd s2=0xcccccccf } after allocation 0x00332e98 { s0=0x00332e98 " π­Ί««««««««ξώξώ" s1=0x00332e99 "π­Ί««««««««ξώξώ" s2=0x00332e9b "Ί««««««««ξώξώ" } after deallocation (with delete) 0x00332e98 <----- Is that a problem? { s0=0x00332e98 "" <----- s1=0x00332e99 "3" <----- s2=0x00332e9b "" <----- } thanks!

                      B Offline
                      B Offline
                      Bram van Kampen
                      wrote on last edited by
                      #10

                      Well, what's being missed is that s0,s1 and s2 are char Arrays of 1,2 and 1 byte respectively, and NOT poiners! Why are they being displayed as DWORD. What byte packing is used. Unless if you have a constructor SOME(), in which you specifically initialise the structure, you should not rely on the content of the structure because it is unpredictable. The DEBUG version of the compiler tries to pad uninitialised and deleted structures with garbage in an attempt to make your code blow if you by mistake forget to initialise, or attempt to refer to de-allocated data. The Release version is by no means as friendly, in that it does none of these things. If your code works while referring to data outside their validity scope, you create a timebomb which at some time in the future will cause unpredictable and mysterious crashes. I note that you read the structure after deallocation, This mere read could lead to an invalid memory access crash. (ever tried to read a NULL pointer?) Regards, :)

                      Bram van Kampen

                      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