C language beginners-memory space problems

Source: Internet
Author: User

[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
Void main (){
Float * p = (float *) malloc (sizeof (float ));
// Apply for a space. If the request succeeds, return the space address. If the request fails, return NULL.
* P = 10.0; // value assignment
Printf ("memory value = % f \ n", * p );
Printf ("memory address = % p \ n", p );
Free (p); // release space
Printf ("value in memory after release = % d \ n", * p );
Printf ("memory address after release = % p \ n", p );
// After the space is released, the memory space does not disappear and the address of the space still exists;
// The system is handed over to other programs for use. If you use this space address again,
// It will destroy the data in it
// The second release is required at this time:
// Free (p );
// But this will cause errors. To avoid errors, remember to perform the following operations:
P = NULL;
Printf ("memory address after clearing = % p \ n", p );
}

Result:
Memory value = 10.000000
Memory Address = 00540F08
Value in memory after release =-1610612736
Memory Address after release = 00540F08
Memory Address after clearing = 00000000
Press any key to continue


From like7xiaoben

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.