c语言分层理解(动态内存分配)

简介: 1. 为什么存在动态内存分配?与数组相比:数组空间大小是固定的,但是要完成可变内存空间的变化就要用动态内存。数组在声明的时候,必须指定数组的长度,它所需要的内存在编译时分配。动态内存分配不要指定长度,用的时候分配多少空间。

1. 为什么存在动态内存分配

与数组相比:

  1. 数组空间大小是固定的,但是要完成可变内存空间的变化就要用动态内存。
  2. 数组在声明的时候,必须指定数组的长度,它所需要的内存在编译时分配。动态内存分配不要指定长度,用的时候分配多少空间。

2. 动态内存库函数

2.1 malloc库函数

void* malloc (size_t size);

作用:向内存申请一块连续可用的空间,并返回指向这块空间的指针。

2.1.1 注意要点

1.如果开辟成功,则返回一个指向开辟好空间的指针。

2.如果开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。所以在使用时候一定要判断开辟空间返回值是否为空指针。

3.返回值的类型是void*,所以malloc函数并不知道开辟空间的类型具体在使用的时候使用者自己来决定。

4.如果参数size为0,malloc的行为是标准是未定义的,取决于编译器

2.2 free库函数

void free (void* ptr);

作用:用来释放动态开辟的内存。

2.2.1 注意要点

1.如果参数ptr指向的空间不是动态开辟的,那free函数的行为是未定义的

2.如果参数ptr是NULL指针,则函数什么事都不做。

2.2.2 使用

#include <stdlib.h>
int main()
{
  int arr[10] = { 0 };
  int* ptr = NULL;
  ptr = (int*)malloc(10 * sizeof(int));
  //判断ptr指针是否为空(一定要做的工作)
  if (NULL != ptr)
  {
    int i = 0;
    for (i = 0; i < 10; i++)
    {
      *(ptr + i) = 0;
    }
  }
  free(ptr);
  //这里ptr空间被释放,但是这个指针还是被占用,为了后面指针的使用,
  //把这里返回的指针置为空指针,防止后面使用的指针和这个指针发生冲突
  ptr = NULL;
  return 0;
}

2.3 calloc库函数

void* calloc (size_t num, size_t size);

作用:为 num 个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。(这里的num代表的是元素个数,这里的size代表的是一个元素的大小)

2.3.1 malloc和calloc库函数的区别

907202d0fdaa71bf56b4911f5d2aa220.png

上图可以看出calloc库函数开辟空间会初始化,但是malloc不会。另外malloc和calloc库函数函数参数也不同,malloc直接申请空间,calloc设置空间并设置元素大小。

2.4 realloc库函数

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

作用:灵活调整申请空间的大小

2.4.1 注意要点

1.指向先前用malloc、calloc或realloc分配的内存块的指针。或者,它可以是一个空指针,在这种情况下分配一个新的块(就像调用malloc一样)。size 调整之后新大小。

2.返回值为调整之后的内存起始位置

3.这个函数调整原内存空间大小的基础上,可能还会将原来内存中的数据移动到 新 的空间。

2.4.2 realloc调整内存空间的的两种请况

2.4.2.1 情况一

b1c397488d90643df7295443f89c6c14.png

要扩展内存就直接原有内存之后直接追加空间,原来空间的数据不发生变化。

2.4.2.2 情况二

b69207e1e35f26890dd654686a0a8667.png

原有空间之后没有足够多的空间时,扩展的方法是:在堆空间上另找一个合适大小的连续空间来使用。这样函数返回的是一个新的内存地址。

2.5 realloc库函数使用

#include <stdio.h>
#include <stdlib.h>
int main()
{
  int* p = (int*)malloc(40);
  if (p == NULL)
    return 1;
  int i = 0;
  for (i = 0; i < 10; i++)
  {
    *(p + i) = i;
  }
  for (i = 0; i < 10; i++)
  {
    printf("%d ", *(p + i));
  }
  printf("\n");
  //增加空间
  //注意这里第一个参数必须是动态内存的指针,不能是固定大小数组名
  int* ptr = (int*)realloc(p, 80);
  if (ptr != NULL)
  {
    p = ptr;
    ptr = NULL;
  }
  for (i = 10; i < 20; i++)
  {
    *(p + i) = i;
  }
  for (i = 0; i < 20; i++)
  {
    printf("%d ", *(p + i));
  }
  free(p);
  p = NULL;
  return 0;
}

3. 常见的动态内存错误

3.1 对NULL指针的解引用

错误代码展示:

#include <stdlib.h>
#include <limits.h>
int main()
{
  int* p = (int*)malloc(INT_MAX);
  *p = 10;
  free(p);//p为空指针,不能对空指针free
  p = NULL;
  return 0;
}

3.2 对动态开辟空间的越界访问

错误代码展示:

#include <stdlib.h>
#include <string.h>
int main()
{
  int* p = (int*)malloc(sizeof(int) * 5);
  if (p == NULL)
  {
    perror("malloc:");
  }
  int i = 0;
  for (i = 0; i < 10; i++)
  {
   //动态申请的大小是5个整形,这里访问10个,产生越界
    *(p + i) = i;
  }
  free(p);
  p = NULL;
  return 0;
}

3.3 对非动态开辟内存使用free释放

错误代码展示:

#include <stdlib.h>
int main()
{
  int a = 10;
  int* p = &a;
  free(p);//释放非动态内存
  return 0;
}

3.4 使用free释放一块动态开辟内存的一部分

错误代码展示:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
  int* p = (int*)malloc(sizeof(int) * 10);
  if (p == NULL)
  {
    printf("%s\n",strerror(errno));
  }
  else
  {
    int i = 0;
    for (i = 0; i < 5; i++)
    {
      *p = i;
      p++;//不是初始位置
    }
  }
  free(p);//error
  p = NULL;
  return 0;
}

3.5 对同一块动态内存多次释放

错误代码展示:

#include <stdlib.h>
int main()
{
  int* p = (int*)malloc(sizeof(int) * 5);
  free(p);
  free(p);//不能多次释放
  p = NULL;
  return 0;
}

3.6 动态开辟内存忘记释放(内存泄漏)

错误代码展示:

#include <stdlib.h>
#include <stdio.h>
int main()
{
  int* p = (int*)malloc(sizeof(int) * 20);
  if (p == NULL)
  {
    printf("%s\n", strerror(errno));
  }
  else
  {
    int i = 0;
    for (i = 0; i < 20; i++)
    {
      *(p + i) = i;
    }
  }
  //没有进行释放操作导致内存泄漏
  return 0;
}

4. 几个经典的笔试题

4.1 实例一

4.1.1 源代码

#include <stdlib.h>
void GetMemory(char* p)
{
  p = (char*)malloc(100);
}
void Test(void)
{
  char* str = NULL;
  GetMemory(str);
  strcpy(str, "hello world");
  printf(str);
}
int main()
{
  Test();
  return 0;
}

4.1.2 错误分析

991589e87b04af77d4bfd6a9e49e47be (1).png

4.1.3 错误改进

#include <assert.h>
#include <stdlib.h>
#include <string.h>
void GetMemory(char** p)
{
  assert(p);
  *p = (char*)malloc(100);
}
void Test(void)
{
  char* str = NULL;
  GetMemory(&str);
  strcpy(str, "hello world");
  printf(str);
  free(str);
  str = NULL;
}
int main()
{
  Test();
  return 0;
}

4.2 实例二

4.2.1 源代码

#include <stdio.h>
char* GetMemory(void)
{
  char p[] = "hello world";
  return p;
}
void Test(void)
{
  char* str = NULL;
  str = GetMemory();
  printf(str);
}
int main()
{
  Test();
  return 0;
}

4.2.2 错误分析

4dea9efb1d1848bed273d9b34c8c5525.png

4.3 实例三

4.3.1 原代码

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
void GetMemory(char** p, int num)
{
  *p = (char*)malloc(num);
}
void Test(void)
{
  char* str = NULL;
  GetMemory(&str, 100);
  strcpy(str, "hello");
  printf(str);
}
int main()
{
  Test();
  return 0;
}

4.3.2 错误分析

image.png

4.3.3 改进

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
void GetMemory(char** p, int num)
{
  *p = (char*)malloc(num);
}
void Test(void)
{
  char* str = NULL;
  GetMemory(&str, 100);
  assert(str);
  strcpy(str, "hello");
  printf(str);
  free(str);
  str = NULL;
}
int main()
{
  Test();
  return 0;
}

4.4 实例四

4.4.1 源代码

#include <stdio.h>
#include <stdlib.h>
void Test(void)
{
  char* str = (char*)malloc(100);
  strcpy(str, "hello");
  free(str);
  if (str != NULL)
  {
    strcpy(str, "world");
    printf(str);
  }
}
int main()
{
  Test();
  return 0;
}

4.4.2 错误分析

dbc6d4b5b8c10e5c0a020c0e0bdcf345.png










相关文章
|
10天前
|
C语言 C++
C语言 之 内存函数
C语言 之 内存函数
25 3
|
2月前
|
存储 编译器 C语言
【C语言篇】数据在内存中的存储(超详细)
浮点数就采⽤下⾯的规则表⽰,即指数E的真实值加上127(或1023),再将有效数字M去掉整数部分的1。
260 0
|
11天前
|
编译器 程序员 C语言
深入C语言:动态内存管理魔法
深入C语言:动态内存管理魔法
|
16天前
|
存储 程序员 编译器
C语言——动态内存管理与内存操作函数
C语言——动态内存管理与内存操作函数
|
6天前
|
C语言
保姆级教学 - C语言 之 动态内存管理
保姆级教学 - C语言 之 动态内存管理
11 0
|
11天前
|
存储 C语言
深入C语言内存:数据在内存中的存储
深入C语言内存:数据在内存中的存储
|
11天前
|
C语言 C++
c语言回顾-内存操作函数
c语言回顾-内存操作函数
34 0
|
13天前
|
存储 C语言 C++
来不及哀悼了,接下来上场的是C语言内存函数memcpy,memmove,memset,memcmp
本文详细介绍了C语言中的四个内存操作函数:memcpy用于无重叠复制,memmove处理重叠内存,memset用于填充特定值,memcmp用于内存区域比较。通过实例展示了它们的用法和注意事项。
43 0
|
18天前
|
存储 程序员 C语言
C语言动态内存管理
C语言动态内存管理
|
18天前
|
程序员 C语言
C语言内存函数精讲
C语言内存函数精讲