Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. char x = 0;

char x = 0;

Scheduled Pinned Locked Moved Clever Code
performancequestionlounge
17 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P PJ Arends

    Given the following code, what would you expect the output to be?

    #include <string>
    #include <iostream>
    using namespace std;

    char a = 'a';
    char b = 'b';
    char res1 = 0;
    char c = 'c';
    char res2 = 0;
    char d = 'd';
    char res3 = 0;

    int main(int argc, char* argv[])
    {
    string s = &a;
    cout << s << endl;
    s = &b;
    cout << s << endl;
    s = &c;
    cout << s << endl;
    s = &d;
    cout << s << endl;

    return 0;
    

    }

    The output I had expected was:

    ab
    b
    c
    d

    But that is not what I got. The output I ended up with is:

    abcd<random garbage>
    bcd<random garbage>
    cd<random garbage>
    d<random garbage>

    It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


    It's not rocket surgery!

    Within you lies the power for good, use it!!!

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #3

    Yeah, don't rely on side effects like that.

    S 1 Reply Last reply
    0
    • P PJ Arends

      Given the following code, what would you expect the output to be?

      #include <string>
      #include <iostream>
      using namespace std;

      char a = 'a';
      char b = 'b';
      char res1 = 0;
      char c = 'c';
      char res2 = 0;
      char d = 'd';
      char res3 = 0;

      int main(int argc, char* argv[])
      {
      string s = &a;
      cout << s << endl;
      s = &b;
      cout << s << endl;
      s = &c;
      cout << s << endl;
      s = &d;
      cout << s << endl;

      return 0;
      

      }

      The output I had expected was:

      ab
      b
      c
      d

      But that is not what I got. The output I ended up with is:

      abcd<random garbage>
      bcd<random garbage>
      cd<random garbage>
      d<random garbage>

      It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


      It's not rocket surgery!

      Within you lies the power for good, use it!!!

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #4

      Honestly, I don't think this qualifies as a "subtle bug". My first thought after seeing this was "this should blow up right in your face and you deserve it!" ;P Seeing something like this in production code would be a good reason for a firm open palm slap to the back of the programmer's head who wrote this code...

      Regards, mav -- Black holes are the places where God divided by 0...

      P 1 Reply Last reply
      0
      • M mav northwind

        Honestly, I don't think this qualifies as a "subtle bug". My first thought after seeing this was "this should blow up right in your face and you deserve it!" ;P Seeing something like this in production code would be a good reason for a firm open palm slap to the back of the programmer's head who wrote this code...

        Regards, mav -- Black holes are the places where God divided by 0...

        P Offline
        P Offline
        PJ Arends
        wrote on last edited by
        #5

        mav.northwind wrote:

        My first thought after seeing this was "this should blow up right in your face and you deserve it!"

        Yeah, normally I would never use this, but it is for my entry in this[^] competition. So the worse the better. The result is just not what I had expected. I have developed another work-around though.


        Darker than a black steer's tookus on a moonless praire night

        Within you lies the power for good, use it!!!

        P S 2 Replies Last reply
        0
        • P PJ Arends

          mav.northwind wrote:

          My first thought after seeing this was "this should blow up right in your face and you deserve it!"

          Yeah, normally I would never use this, but it is for my entry in this[^] competition. So the worse the better. The result is just not what I had expected. I have developed another work-around though.


          Darker than a black steer's tookus on a moonless praire night

          Within you lies the power for good, use it!!!

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #6

          Oh, well that's alright then.

          1 Reply Last reply
          0
          • P PJ Arends

            Given the following code, what would you expect the output to be?

            #include <string>
            #include <iostream>
            using namespace std;

            char a = 'a';
            char b = 'b';
            char res1 = 0;
            char c = 'c';
            char res2 = 0;
            char d = 'd';
            char res3 = 0;

            int main(int argc, char* argv[])
            {
            string s = &a;
            cout << s << endl;
            s = &b;
            cout << s << endl;
            s = &c;
            cout << s << endl;
            s = &d;
            cout << s << endl;

            return 0;
            

            }

            The output I had expected was:

            ab
            b
            c
            d

            But that is not what I got. The output I ended up with is:

            abcd<random garbage>
            bcd<random garbage>
            cd<random garbage>
            d<random garbage>

            It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


            It's not rocket surgery!

            Within you lies the power for good, use it!!!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #7

            That's usually what you get for moving from assembly to C++.:) I made a booboo myself: Booboo

            1 Reply Last reply
            0
            • P PJ Arends

              Given the following code, what would you expect the output to be?

              #include <string>
              #include <iostream>
              using namespace std;

              char a = 'a';
              char b = 'b';
              char res1 = 0;
              char c = 'c';
              char res2 = 0;
              char d = 'd';
              char res3 = 0;

              int main(int argc, char* argv[])
              {
              string s = &a;
              cout << s << endl;
              s = &b;
              cout << s << endl;
              s = &c;
              cout << s << endl;
              s = &d;
              cout << s << endl;

              return 0;
              

              }

              The output I had expected was:

              ab
              b
              c
              d

              But that is not what I got. The output I ended up with is:

              abcd<random garbage>
              bcd<random garbage>
              cd<random garbage>
              d<random garbage>

              It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


              It's not rocket surgery!

              Within you lies the power for good, use it!!!

              S Offline
              S Offline
              scott_hackett
              wrote on last edited by
              #8

              I bet the chars got stored sequentially in memory, like this... abcd????? s then points to the 'a' memory location and cout prints "abcd" and whatever memory follows it until it hits the first 0 (what it thinks is the null terminator). s is then moved to point to the 'b' memory location and prints "bcd" and whatever memory follows it and so on and so forth. Interesting!

              P 1 Reply Last reply
              0
              • P PJ Arends

                Given the following code, what would you expect the output to be?

                #include <string>
                #include <iostream>
                using namespace std;

                char a = 'a';
                char b = 'b';
                char res1 = 0;
                char c = 'c';
                char res2 = 0;
                char d = 'd';
                char res3 = 0;

                int main(int argc, char* argv[])
                {
                string s = &a;
                cout << s << endl;
                s = &b;
                cout << s << endl;
                s = &c;
                cout << s << endl;
                s = &d;
                cout << s << endl;

                return 0;
                

                }

                The output I had expected was:

                ab
                b
                c
                d

                But that is not what I got. The output I ended up with is:

                abcd<random garbage>
                bcd<random garbage>
                cd<random garbage>
                d<random garbage>

                It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


                It's not rocket surgery!

                Within you lies the power for good, use it!!!

                D Offline
                D Offline
                Dan Neely
                wrote on last edited by
                #9

                I don't know if you're allowed to submit a full project or just the .h/.cpp files, but if the former you might try turning off all optimization to see if you can stop the compiler from outsmarting your designed WTFs.

                -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                P 1 Reply Last reply
                0
                • D Dan Neely

                  I don't know if you're allowed to submit a full project or just the .h/.cpp files, but if the former you might try turning off all optimization to see if you can stop the compiler from outsmarting your designed WTFs.

                  -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                  P Offline
                  P Offline
                  PJ Arends
                  wrote on last edited by
                  #10

                  dan neely wrote:

                  you might try turning off all optimization

                  That was the first thing I tried, did not help:sigh: I already have another, possibly even better work-around:cool:


                  It's not rocket surgery!

                  Within you lies the power for good, use it!!!

                  1 Reply Last reply
                  0
                  • S scott_hackett

                    I bet the chars got stored sequentially in memory, like this... abcd????? s then points to the 'a' memory location and cout prints "abcd" and whatever memory follows it until it hits the first 0 (what it thinks is the null terminator). s is then moved to point to the 'b' memory location and prints "bcd" and whatever memory follows it and so on and so forth. Interesting!

                    P Offline
                    P Offline
                    PJ Arends
                    wrote on last edited by
                    #11

                    Yes, that is exactly what happens. The only problem is that uninitialized variables and variables initialized to zero do not follow the same behavior.


                    It's not rocket surgery!

                    Within you lies the power for good, use it!!!

                    1 Reply Last reply
                    0
                    • P PJ Arends

                      mav.northwind wrote:

                      My first thought after seeing this was "this should blow up right in your face and you deserve it!"

                      Yeah, normally I would never use this, but it is for my entry in this[^] competition. So the worse the better. The result is just not what I had expected. I have developed another work-around though.


                      Darker than a black steer's tookus on a moonless praire night

                      Within you lies the power for good, use it!!!

                      S Offline
                      S Offline
                      Sebastian Schneider
                      wrote on last edited by
                      #12

                      Then why don't you just assign something else to them first, then assign the 0, just to "work around those stupid compiler limitations"?

                      Cheers, Sebastian -- "If it was two men, the non-driver would have challenged the driver to simply crash through the gates. The macho image thing, you know." - Marc Clifton

                      P 1 Reply Last reply
                      0
                      • S Sebastian Schneider

                        Then why don't you just assign something else to them first, then assign the 0, just to "work around those stupid compiler limitations"?

                        Cheers, Sebastian -- "If it was two men, the non-driver would have challenged the driver to simply crash through the gates. The macho image thing, you know." - Marc Clifton

                        P Offline
                        P Offline
                        PJ Arends
                        wrote on last edited by
                        #13

                        That just makes too much sense.


                        Why do things that only happen to stupid people keep happening to me?

                        Within you lies the power for good, use it!!!

                        1 Reply Last reply
                        0
                        • P PJ Arends

                          Given the following code, what would you expect the output to be?

                          #include <string>
                          #include <iostream>
                          using namespace std;

                          char a = 'a';
                          char b = 'b';
                          char res1 = 0;
                          char c = 'c';
                          char res2 = 0;
                          char d = 'd';
                          char res3 = 0;

                          int main(int argc, char* argv[])
                          {
                          string s = &a;
                          cout << s << endl;
                          s = &b;
                          cout << s << endl;
                          s = &c;
                          cout << s << endl;
                          s = &d;
                          cout << s << endl;

                          return 0;
                          

                          }

                          The output I had expected was:

                          ab
                          b
                          c
                          d

                          But that is not what I got. The output I ended up with is:

                          abcd<random garbage>
                          bcd<random garbage>
                          cd<random garbage>
                          d<random garbage>

                          It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


                          It's not rocket surgery!

                          Within you lies the power for good, use it!!!

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          My initial response ie "EEK!" - this code, if it did work, would be 'getting away with it' except that you didn't. I would kick it ould in a code review (Yes, I do QA!). Elaine (harsh fluffy tigress)

                          The tigress is here :-D

                          1 Reply Last reply
                          0
                          • P PJ Arends

                            Given the following code, what would you expect the output to be?

                            #include <string>
                            #include <iostream>
                            using namespace std;

                            char a = 'a';
                            char b = 'b';
                            char res1 = 0;
                            char c = 'c';
                            char res2 = 0;
                            char d = 'd';
                            char res3 = 0;

                            int main(int argc, char* argv[])
                            {
                            string s = &a;
                            cout << s << endl;
                            s = &b;
                            cout << s << endl;
                            s = &c;
                            cout << s << endl;
                            s = &d;
                            cout << s << endl;

                            return 0;
                            

                            }

                            The output I had expected was:

                            ab
                            b
                            c
                            d

                            But that is not what I got. The output I ended up with is:

                            abcd<random garbage>
                            bcd<random garbage>
                            cd<random garbage>
                            d<random garbage>

                            It turns out that the NULL values I stored in res1 etc. were not being stored in memory in the exact sequence that I had declared them. They were instead being stored in some other place in memory.


                            It's not rocket surgery!

                            Within you lies the power for good, use it!!!

                            J Offline
                            J Offline
                            John Rossini
                            wrote on last edited by
                            #15

                            if you put all the characters into a struct, and put #pragma pack before the struct, I think you'll get what you were expecting.

                            1 Reply Last reply
                            0
                            • D Dan Neely

                              Since you never used res* it's possible the compiler optimized them out of existence entirely. If you use by never modify them it's possible they were optimized into a single memory location.

                              -- You have to explain to them [VB coders] what you mean by "typed". their first response is likely to be something like, "Of course my code is typed. Do you think i magically project it onto the screen with the power of my mind?" --- John Simmons / outlaw programmer

                              S Offline
                              S Offline
                              Sathesh Sakthivel
                              wrote on last edited by
                              #16

                              :)

                              Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

                              1 Reply Last reply
                              0
                              • P PIEBALDconsult

                                Yeah, don't rely on side effects like that.

                                S Offline
                                S Offline
                                Sathesh Sakthivel
                                wrote on last edited by
                                #17

                                :laugh::laugh:

                                Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups