Like Tree1Likes

Why do G++ and MSVS handle s1 += " world!" differently?

This is a discussion on Why do G++ and MSVS handle s1 += " world!" differently? within the C++ Programming forums, part of the General Programming Boards category; This thread is related to this thread: Memory leak in Soln8_05.cpp from Ivor Horton's Beginning Visual C++ 2008 book? . ...

  1. #1
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63

    Question Why do G++ and MSVS handle s1 += " world!" differently?

    This thread is related to this thread: Memory leak in Soln8_05.cpp from Ivor Horton's Beginning Visual C++ 2008 book?.

    I basically stuck debug output statements all through a modified copy of the author's solution for one of the chapter exercises in an effort to dissect what constructors, destructors and overloaded functions were being used.

    I compiled Soln8_05.cpp (r11) and got two different results that I did not expect.

    1. CSimpleString(const char* p) was called and I could not find where
    2. I saw that Microsoft Visual Studio 2010 called a copy constructor when I wasn't expecting it


    So, I fired up another VM where I have Ubuntu 10.04 installed and used g++ to compile the code.

    I saved the output from both and this is what I have (first two lines in both files were added manually, as was the first line in the diff file):



    So, that brings me to my questions:

    1. Why do G++ and MSVS handle
      Code:
      s1 += " world!"
      differently?
    2. Why is
      Code:
      CSimpleString(const char* p)
      called as part of resolving
      Code:
      s1 += " world!"
      ?


    Thanks in advance!
    It is better to fail with honor than win by deceit
    - unknown

    My erratic tinkerings:
    http://projects.whyaskwhy.org/

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    1) MSVC is doing it by the book. I am guessing that g++ is applying optimizations here. Try running the MSVC build in Release mode.
    2) Because there is no operator += that takes a const char*. However, there is one operator that takes a const CSimpleString&. So the compiler checks the constructors to see if there is CSimpleString(const char*), and there is. So it invokes that to create a temp object, and then calls operator +=. This is called an implicit conversion.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Am I right to assume that you speak german?

    g++ scheint die Konstruktion/Destruktion des temporären Rückgabeobjektes des Op+ an den Op+= wegzuoptimieren.
    Wegen solcher Dinge (Der Standard erlaubt solche Optimierungen explizit) sollte man beim Debuggen alle Optimierungen abschalten.
    Am Rande: Warum hast Du den Op+= mit Hilfe des Op+ gebaut und nicht andersrum, wie empfohlen?

    Edit: Too late again...
    Last edited by Caligulaminus; 01-14-2012 at 12:04 PM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    Google Translate says:

    g + + seems to be the construction / destruction of the temporary return of the object on the Op Op wegzuoptimieren + + =.
    Because of such things (The standard explicitly allows such optimizations) you should turn off all optimizations when debugging.
    By the way: Why did you build the Op + = with the help of the Op + and not the other way, as recommended?

    Please keep to English on the boards. This is supposed to be an international forum, and you are basically destroying the ability to read what you write for 99% of the people. So unless you have a very special reason for using german, do please refrain from doing so.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by Elysia View Post
    unless you have a very special reason for using german, do please refrain from doing so.
    I did have a reason.
    Looking at deoren's web site I had reason to assume that he might speak german.
    I used german to say something I couldn't have said nearly as clearly in english (since english is not my mother tongue).

    Don't you think that you are overreacting a little?

    PS: Is this site about helping each other or about using the right language?
    You said it yourself: "This is supposed to be an international forum".
    Last edited by Caligulaminus; 01-14-2012 at 12:26 PM.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    No, international as in "english."
    I posted that as both as a reminder (there are people who don't know) and as a message to others who read it should they wish to use a non-english language.
    I'm not overreacting as in "stop doing this or else." It was simply a little reminder. Now that that's done, you can do what you want.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Compilers are allowed to perform optimisation to avoid copy-construction in certain cases.
    As such, the standard says that you're not allowed to rely on side-effects that occur during copy-construction. Outputting any kind of debug message is a side-effect. In both cases, any temporary object that is created is of course destructed.
    Just remove any logging you have and the problem is gone. Yes the compilers optimised differently, but the net result is code that has the same effect. Of course if your code is broken, then no matter what the compiler does, your code might not work.

    There are a lot of differences between compilers and this is very much not one of the differences between compilers that you should worry about. There are far bigger issues that you'll run into eventually if using multiple compilers.

    Edit: Please keep it in English. It's not fair to exclude a large portion of the posters who cannot read other languages. Posting in a different language could mean that you are giving totally wrong advice and without anyone else to understand and refute your post, the originaly poster could be very much sent down the wrong path.
    You might say that it is unfair that this site be for English speakers, but I'm sure you could find a German programming site if you wanted to, and like it or not, C and C++ are English programming languages.
    Last edited by iMalc; 01-14-2012 at 12:52 PM.
    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"

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by Elysia View Post
    No, international as in "english."
    international == english?!?

    This must be a big misunderstanding... Couldn't explain it otherwise.

    Are you japanese?

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by iMalc View Post
    [...] side-effects [...] Just remove any logging you have and the problem is gone.
    Thanks iMalc. I didn't test it but what you say makes (a.) perfect sense and (b.) widened(broadened?) my horizons on the overall matter.
    Last edited by Caligulaminus; 01-14-2012 at 01:04 PM.

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by iMalc View Post
    Edit: Please keep it in English. It's not fair to exclude a large portion of the posters who cannot read other languages. Posting in a different language could mean that you are giving totally wrong advice and without anyone else to understand and refute your post, the originaly poster could be very much sent down the wrong path.
    You might say that it is unfair that this site be for English speakers, but I'm sure you could find a German programming site if you wanted to,
    Please read my answer to Elysia.

    Quote Originally Posted by iMalc View Post
    and like it or not, C and C++ are English programming languages.
    No offence intended. But that's complete nonsense. Like we could only use latin to talk about medicine.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    The reason the forum uses English is because more people speak it and because the forum isn't a personal help service. By that I mean it's important that more than one person, the OP, understands what's being said, for reasons that have already been explained. Personally I don't care since a translation has been provided, as shabby as it is. Except I hope we don't have to rely on people translating to English when there is an Indian on the forum, or someone from the Middle East. We simply do not have the resources to provide help in as many languages as possible like some sort of instruction leaflet.

  12. #12
    Registered User
    Join Date
    Aug 2003
    Posts
    1,197
    Not to mention rule 7:
    Please construct posts with content that furthers the community

    And by excluding a big part of the community (by using a language that most people probably don't know) I think there is little doubt that content does not further the community. English is the standard language to use here.

    Edit:
    iMalc wouldnt C++ technically be a norwegian language, considering the inventor is norwegish
    whiteflags likes this.

  13. #13
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Am I starting a major upheaval here?

    Quote Originally Posted by Shakti View Post
    there is little doubt that content does not further the community.
    Does that mean that deoren is not part of the community?

    As I tried to explain earlier: Trying to help him I told him something in the only language I saw myself able to express myself in without introducing more confusion.

    Given that choice(speaking german or remaining silent): you mean I should stay quiet rather than try and help out?
    Think that is constructive?


    It really feels like people are offended by having to overlook three foreign sentences...

    Quote Originally Posted by Elysia
    This is supposed to be an international forum
    Last edited by Caligulaminus; 01-14-2012 at 02:12 PM.

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    I think Elysia means that this forum is used by people internationally. We use English to understand each other, that's all, and no one said you had to be silent. I don't know why you elected to use German other than you saw the opportunity, even though this person writes fluently. But it's important that people who don't speak German understand.

  15. #15
    Registered User
    Join Date
    Dec 2011
    Posts
    24
    Quote Originally Posted by whiteflags View Post
    even though this person writes fluently.
    But I couldn't in that particular situation.

    (the fact that I obviously didn't manage to make myself clear yet ultimately supports my point here)

    If this is supposed to be an english only forum it should be stated clearly. And I would gladly stay (far) away.

    I use english in forums like this almost always, because it is the lingua franca of our time. But when I hit a point when my english fails my communication needs I use what enables me to express myself. Believe it or not, I even use latin phrases sometimes.

    If that's inacceptable, only native english speakers should be granted full membership...

    I really don't understand this uproar. Not a bit!
    Last edited by Caligulaminus; 01-14-2012 at 02:56 PM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem about handle "double" variable type? or?
    By Mathsniper in forum C Programming
    Replies: 4
    Last Post: 12-31-2006, 09:11 PM
  2. Replies: 2
    Last Post: 04-16-2006, 07:22 PM
  3. Apps that act "differently" in XP SP2
    By Stan100 in forum Tech Board
    Replies: 6
    Last Post: 08-16-2004, 10:38 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21