c/c++语言开发共享C++使用动态内存分配的原因解说

上节我们讲了c++程序的内存分布。c++程序的内存分布本节来介绍为什么要进行内存分配。按需分配,根据需要分配内存,不浪费。内存拷贝函数void* memcpy(void* dest, const vo

上节我们讲了c++程序的内存分布。c++程序的内存分布
本节来介绍为什么要进行内存分配。

按需分配,根据需要分配内存,不浪费。
内存拷贝函数void* memcpy(void* dest, const void* src, size_t n);
从源src中拷贝n字节的内存到dest中。需要包含头文件#include <string.h>

  #include <stdio.h>    #include <string.h>    using namespace std;    int main() {      int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };        int* b;      b = new int[15];        //从a拷贝10 * 4字节的内存到b      memcpy_s(b, sizeof(int) * 10, a, sizeof(int) * 10);        //进行赋值      for(int i = sizeof(a) / sizeof(a[0]); i < 15; i++){          *(b + i) = 15;      }            for (int i = 0; i < 15; i++) {          printf("%d ", b[i]);      }          return 0;  }

输出结果:

1 2 3 4 5 6 7 8 9 10 15 15 15 15 15

C++使用动态内存分配的原因解说

被调用函数之外需要使用被调用函数内部的指针对应的地址空间

  #include <stdio.h>    #include <stdlib.h>    #include <string.h>    using namespace std;    //定义一个指针函数  void* test() {      void* a;      //分配100*4个字节给a指针      //mallocc语言的动态分配函数      a = malloc(sizeof(int) * 100);      if (!a) {          printf("内存分配失败!");          return null;      }        for (int i = 0; i < 100; i++)      {          *((int*)a + i) = i;      }        return a;  }    int main() {      //test()返回void*的内存,需要强转换      int* a = (int*)test();        //打印前20个      for (int i = 0; i < 20; i++) {          printf("%d ", a[i]);      }        //c语言的释放内存方法    free(a);        return 0;  }

输出结果:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

此处在main函数中使用了在test()函数中分配的动态内存重点地址。

也可以通过二级指针来保存,内存空间:

  #include <stdio.h>    #include <stdlib.h>    #include <string.h>    using namespace std;    //定义一个指针函数  void test(int **a) {        *a = (int*)malloc(sizeof(int) * 100);      if (!*a) {          printf("内存分配失败!");          exit(0);      }        for (int i = 0; i < 100; i++)      {          *(*a + i) = i;      }  }    int main() {      //test()返回void*的内存,需要强转换      int* a;      test(&a);        //打印前20个      for (int i = 0; i < 20; i++) {          printf("%d ", a[i]);      }        free(a);        return 0;  }

突破栈区的限制,可以给程序分配更多的空间。
栈区的大小有限,在windows系统下,栈区的大小一般为1~2mb

  #include <stdio.h>    #include <stdlib.h>    #include <string.h>    using namespace std;    void test() {      //分配一个特别大的数组      int a[102400 * 3];// 100k * 3 * 4 = 1200k      a[0] = 0;  }    int main() {      test();        return 0;  }

点运行会出现stack overflow的提示(栈区溢出!)。
修改:

  #include <stdio.h>    #include <stdlib.h>    #include <string.h>    using namespace std;    void test() {      //在堆中分配一个特别大的数组1g      //在windows 10 系统限制的堆为2g      int* a = (int*)malloc(1024 * 1000 * 1000 * 1); //1g      a[0] = 0;  }    int main() {      test();        return 0;  }

成功运行!但是分配两个g的动态内存,就会报错,这个时候分配失败,a = null;

总结:使用堆分三个点。

1、按需分配,根据需要分配内存,不浪费。
2、被调用函数之外需要使用被调用函数内部的指针对应的地址空间。
3、突破栈区的限制,可以给程序分配更多的空间。

本节介绍了为什么使用动态内存分配,下节我们介绍动态内存的分配、使用、释放。

到此这篇关于c++使用动态内存分配的原因解说的文章就介绍到这了,更多相关c++使用动态内存分配内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/595660.html

(0)
上一篇 2021年5月8日
下一篇 2021年5月8日

精彩推荐