The Linux programming Interface modified Memory allocation allocates memory __linux

Source: Internet
Author: User

The Linux programming Interface

Memory allocation

(1) Content

This chapter describes the "functions" are used to allocate memory on the heap or the stack.

To allocate memory, C programs normally use the malloc family of functions, which we describe shortly. However, we begin with a description of BRK () and SBRK (), upon which the malloc, functions are.

#include <unistd.h>

int brk (void *end_data_segment);

The BRK () system call sets the "program break to" location specified by End_data_segment. Since virtual memory is allocated in units of pages, end_data_segment are effectively rounded up to the next page boundary.

On success, SBRK () returns the "previous address of the" program break.

The call SBRK (0) returns the "current setting" program break without changing it.

() malloc () function, allocating memory usage

The malloc function allocates size bytes from the heap and returns a pointer to the "start of the" newly allocated block of Memory.

#include <stdlib.h>

void *malloc (size_t size);

Because malloc () returns void *, we can assign it to any type of C pointer.

() free () Frees memory

void free (void *ptr);

The free function deallocates the "block of" memory pointed to through its PTR argument, which should is a address previously re Turned by malloc () or others.

(04) Show free Use cases

#include "tlpi_hdr.h" #define MAX_ALLOCS 1000000 int main (int argc, char *argv[]) {char *ptr[max_allocs];

	int Freestep, freemin, Freemax, BlockSize, Numallocs, J;

	printf ("\ n");

	if (argc < 3 | | strcmp (argv[1], "--help") = = 0) usageerr ("%s num-allocs block-size [step [min [max]]]\n", argv[0]);

	Numallocs = GetInt (argv[1], gn_gt_0, "Num-allocs");

	if (Numallocs > Max_allocs) cmdlineerr ("Num-allocs >%d\n", Max_allocs); BlockSize = GetInt (argv[2], GN_GT_0 |

	Gn_any_base, "block-size"); Freestep = (argc > 3)?
	GetInt (Argv[3], gn_gt_0, "Step"): 1; Freemin = (argc > 4)?
	GetInt (Argv[4], gn_gt_0, "min"): 1; Freemax = (argc > 5)?

	GetInt (Argv[5], gn_gt_0, "Max"): Numallocs;

	if (Freemax > Numallocs) cmdlineerr ("Free-max > num-allocs\n");

	printf ("Initial Program break:%10p\n", SBRK (0));

	printf ("Allocating%d *%d bytes\n", Numallocs, blockSize);
		for (j = 0; J < Numallocs; J +) {Ptr[j] = malloc (blockSize); if (ptr[j] = = NULL) errExit ("malloc");

	printf ("Program break are now:%10p\n", SBRK (0));
		
	printf ("Freeing blocks from%d to%d in steps of%d\n", Freemin, Freemax, freestep);

	for (j = freeMin-1 J < Freemax J + + Freestep) free (ptr[j));

	printf ("After free (), program break is:%10p\n", SBRK (0));
Exit (exit_success); }
Output:

wang@wang:~/test/tlpi-dist/lib$./FREE_AND_SBRK 1000 10240 2

Initial program break:0x19fa000
Allocating 1000 * 10240 bytes
Program Break is now:0x23c2000
Freeing blocks from 1 to 1000 in steps of 2
After free (), program break is:0x23c2000
wang@wang:~/test/tlpi-dist/lib$./FREE_AND_SBRK 1000 10240 1 1 999

Initial program break:0xdb6000
Allocating 1000 * 10240 bytes
Program Break is now:0x177e000
Freeing blocks from 1 to 999 in steps of 1
After free (), program break is:0x177e000

(a) malloc implementation

The implementation of malloc () is straightforward. It scans the list of memory blocks previously released by Free () in order to find one whose the size is larger than or E Qual to its requirement.

If It is larger, then it are split, so that a blocks of the correct size is returned to the caller and a smaller The free list.

IF no block on the free list are large enough, then malloc () calls SBRK () to allocate more memory.

(a) Calloc use

This is a example of the use of Calloc ():
struct {/* Some field definitions/} MyStruct;
struct MyStruct *p;

p = calloc (1000, sizeof (struct mystruct));
if (p = = NULL)
    errexit ("calloc");
(modified) ReAlloc function using

The ReAlloc () function is used to resize (usually enlarge) a blocks of memory previously allocated by one of the functions In the malloc package.

void *realloc (void *ptr, size_t size);

(a) Alloca ()

Instead of obtaining memory from the heap, Alloca () obtain memory to the stack by increasing the size of the stack frame .

void *alloca (size_t size);

(09) Summary

Using the malloc family of functions, a process can dynamically allocate and release memory on the heap. In considering the implementation of these functions, we saw this various things can go wrong in a program that mishandles The blocks of allocated memory, and we noted that a number of debugging tools are available to help locate the source of Such errors.

The Alloca () function allocates memory on the stack. This memory was automatically deallocated the function that calls Alloca () returns.

(10) Exercise


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.