473,473 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Free

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
3 1283
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: dam_fool_2003 | last post by:
hai, Consider this following fragement typedef struct node { ... ... ... struct node *next; }NODE;
9
by: zerro | last post by:
Hello, I try to understand heap overflows (under Linux), but can not understand one thing with freeing memory allocated with malloc(). Here it comes: I have a program called 1.c: main() {...
16
by: Peter Ammon | last post by:
Often times, I'll have some malloc()'d data in a struct that need not change throughout the lifetime of the instance of the struct. Therefore, the field within the struct is declared a pointer to...
13
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
17
by: kj | last post by:
How can one test if a pointer has been "freed" (i.e. with free())? My naive assumption was that such a pointer would equal NULL, but not so. Thanks, kj -- NOTE: In my address everything...
7
by: slashdotcommacolon | last post by:
Hello, I'm working on the exercises from k&r, exercise 5-13 is to implement a simple replacement for the unix tail command. The brief says it should be able to cope no matter how unreasonable the...
76
by: dbansal | last post by:
Hi group, I have a question to ask you all. I have allocated some chunk of memory using ptr=(int*)malloc(). now I am trying to free that memory using free((void*)ptr). My question is does free()...
13
by: Chad | last post by:
Excercise 6-5 Write a functon undef that will remove a name and defintion from the table maintained by lookup and install. Code: unsigned hash(char *s); void undef(char *s)
3
by: dreiko466 | last post by:
(sorry about my english...) I am a newbie in C (3 month expierience) I have wrote a simple test programm in VS2005, what i do wrong?Please... In this programm i create a double linked list.Then ...
20
by: sirsnorklingtayo | last post by:
hi guys please help about Linked List, I'm having trouble freeing the allocated memory of a single linked list node with a dynamic char* fields, it doesn't freed up if I use the FREE()...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.