CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2003
    Location
    Pasadena, CA
    Posts
    48

    another childish 'exception' question

    if...

    int main(void) {
    int * p(some_value);

    p = new int;

    ...

    return 0;
    }


    Assume that the 'new' invocation fails generating a 'bad_alloc' exception... is the value of p modified? if so, is the value undefined? My gut feel would be that p is not modified and (p == some_value) still holds true but I'd like some confirmation on this.
    The views expressed are those of the author and do not reflect any position taken by the Goverment of the United States of America, National Aeronautics and Space Administration (NASA), Jet Propulsion Laboratory (JPL), or California Institute of Technology (CalTech)

  2. #2
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128
    I believe that the exception should be thrown from within the new operator or any underlying function. Thus it shoudn't be affecting the pointer, p.

  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470
    Remember that you have two operators in that expression: the new operator and the assignment operator. Assignment operators for fundamental types do not throw exceptions, so only the new operator could throw, which will happen before the assignment operator is invoked.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured