Learning pointers question

Discussion in 'C++' started by macawm, Nov 24, 2005.

  1. macawm

    macawm Guest

    Hi all,
    What is the difference between these two statements?
    int* a = new int[8]
    and
    int a[8]

    The reason I ask is because this
    int len = sizeof(a) / sizeof(*a)
    works for the second construct, but not the first.
    macawm, Nov 24, 2005
    #1
    1. Advertising

  2. macawm

    Ron Natalie Guest

    macawm wrote:
    > Hi all,
    > What is the difference between these two statements?
    > int* a = new int[8]


    a is a pointer that is initialized with the address
    of a dynamically allocated array of 8 ints.

    > and
    > int a[8];


    a is an ARRAY of 8 ints (allocated wherever this statement
    appears).
    >
    > The reason I ask is because this
    > int len = sizeof(a) / sizeof(*a)
    > works for the second construct, but not the first.
    >

    Pointers are not arrays nor vice versa.

    The size of a pointer is the same regardless of what value
    you set it to and is unrelated to the size of the thing
    it points to.

    An array however, has a size that is the number of elements
    in the array times the size of each element. Hence your
    length test works fine to reverse that.

    Pointers only really have the concept of pointing to one
    thing. If you dynamically allocate an array, you'll have
    to remember the size. Alternatively (and this is almost
    certainly a better idea), use the standard vector class.

    vector<int> a(8);

    You can do things like:
    a[3]
    and you can ask the size by
    a.size()

    and you can size it with a variable (arrays only work with constant
    sizes) and even resize it. You further don't have to remember
    to free it or do special things when copying like you would for
    dynamic allocations.
    Ron Natalie, Nov 24, 2005
    #2
    1. Advertising

  3. macawm

    Moonlit Guest

    Hi,

    int a[8] is created on the stack it ceases to exist when the function
    returns ( well actually it points to memory that is likely to be
    overwritten).

    a variable with new is created on the heap (a bunch of memory located
    somewhere independent of the function).

    in the first case your expression yields:

    sizeof integer pointer divided by sizeof where the pointer is pointing to
    (i.e. an int)

    the second case
    the sizeof of the array (8 times sizeof int) divided by sizeof int ( a
    becomes pointer to array of int so sizeof *a is sizeof int)

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "macawm" <> wrote in message
    news:...
    > Hi all,
    > What is the difference between these two statements?
    > int* a = new int[8]
    > and
    > int a[8]
    >
    > The reason I ask is because this
    > int len = sizeof(a) / sizeof(*a)
    > works for the second construct, but not the first.
    >
    Moonlit, Nov 24, 2005
    #3
  4. macawm

    Peter_Julian Guest

    "macawm" <> wrote in message
    news:...
    | Hi all,
    | What is the difference between these two statements?
    | int* a = new int[8]
    | and
    | int a[8]
    |
    | The reason I ask is because this
    | int len = sizeof(a) / sizeof(*a)
    | works for the second construct, but not the first.
    |

    The first statement allocates the array on the heap and the second is
    allocated on the stack. As far as int *a is concerned, consider writing
    those 2 statements like so:

    int *p = new int[10];
    delete [] p;

    int a[10];

    and keep in mind that p is a pointer to an integer, not an array.

    That len statement should be:

    int len = sizeof(a) / sizeof(int);

    which only applies to the stack allocated container.

    and since p is not pointing to an array:

    const int len(10);
    int *p = new int[len];
    std::cout << "len = " << len << std::endl;
    delete [] p;

    ___
    Vectors to the rescue:

    std::vector<int> v(10, 0); // an instant vector of 10 elements preset to
    0
    std::cout << v.size() << std::endl;

    std::vector<int> *p = v;

    now p *is really* pointing to a std::vector of integers. Lets go one
    step further, the following works regardless of what size the vector is
    at runtime. Using classic STL with a reference instead of dumb pointers.

    #include <iostream>
    #include <ostream>
    #include <vector>

    template< class T >
    void display(const std::vector< T >& v) // ref to a std::vector<T>
    {
    std::cout << "\nstd::vector with " << v.size() << " elements.\n";
    for(int i = 0; i < v.size();++i) { std::cout << v << " "; }
    std::cout << std::endl;
    }

    int main()
    {
    std::vector<int> vn(10, 0);
    display<int>(vn); // templated for integers

    std::vector<double> vd(5, 1.1);
    display<double>(vd); // templated for doubles

    vd.push_back(1.2);
    vd.push_back(1.3);
    display<double>(vd);

    return 0;
    }

    /*

    std::vector with 10 elements.
    0 0 0 0 0 0 0 0 0 0

    std::vector with 5 elements.
    1.1 1.1 1.1 1.1 1.1

    std::vector with 7 elements.
    1.1 1.1 1.1 1.1 1.1 1.2 1.3

    */

    Think about the astronomical amount of work required to duplicate that
    project using primitive arrays. Note that i could have used std::strings
    or for that matter any user-defined class(with a defined friend
    operator<<).

    std::vector< std::string > vs(1000, "string");
    display(vs);
    std::vector< MyClass > my_v(100, MyClass(...));
    display(my_v);

    And you wouldn't need to modify display() in any way.
    Peter_Julian, Nov 25, 2005
    #4

  5. > Hi all,
    > What is the difference between these two statements?
    > int* a = new int[8]
    > and
    > int a[8]
    >
    > The reason I ask is because this
    > int len = sizeof(a) / sizeof(*a)
    > works for the second construct, but not the first.
    >

    Here's a link to a C++Course explaining all the stuff around pointers and
    references (and more):

    http://www.vias.org/cppcourse/chap16_02.html

    Just browse through the pages or use the index, there is plenty of
    introductory material..

    Hope this helps,

    Hans


    --
    =====================================
    Hans Lohninger
    EPINA GmbH - Software Development Lohninger
    www.lohninger.com
    mailto:eek:
    fax: +43-2233-541945
    ======================================
    Hans Lohninger, Nov 25, 2005
    #5
  6. macawm

    Guest

    Peter_Julian wrote:
    > "macawm" <> wrote in message
    > news:...
    > | Hi all,
    > | What is the difference between these two statements?
    > | int* a = new int[8]
    > | and
    > | int a[8]
    > |
    > | The reason I ask is because this
    > | int len = sizeof(a) / sizeof(*a)
    > | works for the second construct, but not the first.


    <snip>

    > That len statement should be:
    >
    > int len = sizeof(a) / sizeof(int);


    No it shouldn't

    If you want to use this technique to measure the size of an array, the
    OP's code is better. If the array type is changed to, for example

    short a[8];

    you have to remember to change your len statement to

    int len = sizeof(a) / sizeof(short);

    or you will get the wrong answer (unless short and int happen to be the
    same size - but then what about double ...)

    The OP's code is more robust. You can change they type of the array and
    the len statement will still be correct as written.

    Gavin Deane
    , Nov 25, 2005
    #6
  7. * Hans Lohninger:
    >
    > > Hi all,
    > > What is the difference between these two statements?
    > > int* a = new int[8]
    > > and
    > > int a[8]
    > >
    > > The reason I ask is because this
    > > int len = sizeof(a) / sizeof(*a)
    > > works for the second construct, but not the first.
    > >

    > Here's a link to a C++Course explaining all the stuff around pointers and
    > references (and more):
    >
    > http://www.vias.org/cppcourse/chap16_02.html
    >
    > Just browse through the pages or use the index, there is plenty of
    > introductory material..


    It's not a good idea to post links to garbage.

    <quote>
    #include <iostream.h>

    int main()
    {
    int someNumber = 12345;
    int* ptrSomeNumber = &someNumber;

    cout << "someNumber = " << someNumber << endl;
    cout << "ptrSomeNumber = " << ptrSomeNumber << endl;

    return 0;
    }

    If you compiled and ran the above code, you would have the variable
    someNumber output 12345 while ptrSomeNumber would output some
    hexadecimal number (addresses in memory are represented in hex).
    </quote>

    The first line quoted says: _stay away_.

    And the last line quoted says the same.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 25, 2005
    #7
  8. macawm

    macawm Guest

    Thanks to everyone. Your replies were to the point and fully explained
    what I was missing. I didn't fully realize the implications of
    dynamically allocating arrays. And I do agree with those that suggested
    Vector, it seems to be a much nicer beast.

    I was under the assumption that arrays are pointers.
    int a[8]
    *a or *(a+1) or *(a+n) will give the first, second, and nth element
    of the array by dereferencing a's address.
    Is this just wrong or am I still missing something?
    macawm, Nov 25, 2005
    #8
  9. * macawm:
    >
    > I was under the assumption that arrays are pointers.
    > int a[8]
    > *a or *(a+1) or *(a+n) will give the first, second, and nth element
    > of the array by dereferencing a's address.
    > Is this just wrong or am I still missing something?


    An array decays to a pointer to its first element in the cases you list.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 25, 2005
    #9
    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. Javier

    Learning pointers in C++

    Javier, Feb 3, 2005, in forum: C++
    Replies:
    17
    Views:
    2,060
    Noah Roberts
    Feb 3, 2005
  2. Replies:
    1
    Views:
    631
    -berlin.de
    Mar 28, 2005
  3. Hal Vaughan
    Replies:
    7
    Views:
    458
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    642
  5. Andrey Popp

    [I'm learning C]: Learning to use ucontext

    Andrey Popp, Jan 29, 2012, in forum: C Programming
    Replies:
    5
    Views:
    681
    Keith Thompson
    Jan 31, 2012
Loading...

Share This Page