new int?

Discussion in 'C++' started by thomas, Apr 9, 2008.

  1. thomas

    thomas Guest

    #include<iostream>
    using namespace std;

    int main(){
    int *x = new int[10];
    cout<<sizeof(x)/sizeof(int)<<endl;
    for(int i=0;i<20;i++) cout<<x<<" ";
    cout<<endl;
    }

    ********output:******
    1
    3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
    524748 369016
    8 3690232

    while I expected:
    10
    ??? error?
     
    thomas, Apr 9, 2008
    #1
    1. Advertising

  2. thomas

    Martin York Guest

    On Apr 9, 5:23 am, thomas <> wrote:
    > #include<iostream>
    > using namespace std;
    >
    > int main(){
    > int *x = new int[10];
    > cout<<sizeof(x)/sizeof(int)<<endl;
    > for(int i=0;i<20;i++) cout<<x<<" ";
    > cout<<endl;
    >
    > }
    >
    > ********output:******
    > 1
    > 3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
    > 524748 369016
    > 8 3690232
    >
    > while I expected:
    > 10
    > ??? error?


    sizeof(x) returns the sizeof(int*):
    the number of bytes used by a pointer to int.
    sizeof(int)
    the number of bytes used by a int.

    It looks like the size of the pointer is the same size of an int on
    your system. What you wanted was the size of what x pointed at.
    Unfortunately thats not possible via a pointer. What you may have
    wanted to do was:

    int main()
    {
    int x[10];
    cout<<sizeof(x)/sizeof(int)<<endl;
    }
     
    Martin York, Apr 9, 2008
    #2
    1. Advertising

  3. thomas

    Martin York Guest

    On Apr 9, 5:23 am, thomas <> wrote:
    > #include<iostream>
    > using namespace std;
    >
    > int main(){
    > int *x = new int[10];
    > cout<<sizeof(x)/sizeof(int)<<endl;
    > for(int i=0;i<20;i++) cout<<x<<" ";
    > cout<<endl;
    >
    > }
    >
    > ********output:******
    > 1
    > 3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
    > 524748 369016
    > 8 3690232
    >
    > while I expected:
    > 10
    > ??? error?


    Also note:
    1) <emory allocated by new int[10] is un-initialized. ie it contains
    random data. Hence the list of random numbers that you printed out.
    2) Accessing beyond the end of the array is undefined behavior so
    even though it managed to print out random numbers it is just as
    likely to crash the program.
    3) You should de-allocate the memory with delete [] x;
     
    Martin York, Apr 9, 2008
    #3
  4. On Apr 9, 1:23 pm, thomas <> wrote:
    >

    [snip]
    >
    > while I expected:
    > 10
    > ??? error?


    I assume you mean in the sizeof(x)/sizeof(int) line?

    The reason you don't get 10 here and you get 1 is that sizeof operator
    will not work on dynamically allocated storage such as pointers. It
    will just return the size of the pointer in bytes so in this case
    it'll return 4 (if on a 32-bit system). sizeof only works properly on
    stack variables, so if you had an array of 10 ints

    eg. int x[10];

    then

    cout << sizeof(x) << endl;

    would return 10

    You'll just have to store the size of the dynamically allocated array,
    there's no way to uniquely find the length of the array. If using
    strings, you can use strlen if the array is null terminated. But
    otherwise no, just store the length.
     
    Keith Halligan, Apr 9, 2008
    #4
  5. Martin York wrote:
    > 3) You should de-allocate the memory with delete [] x;


    Even better, you should avoid the 'new' altogether (if possible) and
    use "std::vector<int> x(10);" instead.
     
    Juha Nieminen, Apr 10, 2008
    #5
  6. thomas

    Jim Langston Guest

    "thomas" <> wrote in message
    news:...
    > #include<iostream>
    > using namespace std;
    >
    > int main(){
    > int *x = new int[10];
    > cout<<sizeof(x)/sizeof(int)<<endl;


    What is x? x is an int*. What is the size of a pointer? On your system it
    appears to be the size of an int (guessing 4). So this is outputting
    cout << 4 / 4 << endl;

    There is no way to know how much data a pointer is pointing to. You have to
    store that value somewhere or get the information some other way.

    > for(int i=0;i<20;i++) cout<<x<<" ";
    > cout<<endl;
    > }
    >
    > ********output:******
    > 1
    > 3670712 3670712 0 0 0 0 0 0 0 0 393219 524787 4301368 1 0 0 196610
    > 524748 369016
    > 8 3690232
    >
    > while I expected:
    > 10
    > ??? error?




    --
    Jim Langston
     
    Jim Langston, Apr 10, 2008
    #6
    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. Schnoffos
    Replies:
    2
    Views:
    1,223
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  4. Replies:
    6
    Views:
    437
    Andrey Tarasevich
    Nov 16, 2005
  5. Replies:
    9
    Views:
    436
    James Kanze
    Apr 17, 2007
Loading...

Share This Page