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. CArray memory issue

CArray memory issue

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformancehelp
7 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.
  • M Offline
    M Offline
    mohanrajh
    wrote on last edited by
    #1

    Hey guys, I would like to know what happends with memory in this code, CArray myArray; //my array could be 0 or can grow upto 1000 myArray.SetSize(0,1000); for (int j = 0; j < 900; j++) { myArray.Add(j); } //what is below line going to do? myArray.FreeExtra(); Is SetSize & FreeExtra functions anyway improves anything here or it would be better of without these lines. Thanks Mohan

    C L G 3 Replies Last reply
    0
    • M mohanrajh

      Hey guys, I would like to know what happends with memory in this code, CArray myArray; //my array could be 0 or can grow upto 1000 myArray.SetSize(0,1000); for (int j = 0; j < 900; j++) { myArray.Add(j); } //what is below line going to do? myArray.FreeExtra(); Is SetSize & FreeExtra functions anyway improves anything here or it would be better of without these lines. Thanks Mohan

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      http://www.google.com.au/search?hl=en&q=CArray+FreeExtra&meta=[^] FreeExtra apparrenty will free any memory allocated, but I would guess what it does depends on the classes grow policy, no memory was pre allocated. std::vector is a far better container to use than CArray. Christian Graus - Microsoft MVP - C++

      N 1 Reply Last reply
      0
      • M mohanrajh

        Hey guys, I would like to know what happends with memory in this code, CArray myArray; //my array could be 0 or can grow upto 1000 myArray.SetSize(0,1000); for (int j = 0; j < 900; j++) { myArray.Add(j); } //what is below line going to do? myArray.FreeExtra(); Is SetSize & FreeExtra functions anyway improves anything here or it would be better of without these lines. Thanks Mohan

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

        HI Mohan, function FreeExtra(): Frees any extra memory that was allocated while the array was grown. This function has no effect on the size or upper bound of the array function SetSize(): Use this function to set the size of your array before you begin using the array. If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory ...referenced from MSDN oct 2001 Knock out 'T' from CAN'T , You 'CAN' if you think you 'CAN' :cool:

        1 Reply Last reply
        0
        • C Christian Graus

          http://www.google.com.au/search?hl=en&q=CArray+FreeExtra&meta=[^] FreeExtra apparrenty will free any memory allocated, but I would guess what it does depends on the classes grow policy, no memory was pre allocated. std::vector is a far better container to use than CArray. Christian Graus - Microsoft MVP - C++

          N Offline
          N Offline
          NiceNaidu fo
          wrote on last edited by
          #4

          CArray myArray; int i; // Allocate memory for at least 32 elements. myArray.SetSize(32, 128); // Add elements to the array. CPoint* pPt = (CPoint*) myArray.GetData(); for (i=0;i < 32;i++,pPt++) *pPt = CPoint(i, 2*i); **// Only keep first 5 elements and free extra (unused) bytes.** myArray.SetSize(5, 128); myArray.FreeExtra(); Look at the above code . Hope now u can understand the use of FreeExtra. Appu.. "If you judge people, you have no time to love them."

          1 Reply Last reply
          0
          • M mohanrajh

            Hey guys, I would like to know what happends with memory in this code, CArray myArray; //my array could be 0 or can grow upto 1000 myArray.SetSize(0,1000); for (int j = 0; j < 900; j++) { myArray.Add(j); } //what is below line going to do? myArray.FreeExtra(); Is SetSize & FreeExtra functions anyway improves anything here or it would be better of without these lines. Thanks Mohan

            G Offline
            G Offline
            grigsoft
            wrote on last edited by
            #5

            With SetSize your code runs faster - memory for 1000 items is allocated and not copied\extended during adding new items. Without it array will allocate new memory several times, copying all items to new location. FreeExtra just free unused memory - but it have to copy whole array to new location, which cost time. So I almost never use it - if you will want to add new item afterwards, you will cause array to copy items again. Igor Green http://www.grigsoft.com/ - files and folders comparison tools

            M 1 Reply Last reply
            0
            • G grigsoft

              With SetSize your code runs faster - memory for 1000 items is allocated and not copied\extended during adding new items. Without it array will allocate new memory several times, copying all items to new location. FreeExtra just free unused memory - but it have to copy whole array to new location, which cost time. So I almost never use it - if you will want to add new item afterwards, you will cause array to copy items again. Igor Green http://www.grigsoft.com/ - files and folders comparison tools

              M Offline
              M Offline
              mohanrajh
              wrote on last edited by
              #6

              Igor, I kinda got a clue, thanks. But i got a question... I load the array only once when my application loads & then i just use it for reference purpose only, in this case if i include SetSize(0,1000) & the array loads only 500 items, will that mean the memory for other 500 still exists & not released? Thanks. Mohan

              G 1 Reply Last reply
              0
              • M mohanrajh

                Igor, I kinda got a clue, thanks. But i got a question... I load the array only once when my application loads & then i just use it for reference purpose only, in this case if i include SetSize(0,1000) & the array loads only 500 items, will that mean the memory for other 500 still exists & not released? Thanks. Mohan

                G Offline
                G Offline
                grigsoft
                wrote on last edited by
                #7

                Yes, that's true. Igor Green http://www.grigsoft.com/ - files and folders comparison tools

                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