new int?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thomas

    new int?

    #include<iostre am>
    using namespace std;

    int main(){
    int *x = new int[10];
    cout<<sizeof(x)/sizeof(int)<<en dl;
    for(int i=0;i<20;i++) cout<<x[i]<<" ";
    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?
  • Martin York

    #2
    Re: new int?

    On Apr 9, 5:23 am, thomas <FreshTho...@gm ail.comwrote:
    #include<iostre am>
    using namespace std;
    >
    int main(){
    int *x = new int[10];
    cout<<sizeof(x)/sizeof(int)<<en dl;
    for(int i=0;i<20;i++) cout<<x[i]<<" ";
    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)<<en dl;
    }

    Comment

    • Martin York

      #3
      Re: new int?

      On Apr 9, 5:23 am, thomas <FreshTho...@gm ail.comwrote:
      #include<iostre am>
      using namespace std;
      >
      int main(){
      int *x = new int[10];
      cout<<sizeof(x)/sizeof(int)<<en dl;
      for(int i=0;i<20;i++) cout<<x[i]<<" ";
      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;

      Comment

      • Juha Nieminen

        #4
        Re: new int?

        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<in tx(10);" instead.

        Comment

        • Jim Langston

          #5
          Re: new int?

          "thomas" <FreshThomas@gm ail.comwrote in message
          news:a97a6727-d185-4d5a-a9c9-5d2ca9719c4b@w4 g2000prd.google groups.com...
          #include<iostre am>
          using namespace std;
          >
          int main(){
          int *x = new int[10];
          cout<<sizeof(x)/sizeof(int)<<en dl;
          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[i]<<" ";
          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
          tazmaster@rocke tmail.com


          Comment

          Working...