Program --- C language details 22 (Precautions for returning the pointer to the function (floating pointer), view the memory size that can be allocated by the process)

Source: Internet
Author: User

Program --- C language details 22 (Precautions for returning the pointer to the function (floating pointer), view the memory size that can be allocated by the process)
Main Content: Precautions for returning pointers to Functions <Floating pointer> View the memory size that a process can allocate

# Include

 
  
Char * favorite_fruit () {static char fruit [] = "apple"; // If static is not added, the pointer returned by this function is left blank, because when the function exits, the number of fruit groups is destroyed // After static is added, the fruit array is allocated in the Data Segment, instead of in the stack. The life cycle is as long as that of the program, return fruit;} int main () {char * a = favorite_fruit (); printf ("% s \ n", ); /** view how much memory your process can allocate * The total amount of memory allocated depends on the System Configuration */int MB = 0; while (malloc (1 <10 )) + KB; printf ("Allocated % d KB total \ n", KB); return 0 ;}

 

Output:


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.