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
- #include<iostream>
-
-
struct b
-
{
-
char fullname[100];
-
char title[100];
-
-
b() :
-
number(++nextNumber)
-
{
-
std::cout << "Constructing b: " << number << std::endl;
-
}
-
-
~b()
-
{
-
std::cout << "Desstructing b: " << number << std::endl;
-
}
-
-
private:
-
int number;
-
static int nextNumber;
-
};
-
-
int b::nextNumber = 0;
-
-
int main()
-
{
-
b* bp;
-
-
bp = new b[5];
-
-
delete[] bp;
-
}
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.