C language Dynamic Storage Allocation

Source: Internet
Author: User

Dynamic Storage Allocation

The C language supports dynamic storage allocation, that is, the ability to allocate memory units during program execution. with dynamic storage allocation, you can design an extended (or reduced) data structure as needed, although it can be applied to all types of data, dynamic storage allocation is more commonly used for strings, arrays, and struct

Address: Workshop.

1. Memory Allocation Function

The three memory allocation functions are declared in <stdlib. h>:

  • Malloc function-allocates memory blocks, but does not initialize memory blocks.
  • Calloc function: allocates memory blocks and removes memory blocks.
  • Realloc function -- adjust previously allocated memory blocks

The malloc function does not need to clear the allocated memory quickly, so it is more efficient than the calloc function.

When you call the memory allocation function when applying for a memory block, the function returns the void * type value. The size of the object space in the memory is calculated by the number of bytes. Many header files define the size_t type, which is used to save the information related to this memory space, for example, the sizeof operator returns the number of bytes. The type is size_t.

2. NULL pointer

Since NULL macro is used to represent a NULL pointer, the following methods are often used to test the return value of the malloc function:

P = malloc (10000); if (p = NULL) {/* allocation failed */}

In the C language, the method for testing true and false pointers is the same as that for testing data:

if(p==NULL)if(!p)if(p!=NULL)if(p)

Example:

typedef struct {    long key;    /*...*/}Record;float *myFunc(size_t n){    double *dptr=malloc(sizeof(double));    if(dptr==NULL)    {        /*...*/        return NULL:    }    else    {        *dptr=0.07;    }    /*...*/    Record *rptr;    if(rptr=malloc(2*sizeof(Record))==NULL)    {        /*...*/        return NULL;    }    float *fptr=malloc(n*sizeof(float));    if(fptr==NULL)    {        /*...*/        return NULL:    }    /*...*/    return fptr;}
Dynamic Array allocation

The malloc function is used to allocate storage space for arrays. the sizeof operator is used to calculate the amount of space required for each element.

int *a;a=malloc(n * sizeof(int));

Calloc Function

The calloc function has the following prototype in <stdlib. h>:

Void * calloc (size_t nmemb, size_t size );

After memory is allocated, the calloc function initializes all bits by setting them to 0.

a=calloc(n, sizeof(int))struct point{int x,y;}*p;p=calloc(1,sizeof(struct point));

Realloc Function

The realloc function can adjust the array size to make it more suitable. The realloc function prototype is in <stdlib. h>:

Void * realloc (void * ptr, size_t size)

The memory block pointed by ptr must have been previously obtained through calling the malloc function, calloc function, or realloc function. size indicates the new size of the memory block.

The C language standard lists several rules about realloc functions:

  • When the memory block is extended, the realloc function does not initialize itself added to the memory block.
  • If the realloc function cannot expand the memory block as required, it will return a null pointer and the data in the original memory block will not change.
  • If the realloc function is called with a null pointer as the first actual parameter, it acts like the malloc function.
  • If the realloc function is called with 0 as the second actual parameter, it will release the memory block.
Release Storage

The memory block obtained by the memory allocation function comes from a storage pool called heap.

p=malloc(...)q=malloc(...)p=q;

The above code points p and q to the same memory, leading to memory leakage. The free function is used to release unnecessary memory to recycle garbage.

The free function has the following prototype in <stdlib. h>:

Void free (void * ptr)

The above code is rewritten as follows:

p=malloc(...)q=malloc(...)free(p);p=q;

 

 

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.