int x = new int[3];

This is a discussion on int x = new int[3]; within the C++ Programming forums, part of the General Programming Boards category; is this a way of creating an array of 3 int called x in C++?? (I've seen this somewhere.)...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    19

    int x = new int[3];

    is this a way of creating an array of 3 int called x in C++??

    (I've seen this somewhere.)

  2. #2
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    yes it is
    My Website
    010000110010101100101011
    Add Color To Your Code!

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    19
    but what's the difference between
    Code:
    int x[3] = {0};
    and
    Code:
    int x = new int[3];
    ???

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > int x = new int[3];
    This should be
    Code:
    int *x = new int[3];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    The former creates an array named x, of 3 ints, and zero initialises them. The latter will not even compile.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    That syntax isn't quite right. It should be:
    Code:
    int* x = new int[3];
    //...
    delete [] x;
    This is called dynamic allocation. The benefit is you can allocate whatever sized array you need at runtime. The disadvantage is you have to ensure the delete is called when you no longer need the array.

    In C++ an alternative to this could be std::vector

    Edit: Wow I'm slow...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    19
    to laser light, why won't it compile??

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,754
    to laser light, why won't it compile??
    Read the posts by Salem and JaWiB. You need a pointer to int.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    19
    okkkk!!! thanks!!
    Sorry for the silly that silly post ;P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 2
    Last Post: 03-24-2006, 07:36 PM
  3. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 06:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21