欢迎光临散文网 会员登陆 & 注册

对malloc、指针、free的理解

2022-06-23 01:34 作者:凫水亿  | 我要投稿

malloc函数声明如下

void* malloc (size_t size);

这是原文解释:

Allocate memory block

Allocates a block of size bytes of memory, returning a pointer to the beginning of the block.
The content of the newly allocated block of memory is not initialized, remaining with indeterminate values.
If size is zero, the return value depends on the particular library implementation (it may or may not be a null pointer), but the returned pointer shall not be dereferenced.

Parameters

    size

         Size of the memory block, in bytes.

        size_t is an unsigned integral type.

Return Value

    On success, a pointer to the memory block allocated by the function.
    The type of this pointer is always 
void*, which can be cast to the desired type of data     pointer in order to be     dereferenceable.
    If the function failed to allocate the requested block of memory, a null pointer is returned

Example

/* malloc example: random string generator*/
#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}

This program generates a string of the length specified by the user and fills it with alphabetic characters. The possible length of this string is only limited by the amount of memory available to malloc

Data races

Only the storage referenced by the returned pointer is modified. No other storage locations are accessed by the call.
If the function reuses the same unit of storage released by a deallocation function (such as free or realloc), the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.

Exceptions (C++)

No-throw guarantee: this function never throws exceptions.


up主将其翻译如下:

函数声明:

void* malloc (size_t size);

分配内存块

分配以字节为单位的内存块,返回指向该内存块的首地址。

分配新的内存块尚未初始化,残存不确定的值。

如果大小为0,则返回值为特殊库实现(可能也可能不是空指针(null pointer)),返回的指针将不是废弃的。

参数

内存块大小,单位为字节。

size_t为无符号整型。

返回值

分配内存成功:返回函数分配的内存块的首地址,该指针类型为void *,可强制转换为期望的数据类型。指针可被释放。

分配内存失败:返回空指针(null pointer)。

举例

/* malloc example: random string generator*/
#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}

此程序由使用者指定字符串的长度,以字母数字符填充。字符串的长度仅被分配内存块的长度决定。

数据竞争

Only(adv) the(定冠词) storage(adv) referenced(v) by the returned(v) pointer(n) is(v) modified(v).

->Only  the return pointer referenced storage  is modified.

只有返回指针引用的存储会被修改 ,调用不访问其他存储位置。 如果函数重用由回收函数(如free或realloc)释放的相同存储单元,那么函数将以这样一种方式同步,即回收完全发生在下一次分配之前。 (up水平有限,这一段太难翻译了)

异常

不保证抛出异常:函数永不抛出异常

举例

/* malloc example: random string generator*/
#include <stdio.h>      /* printf, scanf, NULL */
#include <stdlib.h>     /* malloc, free, rand */

int main ()
{
  int i,n;
  char * buffer;

  printf ("How long do you want the string? ");
  scanf ("%d", &i);

  buffer = (char*) malloc (i+1);
  if (buffer==NULL) exit (1);

  for (n=0; n<i; n++)
    buffer[n]=rand()%26+'a';
  buffer[i]='\0';

  printf ("Random string: %s\n",buffer);
  free (buffer);

  return 0;
}

指针与malloc

阅读原文得出:malloc()会分配以字节为单位的内存块,单位为字节,但分配的内存块尚未初始化,内存块经由free()函数释放,但这远远不够,查找资料后得出gcc与msvc实现有些许差异,up使用的Mingw-x64,malloc()会在堆上分配内存,手动通过free释放。

使用int *指针分配与释放内存


C语言的变量名、函数名本质上是 “符号” ,在链接的阶段将符号与各个目标文件对应的地址链接起来。

当使用指针指向分配的内存块时,malloc()不是给指针变量分配内存空间,而是分配一个内存块,供开发者使用。指针在函数调用中,指向了该内存块之外,此时的针不是指向被分配的内存块,如果使用free()释放指针指向的内存,读写权限不够,将发生段错误(segment fault)。

分配但不适用内存块

malloc与free总是成对出现,malloc在堆上分配内存,free释放malloc分配的内存。

free无法释放malloc分配以外的内存会触发段错误(segment fault)。


对malloc、指针、free的理解的评论 (共 条)

分享到微博请遵守国家法律