473,383 Members | 1,984 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

using the 'delete' operator on array of structures

Suppose I have the following structure

struct b
{
char fullname[100];
char title[100];
bopname
};

and i declare a pointer to the struct as follows
b* bp;

and at runtime,

bp = new b[5];

In the end, is the statement delete[ ] bp enough to release
the dynamically allocated memory. Or do i need to separately delete bp[0],bp[1]...

Does delete[ ] bp indicate that the array[ ] pointed by bp has to be deleted?? [I am confused as to how this happens, since the pointer only points at the 1st struct address]
Feb 27 '13 #1

✓ answered by Banfa

You only allocated 1 block of memory, called new once so you only have 1 block of memory to delete, you can only call delete once.

You should always call new and delete exactly the same number of times over the course of a program running.

The memory manager only needs the pointer to actually delete the block of memory, however the compiler needs to know that you are deleting an array of structures (or classes) so that it can call the destructor on every class in the array which can be demonstrated by adding constructors and destructors with cout statements

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. struct b
  4. {
  5.     char fullname[100]; 
  6.     char title[100]; 
  7.  
  8.     b() :
  9.      number(++nextNumber)
  10.     {
  11.         std::cout << "Constructing b: " << number << std::endl;
  12.     }
  13.  
  14.     ~b()
  15.     {
  16.         std::cout << "Desstructing b: " << number << std::endl;
  17.     }
  18.  
  19. private:
  20.     int number;
  21.     static int nextNumber;
  22. };
  23.  
  24. int b::nextNumber = 0;
  25.  
  26. int main()
  27. {
  28.     b* bp;
  29.  
  30.     bp = new b[5];
  31.  
  32.     delete[] bp;
  33. }
This demonstrates destructing every structure constructed, if you changed line 32 to remove the [] you will fiuns that although the block of memory is deleted only the destructor for the first element of the array is called.

2 8335
Banfa
9,065 Expert Mod 8TB
You only allocated 1 block of memory, called new once so you only have 1 block of memory to delete, you can only call delete once.

You should always call new and delete exactly the same number of times over the course of a program running.

The memory manager only needs the pointer to actually delete the block of memory, however the compiler needs to know that you are deleting an array of structures (or classes) so that it can call the destructor on every class in the array which can be demonstrated by adding constructors and destructors with cout statements

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. struct b
  4. {
  5.     char fullname[100]; 
  6.     char title[100]; 
  7.  
  8.     b() :
  9.      number(++nextNumber)
  10.     {
  11.         std::cout << "Constructing b: " << number << std::endl;
  12.     }
  13.  
  14.     ~b()
  15.     {
  16.         std::cout << "Desstructing b: " << number << std::endl;
  17.     }
  18.  
  19. private:
  20.     int number;
  21.     static int nextNumber;
  22. };
  23.  
  24. int b::nextNumber = 0;
  25.  
  26. int main()
  27. {
  28.     b* bp;
  29.  
  30.     bp = new b[5];
  31.  
  32.     delete[] bp;
  33. }
This demonstrates destructing every structure constructed, if you changed line 32 to remove the [] you will fiuns that although the block of memory is deleted only the destructor for the first element of the array is called.
Feb 27 '13 #2
Thanks Banfa :) That cleared my doubts.
Feb 27 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: A | last post by:
Hi, Consider this: char* ptr = "a string"; Is ptr a pointer to a dynamically created object for which i must use the delete operator to deallocate memory? I'm sure the "a string" part...
1
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different...
2
by: Stijn Oude Brunink | last post by:
Hello, I want to use the vector class to work with arrays of classes but I seem to get in conflict with the delete operator used in the specific class. The code below gives an assertion?! How is...
13
by: Nemo | last post by:
Hello Folks, I need to manipulate a list of char strings as follows, but when I want to delete the pointer created with new at the end, delete operator crashes, any idea? char* list;...
9
by: bob | last post by:
Let's say you use the delete operator as follows: Rocket *rocket = new Rocket; void *voidptr = (void *) rocket; delete voidptr; Does the memory get deleted right? Do delete operations on...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
3
by: Camel | last post by:
I am very new to C++ and came across this section of code in an example, whilst learning about dynamic memory. The example uses the delete operator in a for loop, it seems to imply that each time...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
10
by: Dreea | last post by:
Hello Could anybody help me? I have the following piece of code cryptReturnVal = new BYTE; char *cryptReturnValueChar; // call decrypt function cryptReturnVal=(*decrypt)(input, len,...
2
by: kapilkumawat | last post by:
I have an requirement to overload the delete operator in C++, but it should also accept the sizeof() the object that is to be deleted. Actually I am trying to built a custom memory allocator and...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.