C-Language memory model

Source: Internet
Author: User

The memory allocation model of C language in article I.

1, program code area: the binary code that holds the function body.

2. Global Data area: The global partition is divided into three regions. The storage of global variables and static variables is placed in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another contiguous area. Constant data is stored in another area. This data is released by the system at the end of the program. What we call a BSS segment (BSS segment) usually refers to an area of memory that is used to store uninitialized global variables in the program. BSS is the abbreviation for English block Started by symbol.

3, the Stack area: The compiler automatically allocates the release, the stored function parameter value, the local variable value and so on. It operates in a manner similar to a stack in a data structure.

4, heap area: Generally by the programmer allocation release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the heap in the data structure, the distribution is similar to the list, hehe.

5. Command line parameter area: Store the values of command line arguments and environment variables.

The local string constants are stored in the global constant or stack area, and different compilers have different implementations. Can be viewed through assembly language. However, in the VC environment, local constants are stored in the stack like local variables, and global constants and character constants are stored in the literal constant area. TC in the constant area.

Under Linux: The build assembly file can be compiled with the parameter-C. Such as:
Gcc-c *.c
GCC *.o-map test.txt-o test.elf
To view the Test.txt file with a text editor, you will see information such as BSS, data, text, and so on, but no information about the stack segment, and use the Objdump command to view the post-disassembly information of the. o file, or use the Gcc-s *.c to view each. s file is clear.

Article II, the type of memory planning

1) In general (conventional memory) occupies the first position in the allocation table, from 0KB to 640KB (address 000000H~109FFFFH), a total of 640KB of capacity. Because it is in the front of the memory and in the Dos manageable memory area, we also call it low DOS memory (lower DOS RAM), or called Basic memory, the program that uses this space has BIOS, Dos operating system, peripheral device driver, interrupt vector table, Some resident programs, free memory space, and general application software can be executed in this space.

2) high-level memory (UM) is an abbreviation for the English upper memory, which is a layer of memory (640KB~1024KB) above the conventional RAM.

3) The high-end memory Area (HMA) is an abbreviation for the English version. It is 64KB memory between 1024KB to 1088KB, the tube is high-end memory area, its address is 100000H~10FFEFH or above, the CPU is addressed in the mode of Segment:offset (segment address: offset) in the field. The maximum logical memory space for its addressing is (FFFF:FFFF), which is 10FFEFH.

4) The EMB is an abbreviation for the English extended memory block (the extended RAM), and the early-used expansion memory (EPM) must follow the EMS specification (e.g. using EMM386.exe), Later use of the extended memory (EXM) must follow the XMS specification (e.g., using Himem.sys). Extended memory refers to more than 1MB of memory space, the address is starting from 100000H, continuous upward expansion of memory, the expansion of memory depends on the CPU's addressing capability.

Article III, general memory functions

Application function: malloc (), Farmalloc (), Calloc (), Farcalloc (), realloc (), Farealloc ()
Function Name: malloc
Function: Memory allocation function
Usage: void *malloc (unsigned size);


Function Name: Farmalloc
Function: Allocating storage blocks from the far heap
Usage: void far *farmalloc (unsigned long size);


Function Name: calloc
Function: Allocate primary memory
Usage: void *calloc (size_t nelem, size_t elsize);


Function Name: Farcalloc
Function: Request space from the far stack
Usage: void far *farcalloc (unsigned long units, unsigned ling Unitsz);


Function Name: realloc
Function: Re-allocating main memory
Usage: void *realloc (void *ptr, unsigned newsize);


Function Name: Farrealloc
Function: Adjust the allocation block in the far heap
Usage: void far *farrealloc (void far *block, unsigned long newsize);


Use function: memcpy (), memset (), Memmove (), Movedata ()


Function Name: memcpy

function: Copy n bytes from source to target Destin

Usage: void *memcpy (void *destin,void *source,unsigned N);


Function Name: memset

Function: Sets all the bytes in S to CH, the size of s array is given by n

Usage: void *memset (void *s, char ch, unsigned n);


Function Name: memmove

Function: Move a piece of bytes

Usage: void *memmove (void *destin,void *source,unsigned N);


Function Name: Movedata

Function: Copy bytes

Usage: void movedata (int segsrc, int offsrc, int segdest, int offdest, unsigned numbytes);


Release function: Free (), Farfree ()


Function Name: Free

Function: Frees the allocated block

Usage: void free (void *ptr);

Function Name: Farfree

Function: Release a piece from the far heap

Usage: void Farfree (void);


Pointer manipulation functions: MK_FP (), Fp_off (), fp_seg ()


Function Name: MK_FP
Function: Set a far pointer
Usage: void far *mk_fp (unsigned seg, unsigned off);


Function Name: Fp_off
Function: Get the FAR address offset
Usage: unsigned fp_off (void far *farptr);


Function name: fp_seg
Function: Get the long address segment value
Usage: unsigned fp_seg (void far *farptr);

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.