使用new int [10]分配的内存必须使用delete [] [英] Memory allocated with new int[10] must be deallocated with delete[]

查看:127
本文介绍了使用new int [10]分配的内存必须使用delete []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

语句:


使用new int [10]分配的内存必须使用delete []解除分配。

Memory allocated with new int[10] must be deallocated with delete[].

So。

new int[10];

那么如何删除呢?

编辑:
谢谢你们:)
我认为Wilson,Paul,Tim等都是对的, - - ...只是

Thank you guys :) I think Wilson, Paul, Tim etc. are right, the statement omit some words therefore confused me - -...it's simply

int * p = new int[10];

delete [] p; 

我从来没有见过Rob的回答,看起来真的很新!任何解释将不胜感激。)

I've never seen Rob's answer though, looks really new to me! Any explanation will be appreciated.:)

delete[] new int[10];


推荐答案

您缺少一个变量来保存返回的值从新。您应该有:

You're missing a variable to hold the value returned from new. You should have something like:

int* intArray = new int[10];

然后当您要删除内存时,可以通过变量引用它:

then later when you want to delete the memory, you reference it through the variable:

delete[] intArray;

这篇关于使用new int [10]分配的内存必须使用delete []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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