removing objects from an array

I am creating a program that uses an array of objects declared with

Element* elements =newElement[number];

where an element is a class that has/needs a its own destructor.

when I go to delete this would I use just use array delete, and have the program worry about calling the destructor:

delete[] elements;

or do I call the destructor for each item explicitly with keyword delete:

for(int ii =0; ii<ArraySize; ii++)

    delete elements[ii];

delete[] elements;

Note: I understand that I could probably use something like boost::ptr_vector, but I wanted similar to hashTable functionality (so the for loop would need additional information, but that is outside of the direct scope of this question) which is why I am using a traditional array. I would still like to know which behavior is needed to avoid memory leaks.

解答:

第一个是正确的,第二个会得到编译错误。

这个问题主要的问题其实是对于多位数组的动态内存分配的问题。比如我们不能直接使用int* p=new int[4][3];等的的。

而是应该借鉴下面的例子:


1

2

3

elements = new Element *[rows];

for (int i=0; i<rows; i++)

    elements[i] = new Element[row_len];

  然后采用:


1

2

3

for (int i=0; i<rows; i++)

    delete [] elements[i];

delete [] elements;

  原文地址:http://stackoverflow.com/questions/10425354/removing-objects-from-an-array

时间: 2024-12-08 23:53:46

removing objects from an array的相关文章

Common ASP.NET Code Techniques (DPC&amp;DWCReference)-

One of the things that I personally found frustrating with classic ASP was the difficulty associated with completing many common Web-related tasks. For example, the need to allow Web visitors to upload files to the Web server is fairly common for Web

Common ASP.NET Code Techniques (DPC&amp;amp;DWCReference)--1

asp.net One of the things that I personally found frustrating with classic ASP was the difficulty associated with completing many common Web-related tasks. For example, the need to allow Web visitors to upload files to the Web server is fairly common

[cocos2d-x]躲蜘蛛游戏设计[进阶]

实现一个躲蜘蛛的游戏,具备以下效果: 1.玩家精灵在游戏的最下方. 2.蜘蛛精灵在游戏的最上方,并且从上往下掉落. 3.手动控制玩家精灵. 4.碰撞检测,如果玩家精灵碰到蜘蛛则重玩. 5.音效的设置. 6.Loading界面 7.暂停和开始的设计 效果图: 代码实现: 1.创建默认的HelloWorld项目,屏幕默认的是横屏的,如何设置屏幕为竖屏? 找到RootViewController.mm文件,修改相应的代码即可: // For ios6, use supportedInterfaceOr

java写的StringList类

  delphi的StringList类真好用啊 试着用java写了一个 package com.zhao_yi.sysutils.classes; import java.util.List;import java.util.ArrayList;import java.util.StringTokenizer;import com.zhao_yi.sysutils.SysUtil;import com.zhao_yi.sysutils.*;import java.util.Arrays;imp

动态按钮生成器(上)

很COOL的,大家一起来研究研究!<html><head><meta http-equiv=Content-Type content="text/html; charset=gb2312"><script language="javascript"><!--Hide//timerspeed=10;//Array to make ObjectsImagi=new Array();//numbers of Linkn

Introduction to .NET Reflection

IntroductionReflection is ability to find information about types contained in an assembly at run time. Prior to .NET languages like C++ provided such ability in a limited sense. .NET provides a whole new set of APIs to introspect assemblies and obje

DotNET WinForm FAQ 16个(下)

9. 如何制作一个MDI的窗体 1. 建立一个新的Windows Application项目 2. 分别加入两个窗体Form1 .Form2 3. 设置Form1的IsMdiContainer属性为True.使它成为MDI主窗体. 4. 在Form2中加入一个RichTextBox控件,并设置Dock为:Fill 5. 在Tools 窗体中拖一个MainMenu到窗体Form1,然后建立一个菜单File|Windows|Help三个菜单项,File中包括New.Exit菜单项:Windows中包

OPC客户程序(VB篇——异步)

程序|异步 建立如下窗体: 引用如下: 代码如下: Option ExplicitOption Base 1 Const WRITEASYNC_ID = 1Const READASYNC_ID = 2Const REFRESHASYNC_ID = 3 '----------------------------------------------------------------------------' Interface Objects'---------------------------

String类使用的例子(3)

if ("first"==strFL) Console.WriteLine("The index value returned is : "+ objString.str.IndexOfAny(c,intStart)); else Console.WriteLine("The index value returned is : "+ objString.str.LastIndexOfAny(c,intStart)); break; case 3: