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. My progremmer can't run,why??

My progremmer can't run,why??

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

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

    class CBuffer
    {
    char * m_pBuffer;
    int m_size;
    public:
    CBuffer()
    {
    m_pBuffer=NULL; }
    ~CBuffer()
    {
    Free();
    }
    void Allocte(int size) (3) {
    m_size=size;
    m_pBuffer= new char[size];
    }
    private:
    void Free()
    {
    if(m_pBuffer!=NULL)
    {
    delete m_pBuffer;
    m_pBuffer=NULL;
    }
    }
    public:
    void SaveString(const char* pText) const
    {
    strcpy(m_pBuffer, pText);
    }
    char* GetBuffer() const
    {
    return m_pBuffer;
    }
    };

    int main ()
    {
    CBuffer buffer1;
    buffer1.SaveString("Microsoft");
    printf("%s", buffer1.GetBuffer());
    return 0;
    }

    C C T 3 Replies Last reply
    0
    • W wbgxx

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

      class CBuffer
      {
      char * m_pBuffer;
      int m_size;
      public:
      CBuffer()
      {
      m_pBuffer=NULL; }
      ~CBuffer()
      {
      Free();
      }
      void Allocte(int size) (3) {
      m_size=size;
      m_pBuffer= new char[size];
      }
      private:
      void Free()
      {
      if(m_pBuffer!=NULL)
      {
      delete m_pBuffer;
      m_pBuffer=NULL;
      }
      }
      public:
      void SaveString(const char* pText) const
      {
      strcpy(m_pBuffer, pText);
      }
      char* GetBuffer() const
      {
      return m_pBuffer;
      }
      };

      int main ()
      {
      CBuffer buffer1;
      buffer1.SaveString("Microsoft");
      printf("%s", buffer1.GetBuffer());
      return 0;
      }

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      And what is the problem exactly ?

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      W C 2 Replies Last reply
      0
      • C Cedric Moonen

        And what is the problem exactly ?

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        W Offline
        W Offline
        wbgxx
        wrote on last edited by
        #3

        sorry ,I don't know why

        C 1 Reply Last reply
        0
        • W wbgxx

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

          class CBuffer
          {
          char * m_pBuffer;
          int m_size;
          public:
          CBuffer()
          {
          m_pBuffer=NULL; }
          ~CBuffer()
          {
          Free();
          }
          void Allocte(int size) (3) {
          m_size=size;
          m_pBuffer= new char[size];
          }
          private:
          void Free()
          {
          if(m_pBuffer!=NULL)
          {
          delete m_pBuffer;
          m_pBuffer=NULL;
          }
          }
          public:
          void SaveString(const char* pText) const
          {
          strcpy(m_pBuffer, pText);
          }
          char* GetBuffer() const
          {
          return m_pBuffer;
          }
          };

          int main ()
          {
          CBuffer buffer1;
          buffer1.SaveString("Microsoft");
          printf("%s", buffer1.GetBuffer());
          return 0;
          }

          C Offline
          C Offline
          Covean
          wrote on last edited by
          #4

          In your constructor you set buffer to NULL. In your main function you just create an instance of CBuffer and call SaveString. SaveString accesses your buffer (what is NULL), so you get an access violation.

          int main ()
          {
          CBuffer buffer1;
          buffer1.Allocate(4096); // allocate some buffer for your string!
          buffer1.SaveString("Microsoft");
          printf("%s", buffer1.GetBuffer());
          return 0;
          }

          Greetings Covean

          1 Reply Last reply
          0
          • W wbgxx

            sorry ,I don't know why

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            What I meant is: try to give more details. "It doesn't work" is way too vague. Did you have a compilation error, a run-time crash, your computer exploded, ... ? Keep in mind that we can't see what's on your screen.

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            1 Reply Last reply
            0
            • C Cedric Moonen

              And what is the problem exactly ?

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              wbgxx wrote:

              "Microsoft"

              ?

              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]

              1 Reply Last reply
              0
              • W wbgxx

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

                class CBuffer
                {
                char * m_pBuffer;
                int m_size;
                public:
                CBuffer()
                {
                m_pBuffer=NULL; }
                ~CBuffer()
                {
                Free();
                }
                void Allocte(int size) (3) {
                m_size=size;
                m_pBuffer= new char[size];
                }
                private:
                void Free()
                {
                if(m_pBuffer!=NULL)
                {
                delete m_pBuffer;
                m_pBuffer=NULL;
                }
                }
                public:
                void SaveString(const char* pText) const
                {
                strcpy(m_pBuffer, pText);
                }
                char* GetBuffer() const
                {
                return m_pBuffer;
                }
                };

                int main ()
                {
                CBuffer buffer1;
                buffer1.SaveString("Microsoft");
                printf("%s", buffer1.GetBuffer());
                return 0;
                }

                T Offline
                T Offline
                Tim Craig
                wrote on last edited by
                #7

                Maybe he needs a new pair of sneakers? :laugh:

                The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.

                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