Memory-related functions for C + +

Source: Internet
Author: User

The functions of the standard library of C and memory management are divided into two functions: one is the function of the system to memory operation, for example, the function that reads memory, write memory function, such as write a byte from the specified address to memory, a word or read one byte or one word to memory, and another is a function for the user to use memory dynamically. For example, assign a C program to a storage area and clear the area to 0 functions;

1. Dynamic Memory allocation function

If you want to design a C program, you need to read a set of data from a file and store it in an array. If you cannot know in advance the exact number of data in a file, or if the number of files changes when you run it again, you need to define the array sufficiently large in your program to accommodate the above situation. But doing so often leads to a waste of memory space. Allocating memory dynamically allows the program to obtain the required memory space during execution to make more efficient use of memory space. Now the commonly used memory management functions and their use are described below:

(1) calloc () function

Function
The function is assigned to the C program as a block of storage and the store is eliminated by 0. The program needs to refer to two parameters when calling the function, indicating the number of objects allocated to the program and the number of bytes of memory space required for each object. The Calloc function returns a character pointer to an inner store, pointing to the starting position of the allocated memory area. Returns NULL if the function call fails. Call Format calloc (number,size);
Description
After calling this function, the function returns a pointer to the starting position of the space, which can occupy a continuous memory space of size character length starting from the memory address number.
When the memory space is not allocated, calloc () returns a null pointer. Therefore, calloc () is a function that returns a pointer to a character. That
Char *calloc ();
Where number and size are unsigned integers, i.e.
unsigned int number size;
Application examples
Apply the Calloc () function to request a 255-byte store from the heap and input an output string to that area, which can be written as
The following program implementation:

#include #defint N 255#define S 1main () extern char *calloc () extern char *fgets (), extern int fputs (), Free (), Char *buffer;bu Ffer=calloc (N,s), if (!buffer) abort ("Ug,too big \ n"), fputs ("Enter data follwed by ctrl-z\n", stdout), and while (fgets (buffer , N,sldin)) fputs (buffer,stdout); free (buffer);}

(2) malloc () function

Call format
malloc (Size);
Function
After the function is called, it can occupy a contiguous memory space of size characters in the pre-specified memory area, which allows the program to allocate memory as needed, and allocates exactly the desired size.
Description
If the function call succeeds, returns the address of the first byte of the size space, and when the memory space is not allocated, returns
Null. It is also a pointer function that returns a value to a character, i.e.:
Char *malloc ();
Size is also an unsigned number, i.e.;
unsigned int size;
The difference between malloc () and Calloc () is that malloc () can only occupy a contiguous space of size characters in the specified memory space, while Calloc () occupies a continuous space of size character length in any memory space, The starting position of this memory space is specified temporarily when the function calloc () is called.
Application examples
Use the malloc () function to allocate 255 bytes of storage from the heap and clear the zone.
The following program implementations can be written:

#includemain () {extern char *malloc (); extern int free (); Char *buf; unsigned number-bytes;int i;number-bytes=255;buf= malloc (number-bytes);p rintf ("%s\n", buf), for (I=0;J * (buf+j) =eos;printf ("%s\n", buf); free (BUF);}}

(3) Free () function

Call format
free (pointer);
Function
This function frees the storage space allocated by the calloc () or malloc () function for the next reallocation to increase the efficiency of memory resource usage.
Description
The call to the function free () requires a pointer parameter that specifies the memory space to be freed, which should be a character pointer in the program.
That
Char *pointer;
If the release succeeds, the function free (pointer) returns a value of zero: When the pointer argument is an invalid pointer, the return value is-
1.

(4) Rcalloc () function

Call format
Rcalloc (pointersize);
Function
This function is used to change the size of the memory area occupied by CALLC () or malloc () and will be calloc () or malloc ()
The specified size number is changed to Rcalloc () specifies the size specified by size.
Description
The function call successfully returns the address pointed to by the pointer pointer variable, and the return value is 0 when the memory space is not allocated. Release with free () or cfree () when there is no memory space to occupy.

2. Memory read-write function (1) Peek () function

Function
Reads a word from the memory-specified segment offset address cell.
Format
int peek (unsigned segment,unsigned offset);
Description
The Peek () function returns the contents of a word on the corresponding address.
The memory addresses are in the format of the segment address: offset address. For example, 0070:0000 is the absolute address X ' 0700 '.
Application examples
The peek () function is used to read the contents of the 40:13 unit (a word of this unit records the total system memory capacity, in kilobytes) and is displayed on the screen. The following program implementations can be written:

#include #include#define SEGMENT 0x0040#define OFFSET 0x0013main () {int Value=0;value=peek (segment,offset);p rintf ("\ n The RAM is%d K. ", value); GetChar (); exit (0);}

(2) peekb () function

The function reads a byte from the memory-specified segment: offset.
Format Char peekb (unsigned segment,unsigned offset);
Description the PEEKB () function returns the contents of a byte on the corresponding address.
The application instance reads a byte at the 0x40:0x11 and analyzes the configuration of its hardware device. The following program implementations can be written:

#include #include#icludemain () {char Value=0;char va;printf ("\ nthe hardware configuration of the current Machine"); value=peekb (0x40,0x11); if (value & 1); printf ("\ n DMA chip is installed"), elseprintf ("\ n DMA chip not installed"), if (value &);p rintf ("\ n Game card already installed"), if (value &);p rintf (" \ n Serial printer Installed "); va=value>>6;printf (" \ n the number of printer installations is%d,va);}

(3) Number of poke ()

function to write a word in memory.
format void poke (unsigned segment,unsigned offset,int value);
Description where segment is the segment address to write memory to. Offset is the amount of offsets to write to memory. Value is the values to write to memory.
Since the poke () function has the ability to modify memory data, it is important to note that if the operation is wrong, the result is often a panic. This type of error occurs because the specified segment address, the value of the offset address is incorrect or not given the initial value, and then there is the value of the write is not recognized by the system.

(4) poded () function

function to write a byte in memory.
format void poke (unsigned segment,unsigned offset,char value);
Description where segment is the segment address to write memory, offset is the amount of memory to write to. Value is to be written to the inner
The value that is stored.
Application examples
Start writing memory 5-word data from the segment address 100H, and then read by Word by character. The following program implementations can be written:

#includemain () {extern int peek (); extern int poke (); unsigned offset,segment;int word=0x0000;int i;segment=0x100; Putchar ("/n"); for (offset=0;offset<10;offset++) poked (offset,segment word++); for (Offset=0;offset<10;offset) {Word=peek (offset,segment);p rintf ("Peek (%XH,%XH) =%xh\n", offset Segment,word); offset++=2}}
The four functions of poke (), Pokeb (), Peek (), peekb () are used synthetically to control the software resources of the operating system.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Memory-related functions for C + +

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.