Thread: Dynamic 2 dimensional array with new?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    847

    Dynamic 2 dimensional array with new?

    I want to create a dynamic 2 dimensional array like this
    Code:
    parray = new z[x][y];
    Why dosen't new allow this? shouldn't it be possible for new to allocate memory the size of x*y and return a pointer to z[0][0]?

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Example of 2D memory allocation
    Code:
    int **ptr;
    ptr=new int *[10];//creates an array pointer
    for(int i=0;i<10;i++)
    p[i]=new int [5];//creates space for each row
    make sure u delete the mempry after use

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    thanx

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    To delete that memory:

    Code:
    for(int i=0;i<10;i++)
        delete p[i];
    delete [] p;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Not quite. Should be:
    Code:
    for(int i=0;i<10;i++)
        delete [] p[i];
    delete [] p;

  6. #6
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    I think what dwks is saying is right

  7. #7
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    >> delete [] p[i];

    nice catch

  8. #8
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    yes...it should be
    Code:
    delete[] p[i];

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Oops. Yes.
    Code:
    delete [] p[i];
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    Sometimes oops just isnt good enough.

    maybe oops[100] would do.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Perhaps you were thinking of [50]. 10*5 = 50.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 02:04 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Dynamic 2 Dimensional Array
    By mrb260478 in forum C Programming
    Replies: 23
    Last Post: 06-21-2008, 07:19 AM
  4. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  5. total size of dynamic memory allocated array
    By trekker in forum C Programming
    Replies: 10
    Last Post: 03-10-2002, 12:59 PM