Thread: auto_ptr array

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Let us know if you have found definite result from standard whether the code in post #11 is exception safe or not? To my point of view, I think it should be safe.
    He did ("Yeah, no such thing there.") and I agree with him. The code is not guaranteed to be exception safe.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks laserlight,


    A little mess. Let me confirm. :-)

    1.

    For code in post #8, we are definitely sure it is not exception safe?

    2.

    For code in post #11, we think it is implementation defined whether or not it is exception safe?

    Quote Originally Posted by laserlight View Post
    He did ("Yeah, no such thing there.") and I agree with him. The code is not guaranteed to be exception safe.

    regards,
    George

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    For code in post #11, we think it is implementation defined whether or not it is exception safe?
    And that, by definition, makes it unsafe. If it's not guaranteed to be safe, then it's not safe.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Now I understand both coding practice of code in post #8 and in post #11 are not exception safe.

    Quote Originally Posted by matsp View Post
    And that, by definition, makes it unsafe. If it's not guaranteed to be safe, then it's not safe.

    --
    Mats

    regards,
    George

  5. #20
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by George2 View Post
    For code in post #8, we are definitely sure it is not exception safe?
    I believe the verdict was "won't compile".

    For code in post #11, we think it is implementation defined whether or not it is exception safe?
    No, the implementation is under no obligation to define anything here. It's unspecified, and may change from one compilation to the other, based on some totally unrelated changes that change the way the optimizer reasons about the code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    1.

    Quote Originally Posted by CornedBee View Post
    I believe the verdict was "won't compile".
    You are correct. I have tried it.

    2.

    Quote Originally Posted by CornedBee View Post
    No, the implementation is under no obligation to define anything here. It's unspecified, and may change from one compilation to the other, based on some totally unrelated changes that change the way the optimizer reasons about the code.
    Interested. In my normal senses, the code in post #11 should not leak. If you know any compiler which will leak memory in code sample in post #11, please let me know. :-)


    regards,
    George

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks CornedBee,

    Interested. In my normal senses, the code in post #11 should not leak. If you know any compiler which will leak memory in code sample in post #11, please let me know. :-)


    regards,
    George
    Well, it won't leak unless you are low on memory, but there's (according to the previous discussion) no guarantee that the compile produces the actual auto_ptr immediately as it's allocating the memory for it, so you may end up with a dangling pointer if you have an exception during the initialization of the array. And as Cornedbee says: It may change at a whim of the compiler, e.g. you change the options, the code generator thinks the function is "too complex", so it uses a different set of rules for the "too complex" function compared to a simple function [because you added an extra if-statement at the end of the code, for example].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Three more comments,

    1.

    Quote Originally Posted by matsp View Post
    Well, it won't leak unless you are low on memory,
    Memory leak has nothing to do with whether memory is low or not?

    2.

    Quote Originally Posted by matsp View Post
    but there's (according to the previous discussion) no guarantee that the compile produces the actual auto_ptr immediately as it's allocating the memory for it, so you may end up with a dangling pointer if you have an exception during the initialization of the array.
    "Dangling pointer" means, if compiler decides to allocate memory before constructing auto_ptr, and exception in the middle before constructing auto_ptr, no pointer pointed to the allocated memory, so this is what you mean "Dangling pointer"?

    3.

    Quote Originally Posted by matsp View Post
    And as Cornedbee says: It may change at a whim of the compiler, e.g. you change the options, the code generator thinks the function is "too complex", so it uses a different set of rules for the "too complex" function compared to a simple function [because you added an extra if-statement at the end of the code, for example].

    --
    Mats
    I do not understand your "if-statement" sample, can you show the code?


    regards,
    George

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Memory leak has nothing to do with whether memory is low or not?
    Of course it does. Say you manage to allocate 5 of the object, then new fails because there's not enough memory, the rest of the pointers will leak unless assigned to the auto_ptr, which it is not guaranteed to do.

    3. I do not understand your "if-statement" sample, can you show the code?
    Mats was making a general statement. Meaning that the behavior could be different just because you added or removed a line of code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by George2 View Post
    Thanks Mats,


    Three more comments,

    1.



    Memory leak has nothing to do with whether memory is low or not?
    Well, not technically, but the only reason it would leak is if your function exits before the auto_ptr's constructor has been called - and this will only happen if new throws, which normally only happens if you're running out of memory (or something has gone wrong with the heap).
    2.



    "Dangling pointer" means, if compiler decides to allocate memory before constructing auto_ptr, and exception in the middle before constructing auto_ptr, no pointer pointed to the allocated memory, so this is what you mean "Dangling pointer"?
    Yes.

    3.



    I do not understand your "if-statement" sample, can you show the code?


    regards,
    George
    Well, the example I was making is if the compiler counts the number of basic-blocks (e.g. if/else branches etc) and then decides which of several code-generation functions to use. So adding an extra if-statement for example, will perhaps push it into "code-generation function 2" instead of "code-generation function 1".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #26
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Two more comments,

    1.

    Quote Originally Posted by Elysia View Post
    Of course it does. Say you manage to allocate 5 of the object, then new fails because there's not enough memory, the rest of the pointers will leak unless assigned to the auto_ptr, which it is not guaranteed to do.
    In my senses, I think when we say exception safety, it means code should not leak memory in any situation based on ensurance of defined behavior of underlying SDK, e.g. we assume delete never throws. It has nothing to do with environment.

    2.

    Any comments to my item 2 in post #23?


    regards,
    George

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    In my senses, I think when we say exception safety, it means code should not leak memory in any situation based on ensurance of defined behavior of underlying SDK, e.g. we assume delete never throws. It has nothing to do with environment.
    I think that sums it up. Exception safety should mean that under no ciruimstance should memory leak or a class be left in a zombie state. Which means avoiding all undefined behaviors,

    2. Any comments to my item 2 in post #23?
    Mats already told you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #28
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mats,


    Quote Originally Posted by matsp View Post
    Well, the example I was making is if the compiler counts the number of basic-blocks (e.g. if/else branches etc) and then decides which of several code-generation functions to use. So adding an extra if-statement for example, will perhaps push it into "code-generation function 2" instead of "code-generation function 1".

    --
    Mats
    You mean compiler has some different internal functions, which for examples handles if-block and non-if block separately?


    regards,
    George

  14. #29
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Elysia,


    Your reply is clear. Looks like we are posting at the same time. :-)

    Quote Originally Posted by Elysia View Post
    I think that sums it up. Exception safety should mean that under no ciruimstance should memory leak or a class be left in a zombie state. Which means avoiding all undefined behaviors,


    Mats already told you.

    regards,
    George

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by George2 View Post
    You mean compiler has some different internal functions, which for examples handles if-block and non-if block separately?
    Maybe. How a compiler works is a mystery to most programmers. You don't know what it does. It's a black box. You put in source on one end and get out object code on the other. Much can happen in-between.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM