What is the strange phenomenon of stack around the variable was wide upted and the strange phenomenon of sprintf?

Source: Internet
Author: User
Code:
  1. Reason: When I use sprintf to pay an unsigned char value in the form of % 02x to an array or pointer of the char [2] type, the system can calculate it normally, but an error is returned: stack around the variable was too upted. Check the cause on the Internet:
  2. There is almost no useful information. There is only one temporary solution:
  3. [Original article: Check project-> Configuration properties-> C/C ++-> code generation-> basic runtime check as the default value. This exception is not reported. The specific cause is being studied ...]
  4. This method does solve this error. But why ??
  5. I re-wrote a very simple value assignment main:
  6. Int _ tmain (INT argc, _ tchar * argv [])
  7. {
  8. Char A [2];
  9. Sprintf (a, "% x", 15 );
  10. Printf ("% C/N", a [0]);
  11. Printf ("% s", );
  12. Return 0;
  13. }
  14. Display:
  15. F
  16. F
  17. The above code will not prompt a warning. Write 15 in hexadecimal form to address a, which is actually a [0] = 'F '. therefore, both printf are correctly executed and no error is reported in the program.
  18. Next, assign a value to a [1]. We know that if a [1] is not 0, there will be no ending symbol when it is output in % S, the system will output something messy:
  19. Int _ tmain (INT argc, _ tchar * argv [])
  20. {
  21. Char A [2];
  22. Sprintf (a, "% x", 15 );
  23. A [1] = 'D ';
  24. Printf ("% C/N", a [0]);
  25. Printf ("% C/N", a [1]);
  26. Printf ("% s", );
  27. Return 0;
  28. }
  29. Display:
  30. F
  31. D
  32. FD hot overlooking'
  33. Despite the problem, no warning is displayed!
  34. However, if we assign a value to a [1] in another way,
  35. Int _ tmain (INT argc, _ tchar * argv [])
  36. {
  37. Char A [2];
  38. Sprintf (a, "% x", 15 );
  39. Sprintf (a + 1, "% x", 14 );
  40. Printf ("% C/N", a [0]);
  41. Printf ("% C/N", a [1]);
  42. Printf ("% s", );
  43. Return 0;
  44. }
  45. An error occurs ::
  46. But printf runs normally. Why ??
  47. What is the essential difference between the direct replication of a [1] and the assignment here? Why is this error reported ??
  48. After testing, if the range of array a is extended, it is changed to a [3], that is:
  49. Int _ tmain (INT argc, _ tchar * argv [])
  50. {
  51. Char A [3];
  52. Sprintf (a, "% x", 15 );
  53. // A [1] = 'D ';
  54. Sprintf (a + 1, "% x", 14 );
  55. Printf ("% C/N", a [0]);
  56. Printf ("% C/N", a [1]);
  57. Printf ("% s", );
  58. Return 0;
  59. }
  60. The system will run normally and the error window will not pop up.
  61. Why ?? What does it do with sprintf ?? In addition, if you Google it, this problem is common. It is generally set to adjust the configuration of vs according to the beginning, just as I have provided several examples above, it must have appeared in a specific situation, why?

I also encountered this problem. Why is such an error? After reading the article, I understand only half of it.

Article Source: http://topic.csdn.net/u/20100620/23/610038d6-95a3-4901-903c-2a9d7fb25bee.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.