×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Run-Time Check Failure #2 - Stack around the variable was corrupted

Run-Time Check Failure #2 - Stack around the variable was corrupted

Run-Time Check Failure #2 - Stack around the variable was corrupted

(OP)
I'm something of a C++ novice and I have this legacy code C++ program I am attampting to update. Basically, this program scrapes the contents of an emulator, formats, and then sends the data to a specialized printer. This code compiled in VS 6.0 without any problems but now when I compile it in VS .NET it throws a stack error message for variable psL when it is executed.

CODE

// -------------------------------
// Print Bundle Summary
// -------------------------------
void    PrintBundleSummary()
{
    int    i;
    const    int    MAXLINES = 18;
    const    int    MAXLENGTH = 67;
    union psLineU {
        char    psLine[MAXLINES][80];
        char    psLinew[1440];
    } psL;
    char sWork[128];

    PrinterStart();
    RCPprinterControl(RCPCLOSEJAW);

    wFldLen = sizeof psL.psLinew;
    wFldLoc = 91;
    RCPCopyPSstr(psL.psLinew, wFldLen, wFldLoc);               //Scrape contents of emulator screen

    for (i=0; i<MAXLINES; i++) {
        psL.psLine[i][MAXLENGTH] = '\0';
                sprintf (sWork, "%s\r\n", psL.psLine[i]);      //Build sWork
                PrinterSendDataString (sWork);                 //Print contents of sWork
    }

    RCPprinterControl(RCPSTARLINE);
    
    RCPprinterControl(RCPPAPEREJECT);
    RCPprinterControl(RCPPAPEREJECT);

    PrinterStop ();
    return;
}
Obviously, this is happening in the FOR loop yet my printer prints fine and it never complained about it in VS 6.0. I reduced the value of MAXLINES but that didn't fix anything and it didn't print the last line, so 1) why does .NET think it's wrong and 2) how do I fix it?

Thanks!

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

build sWork using a loop and look what happens

and you know that inserting '\0' will overwrite data.

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

(OP)

Quote (fheyn):

build sWork using a loop and look what happens
You mean using something other than a FOR loop?

Quote (fheyn):

you know that inserting '\0' will overwrite data.
I understand that '\0' is used to indicate the end of a line so the rest of the array is ignored, but that's about the extent of my knowledge. Is the way it is handled in .NET different than VS 6?

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

What do RCPCopyPSstr() & PrinterSendDataString() do to the variables you pass in to them?  My guess is that one of them is overflowing a buffer.

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

try this :

sWork[MAXLENGTH] = '\r';
sWork[MAXLENGTH+1] = '\n';

for (i=0; i<MAXLINES; i++) {
   for (j=0; j < MAXLENGTH; j++)
     sWork[j] = psL.psLine[i][j];

   PrinterSendDataString (sWork);
}

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

What is wFldLoc?  Should it be less than MAXLINES or MAXLINES+ 1 (19)?

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

(OP)

Quote (xwb):

What is wFldLoc?  Should it be less than MAXLINES or MAXLINES+ 1 (19)?
wFldLoc is the starting position on psLinew. I guess I need to take a step back before I continue. This program scrapes the contents of the emulator, 18 rows of 80 characters, and stores them in psLineU, a single array of (18*80) 1440 characters. RCPCopyPSstr copies wFldLen characters, begining at wFldLoc to psLinew. Because the printer is narrow, I am only sending the first MAXLENGTH characters to it in the FOR loop.

On a related note, I think I found the problem, or at least am heading on the right track. I think the problem involves this union

CODE

union psLineU {
        char    psLine[MAXLINES][80];
        char    psLinew[1440];
    } psL;
and this code snippet

CODE

    wFldLen = sizeof psL.psLinew;
    wFldLoc = 91;
    RCPCopyPSstr(psL.psLinew, wFldLen, wFldLoc);              //Scrape contents of emulator screen
It would appear that psLinew is not the same size as psLine. After some experimententation, I determined that psLinew is too small, but I am not sure what the correct size should be. I enlarged it and assigned wFldLen the constant value 1440 and it runs fine now. Does anyone have an idea why the sizes were no longer correct? ponder

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

Unless my calculator is wrong, they should be the same size.
18 x 80 = 1440

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

Why not set it to (80*MAXLINES).  Then it will adjust accordingly.
 

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

if you store at position x, your array can only handle
1440-x bytes.
 

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

(OP)
I found it. For some reason, VS C .NET insists that psLinew be 1441. I am guessing the extra char is for the string terminator, but it's odd that it didn't care when I compliled this under VC++ 6...
 

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

How big is the actual data that you store in there?
If it's a string with 1440 chars or more, then obviously you'd need to make the variable big enough for the string + the '\0' on the end.

In debug mode, VC++ adds 2 extra chars before & after every buffer, which I believe should be set to this hex value 0xFDFD.  That way, you can easily find buffer overflows in your debugger by checking if those bytes got changed to some other value.

RE: Run-Time Check Failure #2 - Stack around the variable was corrupted

It probably did matter but the run time probably didn't go over the stack frme or corrupt the word boundary so it looked as if it wasn't a problem.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close