By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,988 Members | 1,367 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,988 IT Pros & Developers. It's quick & easy.

Free

P: n/a
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

Sep 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

--

Frederick Gotham
Sep 16 '06 #2

P: n/a
Mohsen wrote:
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<Animalinstead 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
Sep 16 '06 #3

P: n/a
"Mohsen" <ja*******@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
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.
>
Thanks,
Mohsen

Sep 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.