Thread: Run-Time Check Failure #2

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    Run-Time Check Failure #2

    Hi, I can't seem to figure this out. I ran this function and it worked the way I intended but although the output was correct I got this weird error.

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

    Could someone point me in the direction about what I've done wrong?

    thanks,
    MIchelle

    Code:
    int correctionConversion(void)
    {
    	int count = 0;
    	char test[num];
    	printf("Please enter a string");
    	fgets (test , num , stdin);
    	test[num] = '\0';
    	printf("%s", test);
    	printf("if this program is working this should not be on a newline");
    
    }

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    1. Where to you allocate num?
    2. test[num] is outside the bounds of your array. C arrays start indexing at 0.
    3. You declared your function as returning an int but failed to do so.
    4. You never use your count variable, I am assuming you wanted to store the return value of fgets with it.
    Last edited by AndrewHunter; 08-22-2011 at 11:14 PM. Reason: spelling==bad
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mjskolly View Post
    Hi, I can't seem to figure this out. I ran this function and it worked the way I intended but although the output was correct I got this weird error.

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

    Could someone point me in the direction about what I've done wrong?

    thanks,
    MIchelle

    Code:
    int correctionConversion(void)
    {
    	int count = 0;
    	char test[num];
    	printf("Please enter a string");
    	fgets (test , num , stdin);
    	test[num] = '\0';
    	printf("%s", test);
    	printf("if this program is working this should not be on a newline");
    
    }
    Ok... dig into your compiler settings and turn the warning levels all the way up... Then treat each warning as an issue to be dealt with... Your compiler should be screeching at you about an implicit declaration of num, num being used but never intialized to a value, missing return values, count being intialized but never used...

    Considering that num would be implicitly created on the stack and it's initial contents would likely be an address-like value somewhere in range of your program's workspace (i.e. way the heck more than you intended), your test array could amount to several megabytes... created on a stack that is typically 1 megabyte....

    All that because you left out the simple line ... int num = 100; and/or didn't declare your array specifically eg. char test[100]; or char test[MAXBUF];

    This is C ... the devil is in the details.
    Last edited by CommonTater; 08-22-2011 at 10:02 PM.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    HI, I declared num in the header.
    Not where I would have intuitively put it but the teacher gave me ........ for not making all of my numbers into variables. When I declared it in the function the compiler said it needed to be a constant so i put it in the header with #defines num 12 .

    For some reason the teacher seems to think we should define as much stuff as possible in the header, pretty much anything that is not going to change values.

    I fixed it but now the program part doesnt work Now the newline is still there. At least that I can work with.
    thanks

    EDIT: I fixed the program but now I don't know why it worked in the first place earlier.
    Learning is so much fun
    Last edited by mjskolly; 08-22-2011 at 10:57 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by mjskolly
    For some reason the teacher seems to think we should define as much stuff as possible in the header, pretty much anything that is not going to change values.
    Your teacher is probably referring to constants defined with #define. Actually, it makes sense here, though you should rename num to something more descriptive and use the convention that macro names should be fully uppercase.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by mjskolly View Post
    EDIT: I fixed the program but now I don't know why it worked in the first place earlier.
    Learning is so much fun
    Pure dumb luck. You were writing out of bounds of your array which results in undefined behavior. You can read about what undefined behavior is. Read about how to get a line of text from the user.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mjskolly View Post
    HI, I declared num in the header.
    Not where I would have intuitively put it but the teacher gave me ........ for not making all of my numbers into variables. When I declared it in the function the compiler said it needed to be a constant so i put it in the header with #defines num 12 .

    For some reason the teacher seems to think we should define as much stuff as possible in the header, pretty much anything that is not going to change values.

    I fixed it but now the program part doesnt work Now the newline is still there. At least that I can work with.
    thanks

    EDIT: I fixed the program but now I don't know why it worked in the first place earlier.
    Learning is so much fun
    Check the header... it's #define not #defines ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 11-05-2010, 10:34 AM
  2. Run-Time Check Failure #2 help
    By lazysam in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 09:14 AM
  3. Replies: 14
    Last Post: 11-17-2008, 12:31 PM
  4. Replies: 3
    Last Post: 05-22-2007, 11:42 PM
  5. Run-time check failirue #0
    By RoshanX in forum Windows Programming
    Replies: 0
    Last Post: 04-04-2006, 03:50 PM