C language: a typical case involving the garbled and memory stack of the pointer function returned value and printf

Source: Internet
Author: User

C language: a typical case involving the garbled and memory stack of the pointer function returned value and printf
A strange C language problem involves pointers, arrays, stacks, and printf. The following implementation: converts Integers to strings, returns string pointers, and CALLS printf in the main function for display.

 
 
  1. #include
  2. #include
  3. #include
  4. char* swich(int n)
  5. {
  6. char A[20],B[20];
  7. char*p;//=(char*)malloc(4*sizeof(char));
  8. int i=0,a;
  9. int minus=0;
  10. if(n<0)
  11. {
  12. minus=1;
  13. n=-n;
  14. }
  15. while(n/10!=0)
  16. {
  17. a=n%10;
  18. n=n/10;
  19. A[i++]='0'+a;
  20. }
  21. a=n%10;
  22. A[i++]='0'+a;
  23. if(minus==1)
  24. A[i++]='-';
  25. A[i]=0;
  26. int len=i;
  27. int j=len-1;
  28. i=0;
  29. while(i
  30. {
  31. B[i]=A[len-1-i];
  32. i++;
  33. }
  34. B[i]=0;
  35. p=B;
  36. printf("%s,",p);
  37. return p;
  38. }
  39. void main()
  40. {
  41. int a=-234;
  42. char* p=swich(a);
  43. char b[10];
  44. strcpy(b,p);
  45. int i=0;
  46. printf("%s,",b);
  47. }
The execution result of the preceding program is as follows: In the swich function, 234 can be output normally. The output in main is garbled.
  
  
  1. for(int i=0;i<3;i++)
  2. {
  3. printf("%c",p[i]);
  4. }
Then only '2' can be correctly output, p [1], p [2] garbled.
Why? Before calling the function printf, you must press the form parameter on the stack. * p is calculated at this time. Therefore, the first printf statement has calculated the parameters and saved them on the top of the stack. Then call the printf function (the function call needs to use the stack to establish an access connection and control chain. The original function f is executed, and the original function f is at the top of the stack. Therefore, the stack space of function f is released. Array space is also released), printf occupies the stack, so the original function f stack space content is modified. Therefore, the first printf statement can obtain the result. Since the content of the arr space has been modified, subsequent printf statements cannot get the result.
By the way, I will explain why printf ("% s \ n", p); is garbled.
As mentioned above, calculate the value of parameter p to save the stack top. The saved value is the address of arr. Then call the printf function to modify the content of the top stack space. Although the address is saved, the original content has been modified, so no result is obtained.
Source: >

Related Article

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.