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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to do new CArray???

How to do new CArray???

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsharpc++visual-studiotutorial
8 Posts 4 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.
  • G Offline
    G Offline
    Greg Ellis
    wrote on last edited by
    #1

    Hi Guys, I am having a problem creating a pointer to a CArray structure; CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>; This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, Greg

    PJ ArendsP C M 3 Replies Last reply
    0
    • G Greg Ellis

      Hi Guys, I am having a problem creating a pointer to a CArray structure; CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>; This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, Greg

      PJ ArendsP Offline
      PJ ArendsP Offline
      PJ Arends
      wrote on last edited by
      #2

      Have you read the documentation for how to use a CArray? The CArray class is an array management class, not a simply C array. Read the docs and use the class' member functions such as SetSize() and SetAt().

      Within you lies the power for good; Use it!

      G 1 Reply Last reply
      0
      • PJ ArendsP PJ Arends

        Have you read the documentation for how to use a CArray? The CArray class is an array management class, not a simply C array. Read the docs and use the class' member functions such as SetSize() and SetAt().

        G Offline
        G Offline
        Greg Ellis
        wrote on last edited by
        #3

        Yep I have, I am not merely trying to use a CArray I am trying to create a pointer to an array of CArray's. It won't let me do it using malloc for some reason.

        PJ ArendsP 1 Reply Last reply
        0
        • G Greg Ellis

          Yep I have, I am not merely trying to use a CArray I am trying to create a pointer to an array of CArray's. It won't let me do it using malloc for some reason.

          PJ ArendsP Offline
          PJ ArendsP Offline
          PJ Arends
          wrote on last edited by
          #4

          malloc is a C function, not a C++ function. It simply allocates a block of memory, nothing more. You need to use new which allocates a block of memory and then calls the class' c'tor.


          You may be right
          I may be crazy
          -- Billy Joel --

          Within you lies the power for good, use it!!!

          Within you lies the power for good; Use it!

          G 1 Reply Last reply
          0
          • G Greg Ellis

            Hi Guys, I am having a problem creating a pointer to a CArray structure; CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>; This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, Greg

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

            You should never use malloc in C++. You should also prefer to use std::vector over CArray, as it is standard and it is more flexible/powerful.

            Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            1 Reply Last reply
            0
            • PJ ArendsP PJ Arends

              malloc is a C function, not a C++ function. It simply allocates a block of memory, nothing more. You need to use new which allocates a block of memory and then calls the class' c'tor.


              You may be right
              I may be crazy
              -- Billy Joel --

              Within you lies the power for good, use it!!!

              G Offline
              G Offline
              Greg Ellis
              wrote on last edited by
              #6

              ok, but I need to dynamically allocate memory so I wanted to be able to use realloc. Is there a realloc equivilent for new? Thanks

              PJ ArendsP 1 Reply Last reply
              0
              • G Greg Ellis

                ok, but I need to dynamically allocate memory so I wanted to be able to use realloc. Is there a realloc equivilent for new? Thanks

                PJ ArendsP Offline
                PJ ArendsP Offline
                PJ Arends
                wrote on last edited by
                #7

                Why? CArray takes care of all the memory allocations itself. There is no need for you to worry about it.


                You may be right
                I may be crazy
                -- Billy Joel --

                Within you lies the power for good, use it!!!

                Within you lies the power for good; Use it!

                1 Reply Last reply
                0
                • G Greg Ellis

                  Hi Guys, I am having a problem creating a pointer to a CArray structure; CArray<CBackground*, CBackground*>* m_paryBackground; int nNewSize = 2; m_paryBackground = (CArray<CBackground*, CBackground*>*)malloc(nNewSize * sizeof(CArray<CBackground*, CBackground*>*)); m_paryBackground[0] = new CArray<CBackground*, CBackground*>; m_paryBackground[1] = new CArray<CBackground*, CBackground*>; This does not work for some reason. I get a compile time error saying it cannot cast to the m_paryBackground variable. Does anyone know how I can do this properly? I'm using MFC 7 visual studio 2005 Cheers, Greg

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  Something like this?

                  size_t nNewSize = 2;
                  CArray<CBackground*, CBackground*>* m_paryBackground = new CArray<CBackground*, CBackground*>[nNewSize];

                  In the future, you should include the compiler error you are getting so we don't have to guess.

                  --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ Ford, what's this fish doing in my ear?

                  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