C Review---Dynamic memory allocation

Source: Internet
Author: User

Prototype
extern void *malloc (unsigned int num_bytes);
Header file
#include <stdlib.h>
#include <malloc.h>
function declaration
void *malloc (size_t size);
Note: void* represents an undefined type of pointer, void * can point to any type of data, more specifically refers to the application of memory space when the user is not aware of the space to store what type of data (than
such as char or int or other data type). Therefore, in the use of the general to do a forced type conversion.
Function
Allocating a block of memory with a length of num_bytes bytes
return value
If the assignment succeeds, returns a pointer to the allocated memory (the initial value in this store is indeterminate), otherwise the null pointer is returned NULL. Use the free () function to release memory blocks when memory is no longer in use. function return
The back pointer must be properly aligned so that it can be used with any data object.

Function Name: Free
Function: Used in pairing with the malloc () function to release the dynamic memory requested by the malloc function. (another: For the free (p) statement, if p is a null pointer, then no for P no matter how many times the operation
There will be problems. If p is not a null pointer, then the free operation of P for two consecutive times causes the program to run incorrectly. )
Usage: void free (void *ptr); (PTR is typically the space address assigned by malloc)

Null pointer
The null pointer is an untyped pointer and has a value of 0. A pointer that has 2 properties--Address value and pointer type. The function is: 1, the address value identifies the first address of the variable (32-bit system the address of the pointer
The value is 4 bytes); 2, the pointer type tells the compiler how the next data access should be done (the int array and the float array are different; array access is different from struct access; etc.).
The advent of NULL is a rule, in fact it is not a keyword in C; Assigning a pointer to null is often said to be "dangling the pointer". This way, the pointer can no longer make any data access
The There is a relatively easy-to-use error in programming, where the pointer address is not properly updated to be used, which can often result in very serious consequences (improper smear of the memory area). So a good
The good habit is that when a pointer works on a break, it is assigned a value of NULL, and then re-assigned and the pointer type is converted when re-used

The character "NULL"
The first character in the ASCII code is NULL, and its value is 0, which takes up 1 bytes; The C language takes it as the last character of the string to represent the end of the string. " ".

C Review---Dynamic memory allocation

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.