glibc 中内存操作函数总结

1.malloc

malloc() allocates size bytes and returns a pointer to the allocated memory.   The
memory  is  not  cleared.   If  size is 0, then malloc() returns either NULL, or a
unique pointer value that can later be successfully passed to free().

2.realloc

realloc()  changes  the  size of the memory block pointed to by ptr to size bytes.
The contents will be unchanged to the minimum of the  old  and  new  sizes;  newly
allocated  memory will be uninitialized.  If ptr is NULL, then the call is equiva-
lent to malloc(size), for all values of size; if size is equal to zero, and ptr is
not  NULL,  then the call is equivalent to free(ptr).  Unless ptr is NULL, it must
have been returned by an earlier call to malloc(), calloc() or realloc().  If  the
area pointed to was moved, a free(ptr) is done.

3.calloc

calloc()  allocates  memory  for an array of nmemb elements of size bytes each and
returns a pointer to the allocated memory.  The memory is set to zero.   If  nmemb
or  size  is  0, then calloc() returns either NULL, or a unique pointer value that
can later be successfully passed to free().

4.memalign

The obsolete function memalign() allocates size bytes and returns a pointer to the
allocated memory.  The memory address will be a multiple of boundary,  which  must
be a power of two.

5.valloc

The  obsolete  function valloc() allocates size bytes and returns a pointer to the
allocated memory.  The memory address will be a multiple of the page size.  It  is
equivalent to memalign(sysconf(_SC_PAGESIZE),size).

6.brk/sbrk

brk()  and  sbrk() change the location of the program break, which defines the end
of the process's data segment (i.e., the program break is the first location after
the  end of the uninitialized data segment).  Increasing the program break has the
effect of allocating memory to the process; decreasing the break deallocates  mem-
ory.

brk()  sets  the end of the data segment to the value specified by addr, when that
value is reasonable, the system has enough memory, and the process does not exceed
its maximum data size (see setrlimit(2)).

sbrk()  increments  the  program's  data space by increment bytes.  Calling sbrk()
with an increment of 0 can be used to find the current  location  of  the  program
break.

7.mmap

void *mmap(void *addr, size_t length, int prot, int flags,int fd, off_t offset);
int munmap(void *addr, size_t length);

mmap()  creates a new mapping in the virtual address space of the calling process.
The starting address for the new mapping is specified in addr.  The  length  argu-
ment specifies the length of the mapping.

If  addr  is NULL, then the kernel chooses the address at which to create the map-
ping; this is the most portable method of creating a new mapping.  If addr is  not
NULL,  then  the  kernel  takes  it as a hint about where to place the mapping; on
Linux, the mapping will be created at a nearby page boundary.  The address of  the
new mapping is returned as the result of the call.

8.alloca

The  alloca()  function  allocates  size  bytes of space in the stack frame of the
caller.  This temporary space is automatically freed when the function that called
alloca() returns to its caller.

9.free

free() frees the memory space pointed to by ptr, which must have been returned  by
a  previous  call  to malloc(), calloc() or realloc().  Otherwise, or if free(ptr)
has already been called before, undefined behavior occurs.  If  ptr  is  NULL,  no
operation is performed.

 

(To be continued)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值