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. General Programming
  3. C / C++ / MFC
  4. I find an interesting and confusing code..have a look.

I find an interesting and confusing code..have a look.

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 6 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.
  • J Offline
    J Offline
    JackPuppy
    wrote on last edited by
    #1

    fun(){
    int temp;
    throw temp;
    }

    main(){
    void fun1(){
    try{
    int a;
    fun();
    int* p=null;
    p=new int[40]

    }
    catch(int temp){
    delete[] p;

    }
    }
    }

    What will hapen?

    L 1 Reply Last reply
    0
    • J JackPuppy

      fun(){
      int temp;
      throw temp;
      }

      main(){
      void fun1(){
      try{
      int a;
      fun();
      int* p=null;
      p=new int[40]

      }
      catch(int temp){
      delete[] p;

      }
      }
      }

      What will hapen?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      JackPuppy wrote:

      What will hapen?

      My guess is one or more derogatory replies since you posted code that does not compile.

      J 1 Reply Last reply
      0
      • L led mike

        JackPuppy wrote:

        What will hapen?

        My guess is one or more derogatory replies since you posted code that does not compile.

        J Offline
        J Offline
        JackPuppy
        wrote on last edited by
        #3

        fun(){
        int temp;
        throw temp;
        }

        void fun1(){
        try{
        int a;
        fun();
        int* p=null;
        p=new int[40]

        }
        catch(int temp){
        delete[] p;

        }
        }

        main(){
        fun1();
        }

        I write some code that supposed to make you understand, rather than make it perfect without any mistakes. Alas, just edit a bit can make it compliable.

        D L CPalliniC 3 Replies Last reply
        0
        • J JackPuppy

          fun(){
          int temp;
          throw temp;
          }

          void fun1(){
          try{
          int a;
          fun();
          int* p=null;
          p=new int[40]

          }
          catch(int temp){
          delete[] p;

          }
          }

          main(){
          fun1();
          }

          I write some code that supposed to make you understand, rather than make it perfect without any mistakes. Alas, just edit a bit can make it compliable.

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          If you'll clean up the compiler errors, you should be able to answer your question. :|

          "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          J 1 Reply Last reply
          0
          • J JackPuppy

            fun(){
            int temp;
            throw temp;
            }

            void fun1(){
            try{
            int a;
            fun();
            int* p=null;
            p=new int[40]

            }
            catch(int temp){
            delete[] p;

            }
            }

            main(){
            fun1();
            }

            I write some code that supposed to make you understand, rather than make it perfect without any mistakes. Alas, just edit a bit can make it compliable.

            L Offline
            L Offline
            led mike
            wrote on last edited by
            #5

            JackPuppy wrote:

            I write some code that supposed to make you understand

            I can't buy into that concept.

            JackPuppy wrote:

            Alas, just edit a bit

            No. Alas I am bored with you now.

            1 Reply Last reply
            0
            • D David Crow

              If you'll clean up the compiler errors, you should be able to answer your question. :|

              "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              J Offline
              J Offline
              JackPuppy
              wrote on last edited by
              #6

              lol.... you make me largh! Please look into this code because it's not that simple and it contain some deadly fallacies in it! And not just one, many! My confusion is that I don't find a way out to solve it.

              D CPalliniC 2 Replies Last reply
              0
              • J JackPuppy

                fun(){
                int temp;
                throw temp;
                }

                void fun1(){
                try{
                int a;
                fun();
                int* p=null;
                p=new int[40]

                }
                catch(int temp){
                delete[] p;

                }
                }

                main(){
                fun1();
                }

                I write some code that supposed to make you understand, rather than make it perfect without any mistakes. Alas, just edit a bit can make it compliable.

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                What exactly do you find interesting, in the above code? :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                J 1 Reply Last reply
                0
                • J JackPuppy

                  lol.... you make me largh! Please look into this code because it's not that simple and it contain some deadly fallacies in it! And not just one, many! My confusion is that I don't find a way out to solve it.

                  D Offline
                  D Offline
                  David Crow
                  wrote on last edited by
                  #8

                  So has your question changed from "What will hapen?" to "How can I fix it?"

                  "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  1 Reply Last reply
                  0
                  • J JackPuppy

                    lol.... you make me largh! Please look into this code because it's not that simple and it contain some deadly fallacies in it! And not just one, many! My confusion is that I don't find a way out to solve it.

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    JackPuppy wrote:

                    it contain some deadly fallacies in it!

                    Possibly yes.

                    JackPuppy wrote:

                    And not just one, many!

                    Again, possibly yes.

                    JackPuppy wrote:

                    My confusion is that I don't find a way out to solve it.

                    No doubt about. :-D BTW: just kidding.

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    In testa che avete, signor di Ceprano?

                    1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      What exactly do you find interesting, in the above code? :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      J Offline
                      J Offline
                      JackPuppy
                      wrote on last edited by
                      #10

                      OK,let me expain:

                      fun(){
                      int temp;
                      throw temp;
                      }

                      void fun1(){
                      try{
                      int a;
                      fun();
                      int* p=null;
                      p=new int[40]

                      }
                      catch(int temp){
                      delete[] p;

                      }
                      }

                      main(){
                      fun1();
                      }

                      when main() call fun1(), a,p are push into stack without initialize. fun()throw an exception so the try-catch block detect it. and the program jump into catch{} and in the catch{} it delete[] p; wait for a sec, p wasn't initialize yet so this is wrong; however how can we determine whether p was initialize? because when p was pushed into stack it contain some data and become a dangling pointer. by convention, we will write catch{ if(p!=NULL) delete[] p; } but it wasn't the case at this circumstance because p is not null at the very begining! of cause you can write like this: int* p=null; fun(); p=new int[40]; so p is null before calling fun(). so It's OK now. I find this interesting because I have to pay so much programing knowledge to explain this small mistake, the knowledge concerned are : stack, heap management, PE format, linker, loader, lib.

                      B 1 Reply Last reply
                      0
                      • J JackPuppy

                        OK,let me expain:

                        fun(){
                        int temp;
                        throw temp;
                        }

                        void fun1(){
                        try{
                        int a;
                        fun();
                        int* p=null;
                        p=new int[40]

                        }
                        catch(int temp){
                        delete[] p;

                        }
                        }

                        main(){
                        fun1();
                        }

                        when main() call fun1(), a,p are push into stack without initialize. fun()throw an exception so the try-catch block detect it. and the program jump into catch{} and in the catch{} it delete[] p; wait for a sec, p wasn't initialize yet so this is wrong; however how can we determine whether p was initialize? because when p was pushed into stack it contain some data and become a dangling pointer. by convention, we will write catch{ if(p!=NULL) delete[] p; } but it wasn't the case at this circumstance because p is not null at the very begining! of cause you can write like this: int* p=null; fun(); p=new int[40]; so p is null before calling fun(). so It's OK now. I find this interesting because I have to pay so much programing knowledge to explain this small mistake, the knowledge concerned are : stack, heap management, PE format, linker, loader, lib.

                        B Offline
                        B Offline
                        bulg
                        wrote on last edited by
                        #11

                        I believe the forum you want is called "Subtle Bugs"

                        J S 2 Replies Last reply
                        0
                        • B bulg

                          I believe the forum you want is called "Subtle Bugs"

                          J Offline
                          J Offline
                          JackPuppy
                          wrote on last edited by
                          #12

                          thanks for advising. i didn't know it.

                          B 1 Reply Last reply
                          0
                          • J JackPuppy

                            thanks for advising. i didn't know it.

                            B Offline
                            B Offline
                            bulg
                            wrote on last edited by
                            #13

                            no problem, I'm sure you'll get a much better reception there

                            1 Reply Last reply
                            0
                            • B bulg

                              I believe the forum you want is called "Subtle Bugs"

                              S Offline
                              S Offline
                              Stuart Dootson
                              wrote on last edited by
                              #14

                              Apart from the fact his bug ain't subtle...

                              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                              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