DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Array declaration syntax

  1. #1
    Mike Reilly Guest

    Array declaration syntax

    Did the C++ standard change for array declaration after the draft standard?
    I'm looking at the following in both VC++ 6 and with gcc/g++:

    int * pIntArray = new int[100][100];

    This is invalid, though according to a book on ansi C++ I have it should be.

    int * pIntArray = new int[100,100];

    is valid and works on both compilers, though the book I'm reading doesn't
    mention it anywhere.



  2. #2
    Danny Kalev Guest

    Re: Array declaration syntax



    Mike Reilly wrote:
    >
    > Did the C++ standard change for array declaration after the draft standard?
    > I'm looking at the following in both VC++ 6 and with gcc/g++:


    No it didn't. You should use the following form :

    int (*ppi) [5]=new int[4][5]; // parentheses required
    >
    > int * pIntArray = new int[100][100];
    >
    > This is invalid, though according to a book on ansi C++ I have it should be.


    >
    > int * pIntArray = new int[100,100];
    >
    > is valid and works on both compilers, though the book I'm reading doesn't
    > mention it anywhere.


    This works but has an entirely different meaning: the comma is treated
    as a comma separated expression. Without getting too technical, you're
    actually allocating a one dim. array here of 100 elements.

    Danny


  3. #3
    Mike Reilly Guest

    Re: Array declaration syntax

    :No it didn't. You should use the following form :
    :
    :int (*ppi) [5]=new int[4][5]; // parentheses required
    :>
    :> int * pIntArray = new int[100][100];
    So in my case I should use:

    int (*pIntArray)[100] = new int[100][100];

    Is that correct for creating a multidimensional array on the free store?



  4. #4
    Danny Kalev Guest

    Re: Array declaration syntax



    Mike Reilly wrote:
    >
    > :No it didn't. You should use the following form :
    > :
    > :int (*ppi) [5]=new int[4][5]; // parentheses required
    > :>
    > :> int * pIntArray = new int[100][100];
    > So in my case I should use:
    >
    > int (*pIntArray)[100] = new int[100][100];
    >
    > Is that correct for creating a multidimensional array on the free store?


    yep. use delete[] pIntArray; to delete that array.

    Danny

  5. #5
    Mike Reilly Guest

    Re: Array declaration syntax

    >yep. use delete[] pIntArray; to delete that array.
    >
    >Danny

    Hehe, yup, wouldn't want any memory leaks. Thanks again, Danny, both for
    this answer and the fact that every time I've checked into this group you've
    been here answering questions for folks!



  6. #6
    jonnin Guest

    Re: Array declaration syntax


    I should know this, but,
    what would you do to create a 2-d array using variable indices. I know there
    is a resizable array class somewhere in the libs, but in hard c++,

    so we get
    float x[rows][cols];




    "Mike Reilly" <mreilly@*spamremover*sd71.bc.ca> wrote:
    >>yep. use delete[] pIntArray; to delete that array.
    >>
    >>Danny

    >Hehe, yup, wouldn't want any memory leaks. Thanks again, Danny, both for
    >this answer and the fact that every time I've checked into this group you've
    >been here answering questions for folks!
    >
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center