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. stack corrupted

stack corrupted

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancehelp
5 Posts 2 Posters 9 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.
  • S Offline
    S Offline
    Steve Severance
    wrote on last edited by
    #1

    Run-Time Check Failure #2 - Stack around the variable 'strReturn' was corrupted. The following code bloack is giving me this error. char *ProcessOBeginning(char *word, int *change) { char *Position = strpbrk(word,"oO"); if(Position) if(isBeginning(Position,word)) { if(isupper(Position[0])) { char strReturn[] = "O"; word = strcat(strReturn,word); } else { char strReturn[] = "o"; word = strcat(strReturn,word); } } return word; } I think that it has todo with strReturn being allocated but I am not allowed to use memory management funcs for my project. Steve Not all who wander are lost...

    P 1 Reply Last reply
    0
    • S Steve Severance

      Run-Time Check Failure #2 - Stack around the variable 'strReturn' was corrupted. The following code bloack is giving me this error. char *ProcessOBeginning(char *word, int *change) { char *Position = strpbrk(word,"oO"); if(Position) if(isBeginning(Position,word)) { if(isupper(Position[0])) { char strReturn[] = "O"; word = strcat(strReturn,word); } else { char strReturn[] = "o"; word = strcat(strReturn,word); } } return word; } I think that it has todo with strReturn being allocated but I am not allowed to use memory management funcs for my project. Steve Not all who wander are lost...

      P Offline
      P Offline
      Paul M Watt
      wrote on last edited by
      #2

      With this piece of code:

      char strReturn[] = "O";

      you are only allocating one byte of memory for your string. The reason why you are getting the stack corrupt error is because you have the parameters backwards in your strcat function. I think that this is what you meant to do:

      if(isupper(Position[0]))
      {
      char strReturn[] = "O";
      strcat(word, strReturn);
      }
      else
      {
      char strReturn[] = "o";
      strcat(word, strReturn);
      }

      The string that you want to append the data to goes in the first parameter of strcat. Also the value that is returned is the same pointer that is in word. One more thing, you do not have to allocate a buffer for strReturn, you can simply do this:

      strcat(word, "o");

      Good Luck!


      Build a man a fire, and he will be warm for a day
      Light a man on fire, and he will be warm for the rest of his life!

      S 1 Reply Last reply
      0
      • P Paul M Watt

        With this piece of code:

        char strReturn[] = "O";

        you are only allocating one byte of memory for your string. The reason why you are getting the stack corrupt error is because you have the parameters backwards in your strcat function. I think that this is what you meant to do:

        if(isupper(Position[0]))
        {
        char strReturn[] = "O";
        strcat(word, strReturn);
        }
        else
        {
        char strReturn[] = "o";
        strcat(word, strReturn);
        }

        The string that you want to append the data to goes in the first parameter of strcat. Also the value that is returned is the same pointer that is in word. One more thing, you do not have to allocate a buffer for strReturn, you can simply do this:

        strcat(word, "o");

        Good Luck!


        Build a man a fire, and he will be warm for a day
        Light a man on fire, and he will be warm for the rest of his life!

        S Offline
        S Offline
        Steve Severance
        wrote on last edited by
        #3

        actually I am trying to insert on o at the beginning, so the params aren't out of order. Is there something extra that I have to do? Thanks for the reply! How do you make those code blocks? Steve Not all who wander are lost...

        P 1 Reply Last reply
        0
        • S Steve Severance

          actually I am trying to insert on o at the beginning, so the params aren't out of order. Is there something extra that I have to do? Thanks for the reply! How do you make those code blocks? Steve Not all who wander are lost...

          P Offline
          P Offline
          Paul M Watt
          wrote on last edited by
          #4

          First, in order to get the cool blocks of code type this: <pre> place your code here </pre> You can embed HTML in these statements. If you want to put the "o" at the beginning of the string, then you were doing it somewhat correctly. But here is what you need to do to make it work properly.

          if (strlen(word) < 253)
          {
          char szReturn[256];

          strcpy(szReturn, "0");
          strcat(szReturn, word);
          strcpy(word, szReturn);
          }

          The reason that I check the length of word is to prevent a memory overrun because we are only allocating 256 bytes for the string. Good Luck


          Build a man a fire, and he will be warm for a day
          Light a man on fire, and he will be warm for the rest of his life!

          S 1 Reply Last reply
          0
          • P Paul M Watt

            First, in order to get the cool blocks of code type this: <pre> place your code here </pre> You can embed HTML in these statements. If you want to put the "o" at the beginning of the string, then you were doing it somewhat correctly. But here is what you need to do to make it work properly.

            if (strlen(word) < 253)
            {
            char szReturn[256];

            strcpy(szReturn, "0");
            strcat(szReturn, word);
            strcpy(word, szReturn);
            }

            The reason that I check the length of word is to prevent a memory overrun because we are only allocating 256 bytes for the string. Good Luck


            Build a man a fire, and he will be warm for a day
            Light a man on fire, and he will be warm for the rest of his life!

            S Offline
            S Offline
            Steve Severance
            wrote on last edited by
            #5

            Thanks for all your help. I have a general question about stack corruption. I can't seem to find much information on it anywhere. Generally how is this caused? I think its because to much data is on the stack. Is this correct? How can I avoid this in the future? Thank you. Steve Not all who wander are lost...

            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