Thread: why Visual Studio can not optimize the initialization code?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    why Visual Studio can not optimize the initialization code?

    Hello everyone,


    Why Visual Studio compiler can not optimize in this case? I think this case is almost the same as sample 1, why compiler can optimize sample 1 but can not optimze sample 2?

    (sample 2, http://msdn2.microsoft.com/en-us/lib...57(vs.80).aspx)

    Code:
    #include <stdio.h>
    class A {
      public:
        A() {printf ("A: I am in constructor\n");i = 1;}
        ~A() { printf ("A: I am in destructor\n"); i = 0;}
        A(const A& a) {printf ("A: I am in copy constructor\n"); i = a.i;}
        int i, x, w;
    };
     class B {
      public:
        A a;
        B()  { printf ("B: I am in constructor\n");}
        ~B() { printf ("B: I am in destructor\n");}
        B(const B& b) { printf ("B: I am in copy constructor\n");}
    };
    A MyMethod()
    {
        B* b = new B();
        A a = b->a;
        delete b;
        return (a);
    }
    int main()
    {
        A a;
        a = MyMethod();
    }

    thanks in advance,
    George

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Have you turned compiler optimizations on? Apparently VC++ 2005 won't use NRVO (here) if optimizations are off.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    AFAIK, a lot of the optimisations are disabled in the express (ie free) version.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    AFAIK, a lot of the optimisations are disabled in the express (ie free) version.
    Didn't Microsoft tout the express version as having the optimising compiler?
    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

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Didn't Microsoft tout the express version as having the optimising compiler?
    I'm pretty sure it's the same compiler. I have no idea what the answer is to the above question, but I'm pretty sure that it's likely to be "interpreting the linked page", rather than what is supported and not by the compiler. Also, different versions of compilers may be more or less good at certain optimizations, and a newer compiler may have a bug-fix that an older one didn't have, which also means that some cases of certain optimizations are not done [because under some circumstances, the optimisation is unsafe].

    --
    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.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I used the free VC++ 2005 compiler with Code::Blocks and I could produce both the optimized and non-optimized output by changing project settings.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by laserlight View Post
    Didn't Microsoft tout the express version as having the optimising compiler?
    Yes VS2005 Express comes with the full optimising compiler.
    Otherwise my programs wouldn't be able to match or exceed the speed they ran at when compiled with an older full version MS compiler, with all optimisations on
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Problem Compiling Program
    By wco5002 in forum C++ Programming
    Replies: 13
    Last Post: 11-06-2007, 12:56 PM
  3. Visual Studio quick code
    By letdoit in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2007, 09:52 PM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM