Free

M

Mohsen

Hello everyone,

In my program, I have the 'Record class' as follow:

class Record
{
public:
int ID;
int PS[M];
int PD[M];
int Sex;
};

I have also defined the Animal as follows:

Record *Animal=new Record [NoAnimal];

Could anybody let me know what I should use to make the 'Animal'
free? Is it just "delete [] Animal;" or I should add anything more?

Thanks,
Mohsen
 
F

Frederick Gotham

Mohsen posted:
class Record
{
public:
int ID;
int PS[M];
int PD[M];
int Sex;
};


I shall presume that "M" is some sort of macro, perhaps something along the
lines of:

#define M 42

Record *Animal=new Record [NoAnimal];


Inconsistent use of initial capital letters... but legal C++ nonetheless.

(Again, I shall presume that "NoAnimal" is some sort of integral value
defined elsewhere in the source code.)

Could anybody let me know what I should use to make the 'Animal'
free? Is it just "delete [] Animal;" or I should add anything more?


delete [] Animal;

is the only accurate method.

Use the internet to find out how "new", "new []" and "malloc" work --
Google is your friend.
 
K

Kai-Uwe Bux

Mohsen said:
In my program, I have the 'Record class' as follow:

class Record
{
public:
int ID;
int PS[M];
int PD[M];
int Sex;
};

I have also defined the Animal as follows:

Record *Animal=new Record [NoAnimal];

Could anybody let me know what I should use to make the 'Animal'
free? Is it just "delete [] Animal;" or I should add anything more?

"delete [] Animal;" will be sufficient.

You may consider using std::vector<Animal> instead of a dynamic array. It
will do allocation, reallocation, and destruction for you. If you never
need to resize, you could also go for tr1::array. This is more flexible in
case you change algorithms so that performance might benefit from changing
the underlying data structure.


Best

Kai-Uwe Bux
 
J

Jim Langston

Mohsen said:
Hello everyone,

In my program, I have the 'Record class' as follow:

class Record
{
public:
int ID;
int PS[M];
int PD[M];
int Sex;
};

I have also defined the Animal as follows:

Record *Animal=new Record [NoAnimal];

Consider using NumAnimal or NumAnimals insteads of NoAnimal. I mean,
"number" doesn't even have an "o" in it. And if this isn't the number of
animals, then why are you allocating the array that size?
Could anybody let me know what I should use to make the 'Animal'
free? Is it just "delete [] Animal;" or I should add anything more?

delete[] Animal;

is fine in this case. Even if Record had a custom destructor, you would
still use
delete[] Animal;
which would call the custom destructor on each instance.

Also, I would probably make Sex an enum instead of an int. Unless a sex of
3 has any meaning to you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top