The correlation function of allocating memory space in C language programming _c language

Source: Internet
Author: User

C language malloc () function: allocating memory space dynamically
header file:

#include <stdlib.h>

malloc () function is used to dynamically allocate memory space (if you do not understand dynamic memory allocation, see: C Language Dynamic memory allocation and variable storage categories), the prototype is:

void* malloc (size_t size);

The "parameter description" size is the size of the memory space that needs to be allocated, in bytes (byte).

The function Description malloc () allocates a specified amount of memory space in the heap area for storing data. This memory space is not initialized after the function execution completes, and their values are unknown. If you want to initialize while allocating memory, use the Calloc () function.

The return value assignment successfully returns an address to that memory and returns NULL if it fails.

Because there may or may not be an application for memory space, it is necessary to determine whether the application is successful and then follow up.

If the value of size is 0, the return value will vary depending on the standard library implementation, may be NULL or not, but the returned pointer should not be referenced again.

Note: The return value type of the function is void *,void does not mean that there is no return value or null pointer returned, but the type of pointer returned is unknown. So when you use malloc (), you typically need to cast a mandatory type to convert the void pointer to the type we want, for example:

Char *ptr = (char *) malloc (10); Allocates 10 bytes of memory space to hold characters

Dynamic memory allocation Examples:

#include <stdio.h>/* printf, scanf, NULL/
#include <stdlib.h>/* malloc, free, RAND, System */
INT Main ()
{
 int i,n;
 char * buffer;
 printf ("Length of input string:");
 scanf ("%d", &i);
 Buffer = (char*) malloc (i+1); The string finally contains the "if" (Buffer==null) exit (1);///////////////////////////
 randomly generated string for
 (n=0 n<i; n++)
  Buffer[n] ()%26+ ' a ';
 Buffer[i]= ' ";
 printf ("Randomly generated string:%s\n", buffer);
 Free (buffer); Free memory Space
 system ("pause");
 return 0;
}

Run Result:

The length of the input string: The
randomly generated string is: Phqghumeaylnlfdxfirc

C language Calloc () function: Allocate memory space and initialize
header file:

#include <stdlib.h>

The Calloc () function is used to dynamically allocate memory space and initialize to 0, which is modeled as:

 void* calloc (size_t num, size_t size);

Calloc () dynamically allocates num contiguous space of size in memory and initializes each byte to 0. So it's the result of allocating the num*size byte-length memory space, and the value of each byte is 0.

The return value assignment successfully returns an address to that memory and returns NULL if it fails.

If the value of size is 0, the return value will vary depending on the standard library implementation, may be NULL or not, but the returned pointer should not be referenced again.

Note: The return value type of the function is void *,void does not mean that there is no return value or null pointer returned, but the type of pointer returned is unknown. So when you use Calloc (), you typically need to cast a mandatory type to convert the void pointer to the type we want, for example:

Char *ptr = (char *) calloc (10, 10); Allocate 100 bytes of memory space

An important difference between calloc () and malloc () is that calloc () automatically initializes the memory space to zero after the dynamic allocation of memory, while malloc () is not initialized and the inside data is unknown garbage data. The following two ways of writing are equivalent:

Calloc () Allocates the memory space and initializes
the char *str1 = (char *) calloc (2);
malloc () Allocates the memory space and initializes the
char *str2 = (char *) malloc () with memset ();
memset (str2, 0, 20);

code example:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
 int i,n;
 int * PDATA;
 printf ("Number of digits to enter:");
 scanf ("%d", &i);
 PData = (int*) calloc (i,sizeof (int));
 if (Pdata==null) exit (1);
 for (n=0;n<i;n++)
 {
  printf ("Please enter a number #%d:", n+1);
  scanf ("%d", &pdata[n]);
 printf ("The number you entered is:");
 for (n=0;n<i;n++) printf ("%d", Pdata[n]);
 
 Free (pData);
 System ("pause");
 return 0;
}

Run Result:

Number of digits to enter: 4 Enter the number
#1:126 Please enter a number of
#2:343 Please enter
a number #3:
234 Enter a number #4:
The number you entered is: 126 343 45 234

The above program stores the numbers you enter and outputs them. Because when the program is running, you can dynamically allocate the memory according to your needs, so each time you run the program you may enter a different number.

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.