Code getting Crashed( C++)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pallav singh

    Code getting Crashed( C++)

    #include<iostre am.h>
    #include<memory .h>
    #include<string .h>


    // product
    class Pizza
    {
    private :
    std::string Topping;
    std::string Sauce;
    std::string Dough;

    public :
    Pizza(){}
    ~Pizza(){}

    void SetTopping(cons t std::string t) { Topping = t; }
    void SetSauce(const std::string s){ Sauce = s; }
    void SetDough(const std::string d){ Dough = d; }

    void ShowPizza()
    {
    std::cout <<"What a Pizza"<<std::en dl
    <<"Topping is "<<Topping
    <<"Sauce is "<<Sauce
    <<"Dough is "<<Dough
    <<"! ! !"<<'\n';
    }

    };


    // Abstract Builder
    class PizzaBuilder
    {
    protected :
    std::auto_ptr<P izzapizza;

    public :
    PizzaBuilder(){ }
    virtual ~PizzaBuilder() {}

    std::auto_ptr<P izzaGetPizza(){ return pizza;}

    virtual void BuildTopping() = 0;
    virtual void BuildSauce() = 0;
    virtual void BuildDough() = 0;

    std::auto_ptr<P izzaCreateNew_P izzaProduct()
    { pizza.reset(new Pizza()); }

    /*********** code getting crashed here ***************/

    };

    // concrete Pizza Builder 1
    class Hawian_PizzaBui lder : public PizzaBuilder
    {

    public :
    Hawian_PizzaBui lder():PizzaBui lder(){ }
    ~Hawian_PizzaBu ilder(){ }
    void BuildTopping() { pizza->SetTopping(" Hawian_PizzaBui lder
    Topping ");}
    void BuildSauce() { pizza->SetSauce("Hawi an_PizzaBuilder
    Sauce ");}
    void BuildDough() { pizza->SetDough("Hawi an_PizzaBuilder
    Dough ");}

    };

    // concrete Pizza Builder 2
    class Spicy_PizzaBuil der : public PizzaBuilder
    {

    public :
    Spicy_PizzaBuil der():PizzaBuil der(){}
    ~Spicy_PizzaBui lder(){}
    void BuildTopping() { pizza->SetTopping("Sp icy_PizzaBuilde r Topping
    "); }
    void BuildSauce() { pizza->SetSauce("Spic y_PizzaBuilder
    Sauce "); }
    void BuildDough() { pizza->SetDough("Spic y_PizzaBuilder
    Dough "); }

    };


    // Director
    class waiter
    {
    private :
    PizzaBuilder * pizza_builder;
    public :
    waiter(): pizza_builder(N ULL){}
    ~waiter(){}

    void SetPizzaBuilder (PizzaBuilder * b){ pizza_builder = b ;}
    std::auto_ptr<P izzaGetPizza(){ return pizza_builder-
    >GetPizza();}
    void ConstructPizza( )
    {
    pizza_builder->CreateNew_Pizz aProduct();
    pizza_builder->BuildDough() ;
    pizza_builder->BuildSauce() ;
    pizza_builder->BuildTopping() ;
    }
    };


    // user
    int main()
    {

    waiter waiter_1;

    Spicy_PizzaBuil der spicyPizzaBuild er;
    waiter_1.SetPiz zaBuilder(&spic yPizzaBuilder);

    waiter_1.Constr uctPizza(); // code getting Crashed here

    std::auto_ptr<P izzapizza = waiter_1.GetPiz za();
    pizza->ShowPizza();

    return 0;
    }
  • Salt_Peter

    #2
    Re: Code getting Crashed( C++)

    On Aug 7, 5:39 am, Pallav singh <singh.pal...@g mail.comwrote:
    #include<iostre am.h>
    #include<memory .h>
    #include<string .h>
    #include<iostre am>
    #include<memory >
    #include<string >

    if your teacher or book doesn't require the above, they shouldn't be
    teaching or be read.
    Welcome to the 21st century.
    >
    // product
    class Pizza
    {
    private :
    std::string Topping;
    std::string Sauce;
    std::string Dough;
    >
    public :
    Pizza(){}
    ~Pizza(){}
    >
    void SetTopping(cons t std::string t) { Topping = t; }
    void SetSauce(const std::string s){ Sauce = s; }
    void SetDough(const std::string d){ Dough = d; }
    >
    void ShowPizza()
    {
    std::cout <<"What a Pizza"<<std::en dl
    <<"Topping is "<<Topping
    <<"Sauce is "<<Sauce
    <<"Dough is "<<Dough
    <<"! ! !"<<'\n';
    }
    >
    };
    >
    // Abstract Builder
    class PizzaBuilder
    {
    protected :
    std::auto_ptr<P izzapizza;
    >
    public :
    PizzaBuilder(){ }
    virtual ~PizzaBuilder() {}
    >
    std::auto_ptr<P izzaGetPizza(){ return pizza;}
    >
    virtual void BuildTopping() = 0;
    virtual void BuildSauce() = 0;
    virtual void BuildDough() = 0;
    >
    std::auto_ptr<P izzaCreateNew_P izzaProduct()
    { pizza.reset(new Pizza()); }
    aren't you reseting the private member auto_ptr?
    why does the function signature indicate a return type and yet no
    return is supplied?
    try:
    void CreateNew_Pizza Product() { ... }
    >
    /*********** code getting crashed here ***************/
    Your compiler should have detected the above anomaly, perhaps you need
    to take a look at the compiler options. Thats off topic here.
    >
    };
    >
    // concrete Pizza Builder 1
    class Hawian_PizzaBui lder : public PizzaBuilder
    {
    >
    public :
    Hawian_PizzaBui lder():PizzaBui lder(){ }
    ~Hawian_PizzaBu ilder(){ }
    void BuildTopping() { pizza->SetTopping(" Hawian_PizzaBui lder
    Topping ");}
    void BuildSauce() { pizza->SetSauce("Hawi an_PizzaBuilder
    Sauce ");}
    void BuildDough() { pizza->SetDough("Hawi an_PizzaBuilder
    Dough ");}
    >
    };
    >
    // concrete Pizza Builder 2
    class Spicy_PizzaBuil der : public PizzaBuilder
    {
    >
    public :
    Spicy_PizzaBuil der():PizzaBuil der(){}
    ~Spicy_PizzaBui lder(){}
    void BuildTopping() { pizza->SetTopping("Sp icy_PizzaBuilde r Topping
    "); }
    void BuildSauce() { pizza->SetSauce("Spic y_PizzaBuilder
    Sauce "); }
    void BuildDough() { pizza->SetDough("Spic y_PizzaBuilder
    Dough "); }
    >
    };
    >
    // Director
    class waiter
    {
    private :
    PizzaBuilder * pizza_builder;
    public :
    waiter(): pizza_builder(N ULL){}
    ~waiter(){}
    >
    void SetPizzaBuilder (PizzaBuilder * b){ pizza_builder = b ;}
    std::auto_ptr<P izzaGetPizza(){ return pizza_builder-
    >
    GetPizza();}
    >
    void ConstructPizza( )
    {
    pizza_builder->CreateNew_Pizz aProduct();
    pizza_builder->BuildDough() ;
    pizza_builder->BuildSauce() ;
    pizza_builder->BuildTopping() ;
    }
    >
    };
    >
    // user
    int main()
    {
    >
    waiter waiter_1;
    >
    Spicy_PizzaBuil der spicyPizzaBuild er;
    waiter_1.SetPiz zaBuilder(&spic yPizzaBuilder);
    >
    waiter_1.Constr uctPizza(); // code getting Crashed here
    >
    std::auto_ptr<P izzapizza = waiter_1.GetPiz za();
    pizza->ShowPizza();
    >
    return 0;
    >
    }

    Comment

    • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

      #3
      Re: Code getting Crashed( C++)

      On 2008-08-07 11:39, Pallav singh wrote:
      #include<iostre am.h>
      #include<memory .h>
      #include<string .h>
      Drop the .h, it is pre-standard C++ (10 years old).

      // Abstract Builder
      class PizzaBuilder
      {
      protected :
      std::auto_ptr<P izzapizza;
      >
      public :
      PizzaBuilder(){ }
      virtual ~PizzaBuilder() {}
      >
      std::auto_ptr<P izzaGetPizza(){ return pizza;}
      >
      virtual void BuildTopping() = 0;
      virtual void BuildSauce() = 0;
      virtual void BuildDough() = 0;
      >
      std::auto_ptr<P izzaCreateNew_P izzaProduct()
      { pizza.reset(new Pizza()); }
      Actually I'm surprised your code crashes, because it should not even
      compile. In the above function you forgot to return a value. It might be
      possible (though it should not happen) that your compiler returns some
      default value which is somehow converted to std::auto_ptr<P izzawhich
      causes a crash when CreateNew_Pizza Product is called in ConstructPizza.

      --
      Erik Wikström

      Comment

      • Pallav singh

        #4
        Re: Code getting Crashed( C++)

        On Aug 7, 4:00 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
        On 2008-08-07 11:39, Pallav singh wrote:
        >
        #include<iostre am.h>
        #include<memory .h>
        #include<string .h>
        >
        Drop the .h, it is pre-standard C++ (10 years old).
        >
        >
        >
        // Abstract Builder
        class PizzaBuilder
        {
        protected :
        std::auto_ptr<P izzapizza;
        >
        public :
        PizzaBuilder(){ }
        virtual ~PizzaBuilder() {}
        >
        std::auto_ptr<P izzaGetPizza(){ return pizza;}
        >
        virtual void BuildTopping() = 0;
        virtual void BuildSauce() = 0;
        virtual void BuildDough() = 0;
        >
        std::auto_ptr<P izzaCreateNew_P izzaProduct()
        { pizza.reset(new Pizza()); }
        >
        Actually I'm surprised your code crashes, because it should not even
        compile. In the above function you forgot to return a value. It might be
        possible (though it should not happen) that your compiler returns some
        default value which is somehow converted to std::auto_ptr<P izzawhich
        causes a crash when CreateNew_Pizza Product is called in ConstructPizza.
        >
        --
        Erik Wikström
        Thanks
        Pallav

        Comment

        • Old Wolf

          #5
          Re: Code getting Crashed( C++)

          On Aug 7, 11:00 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
          On 2008-08-07 11:39, Pallav singh wrote:
          std::auto_ptr<P izzaCreateNew_P izzaProduct()
          { pizza.reset(new Pizza()); }
          >
          Actually I'm surprised your code crashes, because it should not even
          compile. In the above function you forgot to return a value.
          The above code need not generate a compiler
          diagnostic. Furthermore, no code 'should not
          compile' unless it contains a #error directive.

          The behaviour is undefined if the function is actually called.


          Comment

          • James Kanze

            #6
            Re: Code getting Crashed( C++)

            On Aug 7, 1:00 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
            On 2008-08-07 11:39, Pallav singh wrote:
            std::auto_ptr<P izzaCreateNew_P izzaProduct()
            { pizza.reset(new Pizza()); }
            Actually I'm surprised your code crashes, because it should
            not even compile. In the above function you forgot to return a
            value.
            Which is undefined behavior, and doesn't require a diagnostic.
            It would be nice if the compiler issued a warning, but the code
            is perfectly legal if a) the function is never actually called,
            or b) the expression "new Pizza()" always threw an exception, or
            failed to return normally for some other reason.

            This is even useful, in some admittedly rare cases, e.g.:

            SomeType
            Derived::f()
            {
            // The base class imposes pre-conditions which can
            // never be met in this derived class, so...
            assert( 0, "pre-conditions not met" ) ;
            abort() ;
            }
            It might be possible (though it should not happen) that your
            compiler returns some default value which is somehow converted
            to std::auto_ptr<P izzawhich causes a crash when
            CreateNew_Pizza Product is called in ConstructPizza.
            More likely, his compiler supposes that no code flow will ever
            result in falling off the end, and doesn't do anything. So the
            calling code ends up using uninitialized memory as an auto_ptr.
            (Note that even if the calling code doesn't use the return value
            explicitly, it will call the destructor on it.)

            --
            James Kanze (GABI Software) email:james.kan ze@gmail.com
            Conseils en informatique orientée objet/
            Beratung in objektorientier ter Datenverarbeitu ng
            9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

            Comment

            • Pete Becker

              #7
              Re: Code getting Crashed( C++)

              On 2008-08-07 21:34:45 -0400, Old Wolf <oldwolf@inspir e.net.nzsaid:
              >
              Furthermore, no code 'should not
              compile' unless it contains a #error directive.
              >
              That's the rule in C. In C++ a #error directive makes the code
              ill-formed, so only requires a diagnostic.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              • Stuart Golodetz

                #8
                Re: Code getting Crashed( C++)

                "James Kanze" <james.kanze@gm ail.comwrote in message news:66251dc4-da69-401e-8601-ed735986a6f1@m7 3g2000hsh.googl egroups.com...
                On Aug 7, 1:00 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
                On 2008-08-07 11:39, Pallav singh wrote:
                >> std::auto_ptr<P izzaCreateNew_P izzaProduct()
                { pizza.reset(new Pizza()); }
                >Actually I'm surprised your code crashes, because it should
                >not even compile. In the above function you forgot to return a
                >value.
                Which is undefined behavior, and doesn't require a diagnostic.
                It would be nice if the compiler issued a warning, but the code
                is perfectly legal if a) the function is never actually called,
                or b) the expression "new Pizza()" always threw an exception, or
                failed to return normally for some other reason.

                This is even useful, in some admittedly rare cases, e.g.:

                SomeType
                Derived::f()
                {
                // The base class imposes pre-conditions which can
                // never be met in this derived class, so...
                assert( 0, "pre-conditions not met" ) ;
                abort() ;
                }
                From a purist perspective, should Derived really inherit from the base class in question in this case? (I realise that there can sometimes be occasions when pragmatism is necessary - just wondering whether this is in principle best avoided?) I remember reading somewhere (and it makes sense to me) that an overridden function should have preconditions which are no stronger than than those of the base function it overrides (i.e. it accepts anything the base function would), and postconditions which are no weaker than those of the base function (i.e. it makes at least the same guarantees that the base function does). If the overridden function can't be made to accept something the base function would, then should the inheritance relationship between the containing classes really exist?
                >It might be possible (though it should not happen) that your
                >compiler returns some default value which is somehow converted
                >to std::auto_ptr<P izzawhich causes a crash when
                >CreateNew_Pizz aProduct is called in ConstructPizza.
                More likely, his compiler supposes that no code flow will ever
                result in falling off the end, and doesn't do anything. So the
                calling code ends up using uninitialized memory as an auto_ptr.
                (Note that even if the calling code doesn't use the return value
                explicitly, it will call the destructor on it.)

                Comment

                • James Kanze

                  #9
                  Re: Code getting Crashed( C++)

                  On Aug 8, 3:33 pm, "Stuart Golodetz"
                  <sgolod...@dNiO aSl.PpAiMpPeLxE .AcSoEmwrote:
                  "James Kanze" <james.ka...@gm ail.comwrote in
                  messagenews:662 51dc4-da69-401e-8601-ed735986a6f1@m7 3g2000hsh.googl egroups..com...
                  [...]
                  This is even useful, in some admittedly rare cases, e.g.:
                  >
                  SomeType
                  Derived::f()
                  {
                  // The base class imposes pre-conditions which can
                  // never be met in this derived class, so...
                  assert( 0, "pre-conditions not met" ) ;
                  abort() ;
                  }
                  From a purist perspective, should Derived really inherit from
                  the base class in question in this case?
                  It depends. It depends on the contract of the base class, and
                  on the implementation of the derived class. It's not
                  unreasonable to imagine functions in the base class that can
                  only be called in a specific sequence, or a function f() that
                  can only be called if g() has successfully been called first.
                  If the implementation of the derived class is such that such
                  conditions can never occur, then yes, it's reasonable. I don't
                  think that the case occurs very often, but it can occur.

                  Most of the time such cases occur, of course; they are the
                  result of a compromise: the base class declares all possibly
                  supported functionality in a single interfaces, rather than have a
                  hierarchy of interfaces: say SeekableInputSo urce which derives
                  from InputSource. But I don't think that this is always the
                  case (although I can't think of any really good examples off
                  hand).
                  (I realise that there can sometimes be occasions when
                  pragmatism is necessary - just wondering whether this is in
                  principle best avoided?) I remember reading somewhere (and it
                  makes sense to me) that an overridden function should have
                  preconditions which are no stronger than than those of the
                  base function it overrides (i.e. it accepts anything the base
                  function would), and postconditions which are no weaker than
                  those of the base function (i.e. it makes at least the same
                  guarantees that the base function does). If the overridden
                  function can't be made to accept something the base function
                  would, then should the inheritance relationship between the
                  containing classes really exist?
                  The overriding class can strengthen post-conditions and
                  invariants. What if the pre-condition involves a post-condition
                  or invariant which the overriding class has excluded? E.g. a
                  very artificial example:

                  class Base
                  {
                  public:
                  virtual int f() ; // post: return value >= 0 and < 100
                  virtual void g() ; // pre: f() has been called, and
                  // returned a value 10
                  } ;

                  class Derived : public Base
                  {
                  public:
                  virtual int f() ; // post: return value >= 0 and < 10
                  virtual void g() ; // ???
                  } ;

                  I'm pretty sure I've encountered such cases once or twice (in
                  close to 20 years C++, so they aren't that common), although I
                  can't remember any details off hand.

                  --
                  James Kanze (GABI Software) email:james.kan ze@gmail.com
                  Conseils en informatique orientée objet/
                  Beratung in objektorientier ter Datenverarbeitu ng
                  9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                  Comment

                  Working...