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. Dynamic creation of 3D Array

Dynamic creation of 3D Array

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelp
2 Posts 2 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
    Mahesh Varma
    wrote on last edited by
    #1

    Hi all, How can i dynamically create a 3D array ? I have tried using new operator. But apparently it only support the dynamic creation of the first dimension.Can someone help me? Thanks Mahesh

    A 1 Reply Last reply
    0
    • M Mahesh Varma

      Hi all, How can i dynamically create a 3D array ? I have tried using new operator. But apparently it only support the dynamic creation of the first dimension.Can someone help me? Thanks Mahesh

      A Offline
      A Offline
      Andrew Walker
      wrote on last edited by
      #2

      The short answer is because the syntax to do so is horrible, you can use much nicer tools to do it for you. I believe this will do what you want. Note the use of the delete[] operator to delete arrays.

      = new int*[dy]; for(y = 0; y < dy; y++) { pMatrix[x][y] = new int[dz]; } } for(x = 0; x < dx; x++) { for(y = 0; y < dy; y++) { delete[] pMatrix[x][y]; } delete[] pMatrix[x]; } delete[] pMatrix;
      I think you'll agree that this is much nicer. vector<vector<vector<int> > > matrix; matrix[0][0][0] = 1;However if you need to do alot of matrix math then I strongly suggest you look at something like boost::uBlas which uses template proxy classes to eliminate the expensive copies that are the price of using vectors.


      If you can keep you head when all about you Are losing theirs and blaming it on you; If you can dream - and not make dreams your master; If you can think - and not make thoughts you aim; Yours is the Earth and everything that's in it. Rudyard Kipling

      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