Thread: corrupted stack???

Hybrid View

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

  2. #2
    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.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,337
    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?

  4. #4
    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.

  5. #5
    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.

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