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. Heap corruption?!

Heap corruption?!

Scheduled Pinned Locked Moved C / C++ / MFC
c++debuggingperformancehelpquestion
17 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.
  • N Offline
    N Offline
    Nader Elshehabi
    wrote on last edited by
    #1

    Hello Looks like my C++ is getting really rusty. While playing a bit in a console project the following code gave me a strange heap corruption error.

    #include

    using namespace std;

    void main()
    {
    char* MyText = new char;
    cin>>MyText;
    delete[] MyText;
    }

    It's not even an exception. Sometimes it says "Windows have set a breakpoint in your application because of a heap error." And a break point appears somewhere in the iostream file. Other times, an ugly error message appears saying "HEAP CORRUPTION DETECTED after a normal block. CRT detected that application wrote to memory after end of heap buffer.", Any clue??

    Regards:rose:

    Z J S 3 Replies Last reply
    0
    • N Nader Elshehabi

      Hello Looks like my C++ is getting really rusty. While playing a bit in a console project the following code gave me a strange heap corruption error.

      #include

      using namespace std;

      void main()
      {
      char* MyText = new char;
      cin>>MyText;
      delete[] MyText;
      }

      It's not even an exception. Sometimes it says "Windows have set a breakpoint in your application because of a heap error." And a break point appears somewhere in the iostream file. Other times, an ugly error message appears saying "HEAP CORRUPTION DETECTED after a normal block. CRT detected that application wrote to memory after end of heap buffer.", Any clue??

      Regards:rose:

      Z Offline
      Z Offline
      Zac Howland
      wrote on last edited by
      #2

      Nader Elshehabi wrote:

      char* MyText = new char; cin>>MyText; delete[] MyText;

      new matches up with delete. new[] matches up with delete[]. Calling delete[] on something that was new'd will result in a heap corruption. To fix the problem:

      void main()
      {
      	char* MyText = new char;
      	cin>>MyText;
      	delete MyText;
      }
      

      or

      void main()
      {
      	char* MyText = new char[100];
      	cin>>MyText;
      	delete [] MyText;
      }
      

      If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

      N 1 Reply Last reply
      0
      • N Nader Elshehabi

        Hello Looks like my C++ is getting really rusty. While playing a bit in a console project the following code gave me a strange heap corruption error.

        #include

        using namespace std;

        void main()
        {
        char* MyText = new char;
        cin>>MyText;
        delete[] MyText;
        }

        It's not even an exception. Sometimes it says "Windows have set a breakpoint in your application because of a heap error." And a break point appears somewhere in the iostream file. Other times, an ugly error message appears saying "HEAP CORRUPTION DETECTED after a normal block. CRT detected that application wrote to memory after end of heap buffer.", Any clue??

        Regards:rose:

        J Offline
        J Offline
        Jun Du
        wrote on last edited by
        #3

        Beside what Zac said, another suspicious area is cin>>MyText; When you expected MyText is one char, your input could easily be more than one char, which would cause a memory problem.

        Best, Jun

        N 1 Reply Last reply
        0
        • Z Zac Howland

          Nader Elshehabi wrote:

          char* MyText = new char; cin>>MyText; delete[] MyText;

          new matches up with delete. new[] matches up with delete[]. Calling delete[] on something that was new'd will result in a heap corruption. To fix the problem:

          void main()
          {
          	char* MyText = new char;
          	cin>>MyText;
          	delete MyText;
          }
          

          or

          void main()
          {
          	char* MyText = new char[100];
          	cin>>MyText;
          	delete [] MyText;
          }
          

          If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello Thanks for replying. Well, the first coder didn't work, while the second did. Yet, that makes a buffer of N chars. That's not what I want. What if the user inputs n+1 chars. I tried it and it made the same heap corruption error. Also what if I dynamically reallocate my array -or pointer-?? If you got more details or point me to some article about this issue, I'd be grateful. Thanks again.

          Regards:rose:

          Z 1 Reply Last reply
          0
          • J Jun Du

            Beside what Zac said, another suspicious area is cin>>MyText; When you expected MyText is one char, your input could easily be more than one char, which would cause a memory problem.

            Best, Jun

            N Offline
            N Offline
            Nader Elshehabi
            wrote on last edited by
            #5

            Hello Jun. Actually my input field is char*, and I was expecting a text rather than just one char. Besides, this is nothing more than an experimental project which turned out to be disastrous!!;P

            Regards:rose:

            1 Reply Last reply
            0
            • N Nader Elshehabi

              Hello Thanks for replying. Well, the first coder didn't work, while the second did. Yet, that makes a buffer of N chars. That's not what I want. What if the user inputs n+1 chars. I tried it and it made the same heap corruption error. Also what if I dynamically reallocate my array -or pointer-?? If you got more details or point me to some article about this issue, I'd be grateful. Thanks again.

              Regards:rose:

              Z Offline
              Z Offline
              Zac Howland
              wrote on last edited by
              #6

              Nader Elshehabi wrote:

              Well, the first coder didn't work, while the second did.

              The first code has another problem due to the fact that cin's operator>> will pull in 2 characters even if you only typed 1 (the character you typed and a '\0' to terminate the string). If you are only trying to pull in 1 character, the cin.getc() would work better.

              Nader Elshehabi wrote:

              Yet, that makes a buffer of N chars. That's not what I want. What if the user inputs n+1 chars.

              You will not be able to use the operator>> to do this (at least not yet ... as I said, the new proposal for the C++ standard has all overloaded operators that deal with char* now also overloaded for string). What you can do is iterate through it:

              char ch;
              string input;
              while ((ch = cin.getc()) != '\n')
              {
                 input += ch;
              }
              

              That will prevent any possibility of a buffer overflow issue, but will also be VERY slow (in comparison to pulling down a whole set of data at once).

              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

              N S 2 Replies Last reply
              0
              • Z Zac Howland

                Nader Elshehabi wrote:

                Well, the first coder didn't work, while the second did.

                The first code has another problem due to the fact that cin's operator>> will pull in 2 characters even if you only typed 1 (the character you typed and a '\0' to terminate the string). If you are only trying to pull in 1 character, the cin.getc() would work better.

                Nader Elshehabi wrote:

                Yet, that makes a buffer of N chars. That's not what I want. What if the user inputs n+1 chars.

                You will not be able to use the operator>> to do this (at least not yet ... as I said, the new proposal for the C++ standard has all overloaded operators that deal with char* now also overloaded for string). What you can do is iterate through it:

                char ch;
                string input;
                while ((ch = cin.getc()) != '\n')
                {
                   input += ch;
                }
                

                That will prevent any possibility of a buffer overflow issue, but will also be VERY slow (in comparison to pulling down a whole set of data at once).

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                N Offline
                N Offline
                Nader Elshehabi
                wrote on last edited by
                #7

                Hello Thanks again for your reply! I got two more questions:-D. Take a look at this code;

                char* MyText = new char[5];
                cin>>MyText;
                MyText = new char[10];
                delete[] MyText;

                Now , I called a second new without deleting the first one. Is there a leak here?? What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed? Or will it be permanently locked until reboot?

                Regards:rose:

                D Z 2 Replies Last reply
                0
                • N Nader Elshehabi

                  Hello Thanks again for your reply! I got two more questions:-D. Take a look at this code;

                  char* MyText = new char[5];
                  cin>>MyText;
                  MyText = new char[10];
                  delete[] MyText;

                  Now , I called a second new without deleting the first one. Is there a leak here?? What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed? Or will it be permanently locked until reboot?

                  Regards:rose:

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

                  Nader Elshehabi wrote:

                  Is there a leak here??

                  Yes.

                  Nader Elshehabi wrote:

                  Will the memory be freed?

                  Yes.


                  "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                  "Judge not by the eye but by the heart." - Native American Proverb

                  N 1 Reply Last reply
                  0
                  • N Nader Elshehabi

                    Hello Thanks again for your reply! I got two more questions:-D. Take a look at this code;

                    char* MyText = new char[5];
                    cin>>MyText;
                    MyText = new char[10];
                    delete[] MyText;

                    Now , I called a second new without deleting the first one. Is there a leak here?? What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed? Or will it be permanently locked until reboot?

                    Regards:rose:

                    Z Offline
                    Z Offline
                    Zac Howland
                    wrote on last edited by
                    #9

                    Nader Elshehabi wrote:

                    Now , I called a second new without deleting the first one. Is there a leak here??

                    Are you coming from a Java/C# background by chance? Yes, this will result in a memory leak. Whenever you use new, you must have a delete call to match it. Having 2 calls to new and only 1 call to delete means here is a leak.

                    Nader Elshehabi wrote:

                    What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed? Or will it be permanently locked until reboot?

                    When the program exits its memory will be freed. You do NOT want to rely on this, though. Always clean up your memory properly.

                    If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                    N 1 Reply Last reply
                    0
                    • D David Crow

                      Nader Elshehabi wrote:

                      Is there a leak here??

                      Yes.

                      Nader Elshehabi wrote:

                      Will the memory be freed?

                      Yes.


                      "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                      "Judge not by the eye but by the heart." - Native American Proverb

                      N Offline
                      N Offline
                      Nader Elshehabi
                      wrote on last edited by
                      #10

                      Hello Short, and straight answers!! Thanks:-D

                      Regards:rose:

                      1 Reply Last reply
                      0
                      • Z Zac Howland

                        Nader Elshehabi wrote:

                        Now , I called a second new without deleting the first one. Is there a leak here??

                        Are you coming from a Java/C# background by chance? Yes, this will result in a memory leak. Whenever you use new, you must have a delete call to match it. Having 2 calls to new and only 1 call to delete means here is a leak.

                        Nader Elshehabi wrote:

                        What if I didn't call a delete at all!! Once my program finishes and closes. Will the memory be freed? Or will it be permanently locked until reboot?

                        When the program exits its memory will be freed. You do NOT want to rely on this, though. Always clean up your memory properly.

                        If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                        N Offline
                        N Offline
                        Nader Elshehabi
                        wrote on last edited by
                        #11

                        Hello

                        Zac Howland wrote:

                        Are you coming from a Java/C# background by chance?

                        Well, somehow:)! I was a good old timer C++ programmer:((. But last time I ever wrote C++ code was more than 3 years ago. So I got really rusty, coming back these days to C++ as a new comer. So, while refreshing all that good ol' C++ of mine, it's nice to have you guys around;) Thanks again.

                        Extra Regards:rose:

                        1 Reply Last reply
                        0
                        • N Nader Elshehabi

                          Hello Looks like my C++ is getting really rusty. While playing a bit in a console project the following code gave me a strange heap corruption error.

                          #include

                          using namespace std;

                          void main()
                          {
                          char* MyText = new char;
                          cin>>MyText;
                          delete[] MyText;
                          }

                          It's not even an exception. Sometimes it says "Windows have set a breakpoint in your application because of a heap error." And a break point appears somewhere in the iostream file. Other times, an ugly error message appears saying "HEAP CORRUPTION DETECTED after a normal block. CRT detected that application wrote to memory after end of heap buffer.", Any clue??

                          Regards:rose:

                          S Offline
                          S Offline
                          Stephen Hewitt
                          wrote on last edited by
                          #12

                          Try this instead. #include <iostream> #include <iomanip>   int main(int argc, char *argv[]) {     using namespace std;       char *pChar = new char[2];     cin >> setw(2) >> pChar;     delete [] pChar;       return 0; } You're reading too many charcters: you've got room for a NULL terminator (a string of zero length!) but your reading more and getting a buffer overrun. My code tells the IO classes how big the buffer is so it will not overrun it. If you only want to read a single char you could try doing this (in your code): cin >> *MyText;

                          Steve

                          N 1 Reply Last reply
                          0
                          • Z Zac Howland

                            Nader Elshehabi wrote:

                            Well, the first coder didn't work, while the second did.

                            The first code has another problem due to the fact that cin's operator>> will pull in 2 characters even if you only typed 1 (the character you typed and a '\0' to terminate the string). If you are only trying to pull in 1 character, the cin.getc() would work better.

                            Nader Elshehabi wrote:

                            Yet, that makes a buffer of N chars. That's not what I want. What if the user inputs n+1 chars.

                            You will not be able to use the operator>> to do this (at least not yet ... as I said, the new proposal for the C++ standard has all overloaded operators that deal with char* now also overloaded for string). What you can do is iterate through it:

                            char ch;
                            string input;
                            while ((ch = cin.getc()) != '\n')
                            {
                               input += ch;
                            }
                            

                            That will prevent any possibility of a buffer overflow issue, but will also be VERY slow (in comparison to pulling down a whole set of data at once).

                            If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                            S Offline
                            S Offline
                            Stephen Hewitt
                            wrote on last edited by
                            #13

                            That's code is a good the way to read a single line, try this instead: getline(cin, input);

                            Steve

                            Z 1 Reply Last reply
                            0
                            • S Stephen Hewitt

                              That's code is a good the way to read a single line, try this instead: getline(cin, input);

                              Steve

                              Z Offline
                              Z Offline
                              Zac Howland
                              wrote on last edited by
                              #14

                              That still has the problem that he will need to limit his buffer size in order to prevent overrun problems. And the safer version is:

                              char buffer[100] = {0};
                              cin.getline(buffer, 99);
                              

                              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                              S 1 Reply Last reply
                              0
                              • Z Zac Howland

                                That still has the problem that he will need to limit his buffer size in order to prevent overrun problems. And the safer version is:

                                char buffer[100] = {0};
                                cin.getline(buffer, 99);
                                

                                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                S Offline
                                S Offline
                                Stephen Hewitt
                                wrote on last edited by
                                #15

                                You don't have to limit the size of the buffer, you use std::string. ie. string line; getline(cin, line); Assumes using namespace std; and #include <iostream> and finally #include <string>

                                Steve

                                Z 1 Reply Last reply
                                0
                                • S Stephen Hewitt

                                  You don't have to limit the size of the buffer, you use std::string. ie. string line; getline(cin, line); Assumes using namespace std; and #include <iostream> and finally #include <string>

                                  Steve

                                  Z Offline
                                  Z Offline
                                  Zac Howland
                                  wrote on last edited by
                                  #16

                                  Ah, you are correct. I completely forgot about the general getline function ... :doh:

                                  If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                                  1 Reply Last reply
                                  0
                                  • S Stephen Hewitt

                                    Try this instead. #include <iostream> #include <iomanip>   int main(int argc, char *argv[]) {     using namespace std;       char *pChar = new char[2];     cin >> setw(2) >> pChar;     delete [] pChar;       return 0; } You're reading too many charcters: you've got room for a NULL terminator (a string of zero length!) but your reading more and getting a buffer overrun. My code tells the IO classes how big the buffer is so it will not overrun it. If you only want to read a single char you could try doing this (in your code): cin >> *MyText;

                                    Steve

                                    N Offline
                                    N Offline
                                    Nader Elshehabi
                                    wrote on last edited by
                                    #17

                                    Thanks Steve! I appreciate your response.:)

                                    Regards:rose:

                                    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