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