can you help in this programs?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tamara omar
    New Member
    • Sep 2006
    • 23

    can you help in this programs?

    i want to know what happens when running these programs:
    void main()
    {
    int *p=new int[10];
    while(p)
    p=new int[1000];
    *p=99;
    cout<<*p<<endl;
    }


    void main()
    { double*p;
    while(p!=null)
    {
    p=new double[5000];
    p=new double[10];
    for(inti=0; i<10;i++)
    cin>>p[i];
    for(i=0;i<10;i+ +)
    }
    cout<<"p["<<i<<"]"<<p[i]<<endl:
    }
  • asdfghjkl2007
    New Member
    • Oct 2006
    • 30

    #2
    Originally posted by tamara omar
    i want to know what happens when running these programs:
    void main()
    {
    int *p=new int[10];
    while(p)
    p=new int[1000];
    *p=99;
    cout<<*p<<endl;
    }


    void main()
    { double*p;
    while(p!=null)
    {
    p=new double[5000];
    p=new double[10];
    for(inti=0; i<10;i++)
    cin>>p[i];
    for(i=0;i<10;i+ +)
    }
    cout<<"p["<<i<<"]"<<p[i]<<endl:
    }


    for the first one

    first p will adress a [10] int array then
    it will enter the while loop
    - it will adress a [1000] int array
    -the first element of the [1000] array will become 99 thenh it will write it on the consol
    -then the while will repeat it self until you'll go out of memory
    because the arrays are created but aren't deleted and they continue to exeits although p isen't pointing at the actualy there is no pointer to them
    the spaces you alocated for the arrays will become memory holes
    then you'll problebly receve an error mesage


    for the second one

    i never used " null" if you mean " (p!=0) "
    you'll probably get a "The variable 'p' is being used without being defined. " error
    and it won't compile


    who wrote these programs??? and why ??????? :)
    i supose you didn't wrote these becose if you did you wouldn't had asked :)

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by asdfghjkl2007
      for the first one

      first p will adress a [10] int array then
      it will enter the while loop
      - it will adress a [1000] int array
      -the first element of the [1000] array will become 99 thenh it will write it on the consol
      -then the while will repeat it self until you'll go out of memory
      because the arrays are created but aren't deleted and they continue to exeits although p isen't pointing at the actualy there is no pointer to them
      the spaces you alocated for the arrays will become memory holes
      then you'll problebly receve an error mesage
      You haven't got this quite right, note there is no { following the while so in fact what it does is repeatedly allocate an array of memory until it runs out of memory. Then it writes 99 to the pointer returned from new when the last memory allocation failed, i.e. it writes to a NULL pointer and then it reads from the same NULL pointer.

      This invokes undefined behaviour

      The second example does not compile because it is has at least 2 obvious syntax errors (I stopped looking after that). It therefore does nothing.

      Comment

      • asdfghjkl2007
        New Member
        • Oct 2006
        • 30

        #4
        Originally posted by Banfa
        You haven't got this quite right, note there is no { following the while so in fact what it does is repeatedly allocate an array of memory until it runs out of memory. Then it writes 99 to the pointer returned from new when the last memory allocation failed, i.e. it writes to a NULL pointer and then it reads from the same NULL pointer.

        This invokes undefined behaviour

        The second example does not compile because it is has at least 2 obvious syntax errors (I stopped looking after that). It therefore does nothing.

        yep silly me :D I should wear my glaces more often @-)

        Comment

        Working...