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

    Question

  • When ending the program, I get the error: "Run-Time Check Failure #2 - Stack around the variable 'exit' was corrupted." No idea how to stop the corruption... any ideas? EDIT: After posting on here, I saw exit highlighted, so I changed the variable to "ext". this did not solve my problem.

    void main()
    {
    	pid data;		
    	float co;
    	char exit;
    
    	data=data_input();
    	do					
    	{
    		printf("Please enter a PV: ");
    		scanf("%f", &data.pv);
    		co=calc_co(data);
    		printf("The CO is: %0.2f", co);
    		printf("\nPress Y to continue (any other key to exit):");
    		scanf("%s", &exit);
    	}while(exit=='Y'||exit=='y');
    	flushall();
    	getchar();
    }



    • Edited by CKTofu Wednesday, February 08, 2012 4:50 PM
    Wednesday, February 08, 2012 4:43 PM

Answers

  • Il 08/02/2012 17:43, CKTofu ha scritto:

    When ending the program, I get the error: "Run-Time Check Failure #2 - Stack around the variable 'exit' was corrupted." No idea how to stop the corruption... any ideas? EDIT: After posting on here, I saw exit highlighted, so I changed the variable to ext. this did not solve my problem.

    What is "pid"?
    And what does data_input() do?
    The problem may be in data_input(), i.e. in sections of code you haven't showed.

    Moreover, I suspect the scanf("%s", &exit).
    exit is a char, not a string.
    You may want to use %c instead of %s:

    scanf("%c", &exit);

    Giovanni

    • Marked as answer by CKTofu Wednesday, February 08, 2012 4:54 PM
    Wednesday, February 08, 2012 4:50 PM

All replies

  • Il 08/02/2012 17:43, CKTofu ha scritto:

    When ending the program, I get the error: "Run-Time Check Failure #2 - Stack around the variable 'exit' was corrupted." No idea how to stop the corruption... any ideas? EDIT: After posting on here, I saw exit highlighted, so I changed the variable to ext. this did not solve my problem.

    What is "pid"?
    And what does data_input() do?
    The problem may be in data_input(), i.e. in sections of code you haven't showed.

    Moreover, I suspect the scanf("%s", &exit).
    exit is a char, not a string.
    You may want to use %c instead of %s:

    scanf("%c", &exit);

    Giovanni

    • Marked as answer by CKTofu Wednesday, February 08, 2012 4:54 PM
    Wednesday, February 08, 2012 4:50 PM
  • pid is a type of struct, and data_input() requests data for that struct.

    anyway, changing %s to %c solved the problem, thanks!
    Wednesday, February 08, 2012 4:54 PM
  • Il 08/02/2012 17:54, CKTofu ha scritto:

    pid is a type of struct, and data_input() requests data for that struct.

    anyway, changing %s to %c solved the problem, thanks!

    So, the problem was that scanf() with %s format specifier was adding a \0 string-termination character after the character input by the user, but considering that the storage for the variable "exit" is only one char, there was a kind of buffer overrun.

    Giovanni


    Wednesday, February 08, 2012 5:08 PM