pointers

Discussion in 'C++' started by zombek@gmail.com, Nov 15, 2006.

  1. Guest

    Hi.
    I'd like to know which is faster and when to use:

    this (int type is just an example I mean it generally):
    int* ptr = new int (123);
    int* arr = new int [];

    and this:
    int num = 123;
    int arr [];

    Well I know that the first are pointers and the second are references.

    Szymon
     
    , Nov 15, 2006
    #1
    1. Advertising

  2. wrote:
    > I'd like to know which is faster and when to use:
    >
    > this (int type is just an example I mean it generally):
    > int* ptr = new int (123);
    > int* arr = new int [];


    The former allocates only one int. The latter is a syntax error.

    >
    > and this:
    > int num = 123;
    > int arr [];
    >
    > Well I know that the first are pointers and the second are references.


    The former declares/defines/initialises a single int. The latter
    declares an array of int of unknown size. These are semantically
    different.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #2
    1. Advertising

  3. Rolf Magnus Guest

    wrote:

    > Hi.
    > I'd like to know which is faster and when to use:
    >
    > this (int type is just an example I mean it generally):
    > int* ptr = new int (123);
    > int* arr = new int [];


    The second line is incorrect. You have to specify a size for the array.

    > and this:
    > int num = 123;
    > int arr [];


    Again, you have to specify a size for the array.

    > Well I know that the first are pointers and the second are references.


    There are no references involved at all.
    The first example shows dynamically allocated objects. You can use them if
    you want to control the life time manually. The object will exist until you
    use delete on the pointer. Use this only if you really need to.
     
    Rolf Magnus, Nov 15, 2006
    #3
  4. David Harmon Guest

    On 15 Nov 2006 14:53:57 -0800 in comp.lang.c++, wrote,
    >Well I know that the first are pointers and the second are references.


    The latter are not references, but instead they are the real thing,
    actual variables. That's generally what you want.

    Don't use pointers unless you have a real reason for them.
    Don't use 'new' unless you really have a real reason and the
    standard library containers like std::vector won't do the job.
     
    David Harmon, Nov 16, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil
    Replies:
    1
    Views:
    661
    llewelly
    Sep 16, 2003
  2. muser
    Replies:
    3
    Views:
    776
    Ron Natalie
    Sep 18, 2003
  3. A
    Replies:
    3
    Views:
    466
    Alan Kelon
    Oct 29, 2003
  4. Xamalek
    Replies:
    7
    Views:
    697
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    695
Loading...

Share This Page