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

  1. #16
    Registered User deoren's Avatar
    Join Date
    Mar 2003
    Posts
    63
    Quote Originally Posted by Elysia View Post
    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.
    Ah! That was it. After switching from Debug to Release mode the output was the same. I am definitely a newbie.

    Quote Originally Posted by Elysia View Post
    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.
    That is masterfully explained, thank you for going into that level of detail. After I read it I looked at the
    Code:
    s1 += " world!"
    line and I could kick myself for not seeing it.


    Quote Originally Posted by Caligulaminus View Post
    Am I right to assume that you speak german?
    Sorry, no, I don't speak German. At best I might understand snippets of formal French from my high school days, but Google Translate would be my only hope of working with German.

    Just out of curiosity, what gave you the impression I understood German? I'm particularly curious what makes me seem smarter than I am.


    Quote Originally Posted by Caligulaminus View Post
    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.
    Great tip, thank you. I didn't even think about the fact g++ was probably not compiling in debug mode.

    Quote Originally Posted by Caligulaminus View Post
    By the way: Why did you build the Op + = with the help of the Op + and not the other way, as recommended?

    I probably didn't mention it, but what I did was read the exercise directions, solved it myself and then compared my solution against the author's solution.

    My code was ugly.

    It worked, but each overloaded operator+ function was a copy/paste/modify of each other which resulted in a bunch of duplicated code. As I was comparing my code against the author's I found what looked like a memory leak in both my code and the author's. After getting feedback (see thread I mention my first post to this thread) from oogabooga, I went and modified the author's code to include the proposed fix.

    I've was trying to work with the author's code as-is without making modifications to how it worked. This was because I didn't fully understand what I was dealing with yet and I felt that making enhancements that weren't fully obvious (to me) would confuse me further.

    I've spent many years trying to learn C++ (off/on) and all previous times I got hung up on trying different ways to do something instead of just going through the material. I've got one book I'm going through and about 5-6 other resources (mainly other books) I refer to when I run up against something I feel I need to know more about. The concept of pointers is a good example. I used close to all of my resources and still ended up referring to web sites for clarification.


    Quote Originally Posted by iMalc View Post
    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.
    Good to know. I forget whether it was this book or another that when introducing destructors presented an example of a destructor that only printed output the message that it was called.

    I'm using that same approach here so I can see what order the various constructors/functions fire to follow the code flow.

    Thankfully I've been using debug mode all along with MSVS, so it has been both useful and accurate (from what I could tell). I just so happened to test with g++ this time and ran into the discrepancy. Thanks to everyone for making clear why.

    Quote Originally Posted by iMalc View Post
    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.
    Sure, I'm following you there. Now that I realize that g++ was optimizing the code and MSVS was not, I understand why the results were different. Also with Elysia answering my second question, I completely understand what the debug output messages were telling me.


    Quote Originally Posted by iMalc View Post
    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.
    For the most part during my studies I'll be content to treat the compiler output as a black box, provided I understand what the code I'm working with does. It's when I can't read it and understand it that I turn to dissection via debug output to assist.

    I've read of too many cases where the compiler takes the liberty to optimize away user created loops and slower code when it's able to. My exposure to Gentoo years ago when I had to set compilation flags for package builds helped to impress this upon me. Thanks for the warning though, as it's important not to lose focus of the goal.
    It is better to fail with honor than win by deceit
    - unknown

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

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Caligulaminus View Post
    international == english?!?

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

    Are you japanese?
    If you are at an international airport, does everyone speak everyones' native language? I think not. They tend to speak english.
    Am I japanese? Wish that I were, but alas, I am not (nor am I english).
    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.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Caligulaminus View Post
    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!
    Calm down. You did something wrong and you were corrected for it. But so what? That doesn't mean the end of the world. We all make mistakes and things we rather forget all the time, and we learn to live with it.
    Just accept what you've been told and think about it in the future.

    Also consider this:
    If you are in, say, France and want to explain something to someone, but you can't explain it very well in english, do you say it in german?
    The same analogy could be applied here. I would say it is better to try your utmost explaining it in english instead of german and take help from other members to fill in what you couldn't explain and perhaps even help you formulate it better.
    This is an english forum after all, so take it as an opportunity to help you learn some english.
    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.

  4. #19
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    Quote Originally Posted by Elysia View Post
    Calm down.
    Ommmmmmm.
    Quote Originally Posted by Elysia View Post
    You did something wrong
    I did?
    Quote Originally Posted by Elysia View Post
    and you were corrected for it.
    Thanks to you?
    Quote Originally Posted by Elysia View Post
    That doesn't mean the end of the world.
    "Hush now baby, don't you cry."
    Quote Originally Posted by Elysia View Post
    We all make mistakes and things we rather forget all the time
    True words! (Amen!)
    Quote Originally Posted by Elysia View Post
    and we learn to live with it.
    Hmm...
    Quote Originally Posted by Elysia View Post
    Just accept what you've been told and think about it in the future.
    Who, in the world, do you think you are?

    Quote Originally Posted by Elysia View Post
    Also consider this:
    If you are in, say, France and want to explain something to someone, but you can't explain it very well in english, do you say it in german?
    If I couldn't explain it well enough in french I'd try to explain it in what ever language he and I can communicate in best.

    Quote Originally Posted by Elysia View Post
    The same analogy could be applied here. I would say it is better to try your utmost explaining it in english instead of german and take help from other members to fill in what you couldn't explain and perhaps even help you formulate it better.
    That would have meant waiting for someone else to read my mind i.e. remaining silent. (as I said: I saw no way to express my point in english there. So I tried a shortcut - to no avail though. It was a try...)
    Quote Originally Posted by Elysia View Post
    This is an english forum after all
    Really? If that's so I must have overlooked.
    And I will apologise and restrain my urge to help in this forum in the future. Shame on me!!!

    Quote Originally Posted by Elysia View Post
    so take it as an opportunity to help you learn some english.
    m(

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you wish to be a tard, then fine. You are digging your own grave. My advice is: suck it up and live with it. That way, there won't be any further discussions about this topic. Of course, you may do as you please.
    Last edited by Elysia; 01-14-2012 at 05:06 PM.
    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.

  6. #21
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    Quote Originally Posted by deoren
    Sorry, no, I don't speak German. At best I might understand snippets of formal French from my high school days, but Google Translate would be my only hope of working with German.

    Just out of curiosity, what gave you the impression I understood German? I'm particularly curious what makes me seem smarter than I am.
    I looked at the web sites you linked to:
    WhyAskWhy.org Projects
    and
    raphb(.com) - Synclosure

    There are many signs that would suggest a link to the german language. I was wrong - sorry.

    Elysia explained what I wantetd to say even better just minutes before my post was online.

    *Big grin* for the "smarter"-smart.

    beste Grüße...

  7. #22
    Registered User
    Join Date
    Dec 2011
    Posts
    26
    Quote Originally Posted by Elysia View Post
    You are digging your own grave.
    m(

    Quote Originally Posted by Elysia
    My advice is: suck it up and learn with it.
    My advice is: If you are enraged by three foreign sentences in your presence - avoid
    Quote Originally Posted by Elysia View Post
    international forum
    Good night!

  8. #23
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Gees guys way to go overboard

    I'm sorry I added my 2c on the aforementioned matter as once everyone chimes in it adds up to a lot.
    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"

  9. #24
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Too far off topic to salvage - closed.
    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.

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, 10: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, 08: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, 07:59 AM

Tags for this Thread