Thread: corrupted stack???

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    corrupted stack???

    Hi,
    I'm having a little problem. When my program is terminating (in debug mode) MSVC++ 2008 gives me the following message: Run-Time Check Failure #2 - Stack around the variable 'originalExtension' was corrupted.
    originalExtension is an array with 21 elements that holds a string (20 characters + null). I read a string from a file using the following line:
    fgets(extension, 21, readfile);

    What does this error mean? my program seems to run normally, and the error appears only at the return statement in main().

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are probably overwriting the stack someplace else...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    valid indexes on originalExtension if it is declared to be 21 elements would be 0-20. by writing the null in the 21 element you are writing it off the end of the buffer (which explains the stack error).

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    fgets() reads a max of num-1 bytes, so in this case, it only reads up to 20 bytes.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    but fgets() also adds an end of string char, so again, it's writing past the end of the array, by one char.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    In Debug mode, Visual C++ puts 4 special chars before and after the memory you allocated (0xFD I think), and if it sees that those chars have changed, it knows you wrote past the end (or beginning) of your buffer. If you step through in debug mode and look at the memory address for your pointer you should see something like:
    Code:
    FD FD FD FD 34 38 54 43 ... FD FD FD FD
    If you execute one line at a time and look at the memory after each step, you might see 1 or more of the FD's turn into something else. That would be your bug.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by Adak View Post
    but fgets() also adds an end of string char, so again, it's writing past the end of the array, by one char.
    And?

    fgets() only reads num-1 bytes.

    num=21.

    The array is 21 bytes.

    Therefore, fgets() reads 20 bytes, places them in array positions 0-19, and then writes a null term char in index position 20.

    So, tell me again - where's the overlay here?

  8. #8
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    indeed, my bad, I didn't even notice the fgets the first time I read it, I just assumed you were writing 20 elements and attempting to write a null at the 21.

    the error is also quite evident, your problem exists somewhere other than the call to fgets.

    Run-Time Check Failure #2 - Stack around the variable 'originalExtension' was corrupted.
    fgets(extension, 21, readfile);

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You should be writing
    fgets( extension, sizeof extension, readfile );
    and not using magic numbers for the size.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Another, slightly more advanced method would be to add a data breakpoint to &originalExtension + sizeof(originalExtension), so that when the memory beyond originalExtension is modified, it will break. Handy stuff.

    Quote Originally Posted by cpjust View Post
    In Debug mode, Visual C++ puts 4 special chars before and after the memory you allocated (0xFD I think), and if it sees that those chars have changed, it knows you wrote past the end (or beginning) of your buffer. If you step through in debug mode and look at the memory address for your pointer you should see something like:
    Code:
    FD FD FD FD 34 38 54 43 ... FD FD FD FD
    If you execute one line at a time and look at the memory after each step, you might see 1 or more of the FD's turn into something else. That would be your bug.
    This only applies to when using new, however. All stack variables are filled with 0xCD I believe, but no bytes before or after. That only applies to new.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    EDIT: repeated post
    Last edited by Abda92; 01-31-2008 at 03:24 AM.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    EDIT: repeated post. sorry
    Last edited by Abda92; 01-31-2008 at 03:25 AM.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    I'm sorry. As sl34k said, the error is not here, even though he guessed it wrong. I was looking at the wrong *section* of the program. My error was in another function that extracted and saved the extension from a file name into originalExtension. It did not check whether we have exceeded the capacity of the array or not.

    Thanks again, and sorry for the bothering you but I had been working on it for a while (I guess I was tired because I found the problem now in 5 min.).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM