int *array = new int[n];这个功能实际上在做什么? [英] int *array = new int[n]; what is this function actually doing?

查看:62
本文介绍了int *array = new int[n];这个功能实际上在做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何创建动态定义的数组感到困惑:

I am confused about how to create a dynamic defined array:

 int *array = new int[n];

我不知道这是在做什么.我可以说它正在创建一个名为 array 的指针,该指针指向一个新的对象/数组 int?有人愿意解释吗?

I have no idea what this is doing. I can tell it's creating a pointer named array that's pointing to a new object/array int? Would someone care to explain?

推荐答案

new 分配存储您请求的对象/数组所需的内存量.在这种情况下,n 个整数.

new allocates an amount of memory needed to store the object/array that you request. In this case n numbers of int.

然后指针会将地址存储到这块内存中.

The pointer will then store the address to this block of memory.

但是要小心,这个分配的内存块不会被释放,除非你通过写来告诉它

But be careful, this allocated block of memory will not be freed until you tell it so by writing

delete [] array;

这篇关于int *array = new int[n];这个功能实际上在做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆