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 and memory allocation

CArray and memory allocation

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestion
5 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.
  • S Offline
    S Offline
    Space Ace
    wrote on last edited by
    #1

    I wonder if there's a way of making Windows not reallocate the memory for CArray objects without using CArray::SetSize(int) function? I mean I want to create a dynamic array of relatively large objects and I need pointers to the elements. When I add a new element with CArray::Add function, the operating system reallocates the memory and my pointers to the previous elements become invalid. Is there any way of 'fixing' the addresses of the elements?

    R C M J 4 Replies Last reply
    0
    • S Space Ace

      I wonder if there's a way of making Windows not reallocate the memory for CArray objects without using CArray::SetSize(int) function? I mean I want to create a dynamic array of relatively large objects and I need pointers to the elements. When I add a new element with CArray::Add function, the operating system reallocates the memory and my pointers to the previous elements become invalid. Is there any way of 'fixing' the addresses of the elements?

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Space Ace wrote: When I add a new element with CArray::Add function, the operating system reallocates the memory and my pointers to the previous elements become invalid. Not really. I suspect there are problems in your code. Can you post a sample? /ravi Let's put "civil" back in "civilization" http://www.ravib.com ravib@ravib.com

      1 Reply Last reply
      0
      • S Space Ace

        I wonder if there's a way of making Windows not reallocate the memory for CArray objects without using CArray::SetSize(int) function? I mean I want to create a dynamic array of relatively large objects and I need pointers to the elements. When I add a new element with CArray::Add function, the operating system reallocates the memory and my pointers to the previous elements become invalid. Is there any way of 'fixing' the addresses of the elements?

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

        If it's anything like vector you should be able to pass an initial size into the constructor, then it won't reallocate until you go over that size. That naturally removes one of the main advantages of a dynamic array. By the way, CArray is absolute trash, you should ditch it and use vector instead. Christian We're just observing the seasonal migration from VB to VC. Most of these birds will be killed by predators or will die of hunger. Only the best will survive - Tomasz Sowinski 29-07-2002 ( on the number of newbie posters in the VC forum ) Cats, and most other animals apart from mad cows can write fully functional vb code. - Simon Walton - 6-Aug-2002

        1 Reply Last reply
        0
        • S Space Ace

          I wonder if there's a way of making Windows not reallocate the memory for CArray objects without using CArray::SetSize(int) function? I mean I want to create a dynamic array of relatively large objects and I need pointers to the elements. When I add a new element with CArray::Add function, the operating system reallocates the memory and my pointers to the previous elements become invalid. Is there any way of 'fixing' the addresses of the elements?

          M Offline
          M Offline
          Masaaki Onishi
          wrote on last edited by
          #4

          Hello, the codegurus around the world.;) Generally speaking, we can define Find function to find CArray index. So, without the pointer of CArray, we can find any index with some key value. This means if we derived some class from CObject, we can insert this class in CArray template. (Or, I misunderstand something? X| ) Please, don't send me your email about your questions directly. Have a nice day! Sonork - 100.10571:vcdeveloper ;)

          -Masaaki Onishi-

          1 Reply Last reply
          0
          • S Space Ace

            I wonder if there's a way of making Windows not reallocate the memory for CArray objects without using CArray::SetSize(int) function? I mean I want to create a dynamic array of relatively large objects and I need pointers to the elements. When I add a new element with CArray::Add function, the operating system reallocates the memory and my pointers to the previous elements become invalid. Is there any way of 'fixing' the addresses of the elements?

            J Offline
            J Offline
            Joel Lucsy
            wrote on last edited by
            #5

            Store the index instead of the pointer. You can always get the pointer back. Joel Lucsy (jjlucsy@ameritech.net)

            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