"Go" C Memory Operation function

Source: Internet
Author: User

First, Malloc/calloc

Name: Malloc/calloc
Function: Dynamic Memory allocation function
Header file: #include <stdlib.h>
Function prototype: void *malloc (size_t size);
void *calloc (size_t num,size_t size);
Parameters: Size allocates memory block sizes
Num allocates the number of memory blocks
return value: The first address of the allocated memory block was returned successfully, and null was returned by failure.

Both malloc and calloc can allocate memory areas, but malloc can only request one memory area at a time, and calloc may request multiple memory areas at a time. In addition, Calloc will initialize the allocated memory area to 0,malloc.

#include <stdio.h>
#include <stdlib.h>
Main ()
{
int *p=null;
p= (int *) malloc (sizeof (int));
if (p==null)
{
printf ("malloc error/n");
Exit (1);
}
*p=3;
printf ("%d/n", *p);
Free (p);
}

Second, Malloc/calloc

Name: Free
Function: Dynamic memory deallocation function
Header file: #include <stdlib.h>
Function prototype: void free (void *ptr);
Parameters: PTR uses memory pointers returned by memory allocation functions such as malloc or calloc
return value: No

Free can release memory allocated by memory allocation functions such as malloc or calloc. When the program is very large, the period of time may have to dynamically allocate memory, if not released in time, the program will occupy a lot of memory.

It is important to note that if PTR refers to memory being freed or unknown memory address, there may be unexpected occurrences. If the argument is null, then free does not have any effect.

Third, memset

Name: Memset
Function: Initialize the specified memory space
Header file: #include <stdlib.h>
Function prototype: void *memset (void *buffer,int c,int count);
Parameters: Memory allocated by buffer
C Initialize Content
Count number of bytes initialized
return value: Returns a pointer to buffer

Memset sets the first count bytes of the memory area referred to as buffer to the ASCLL value of a character. Typically used to assign values to arrays, strings, and so on.

Main ()
{
int *p=null;
int i;
Char *q=null;

p= (int *) malloc (sizeof (int) *10);
if (p==null)
Exit (1);
memset (p,0,sizeof (int) *10);
Q=p;
for (i=0;i<10;i++)
printf ("%d", * (q++));
Free (p);

The result of the execution is 10 of 0.

Iv. memcpy

Name: memcpy
Function: Copy memory space
Header file: #include <stdlib.h>
Function prototype: void *memcpy (void *dest,void *src,unsigned int count);
Parameters: Dest Target Memory Area
SRC Original Memory Area
Count of bytes to copy
return value: Pointer to Dest

The memcpy will copy the memory area of SRC to count bytes to the memory area referred to by Dest. If count is larger than the SRC byte number, strcpy will copy '/0 ' after the end. Be aware that dest and SRC do not overlap.

memcpy only copies memory space and does not deal with overlapping space issues.

Main ()
{
int *p1=null;
int *p2=null;
int q;
int i;

P1=malloc (sizeof (int) *10);
if (p1==null)
Exit (1);
P2=malloc (sizeof (int.));
if (p2==null)
Exit (1);
memset (p1,0,sizeof (int) *10);
memcpy (p2,p1,sizeof (int));
Q=P2;
for (i=0;i<5;i++)
printf ("%d", * (q++));

Free (p1);
Free (p2);
)

The result of the operation is 5 x 0.

Wu, Memmove

Name: Memmove
Function: Copy (move) memory space
Header file: #include <stdlib.h>
Function prototype: void *memmove (void *dest,void *src,unsigned int count);
Parameters: Dest Target Memory Area
SRC Original Memory Area
Count of bytes to copy
return value: Pointer to Dest

Memmove functions like the function memcpy function, but only copies memory space and does not deal with space overlap issues. Memmove will deal with spatial overlap issues. When dest and src overlap, they can still be handled correctly, but the SRC content changes.

Liu, Memmove

Name: memcmp
Function: Compare characters in two memory spaces
Header file: #include <stdlib.h>
Function prototype: int memcmp (void *buf1,void *buf2,unsigned int count);
Parameters: BUF1 Memory Area
Buf2 Memory Area
Count the number of characters to compare
return value: See below

MEMCMP compares the first count bytes of the memory area BUF1 and BUF2. The memcmp is then compared according to the ASCLL Code table order. When BUF1<BUF2, returns <0; returns 0 when Buf1=buf2, and returns >0 when Buf1>buf2.

Main ()
{
int *p1=null;
int *p2=null;
int RT;

P1=malloc (sizeof (int) *10);
if (p1==null)
Exit (1);
P2=malloc (sizeof (int) *10);
if (p2==null)
Exit (1);
memset (P1, ' a ', sizeof (int) *10);
memset (P2, ' B ', sizeof (int) *10);
RT=MEMCMP (p1,p2,sizeof (int) *10);
if (rt>0)
printf ("P1>p2");
if (rt<0)
printf ("P1<p2");
if (rt==0)
printf ("P1=p2");

Free (p1);
Free (p2);
}

Running Result: P1<P2

"Go" C Memory Operation function

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.