Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Where to free memory ?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Victor Bazarov  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 17:26
Newsgroups: comp.lang.c++
From: Victor Bazarov <v.Abaza...@comAcast.net>
Date: Mon, 09 Nov 2009 12:26:57 -0500
Local: Mon 9 Nov 2009 17:26
Subject: Re: Where to free memory ?
Phil wrote (second time in less than 10 minutes):

> Hello all,
> Let's say I have two classes A and B defined as follows:
> [..]

Learn to wait patiently.  Usenet is not a chat room.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Phil  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 17:26
Newsgroups: comp.lang.c++
From: Phil <pbruy...@yahoo.com>
Date: Mon, 9 Nov 2009 09:26:34 -0800 (PST)
Local: Mon 9 Nov 2009 17:26
Subject: Where to free memory ?
Hello all,
I kindly request your help for the following problem.
Let's say I have two classes A and B defined as follows:

// ClassA.h //////////////////////////
class ClassA
{
public:
  void ClassA(int nItems); // constructor
  virtual ~ClassA();
protected:
  double *myTable;    // My table of nItems double values

}

/////////////////////////////////////

// ClassA.cpp ///////////////////////
void ClassA(int nItems)
{
  // Allocate memory for the table of double values
  myTable=(double*)calloc(nItems, sizeof(double));

}

~ClassA()
{
// Should I free the memory allocated for myTable here?
// free(myTable);
// myTable=NULL;

}

/////////////////////////////////////

// ClassB.h //////////////////////////
#include "ClassA.h"
class ClassB
{
public:
  void ClassB();
protected:
  ClassA classA(int nItems);

}

/////////////////////////////////////

// ClassB.cpp ///////////////////////
void ClassB()
{
classA=new ClassA(10);
// Do some processing here with classA
delete classA;

}

/////////////////////////////////////

My question is: when should I free the memory allocated for myTable
array of object classA ?
I am using MS Visual C++ 6.0 and Vista. Given the above code, I get a
memory leak. OK.
But if I free the memory as commented out in the destructor, I get an
error message pointing at the free(myTable) line
"memory check error at 0x02FDAA50 = 0xFE, should be 0xFD."
What is the correct way to free the memory ?
TIA,
Phil


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mzdude  
View profile   Translate to Translated (View Original)
 More options 9 Nov, 18:15
Newsgroups: comp.lang.c++
From: mzdude <jsa...@cox.net>
Date: Mon, 9 Nov 2009 10:15:00 -0800 (PST)
Subject: Re: Where to free memory ?
On Nov 9, 12:26 pm, Phil <pbruy...@yahoo.com> wrote:

> Hello all,
> I kindly request your help for the following problem.
> Let's say I have two classes A and B defined as follows:

> // ClassA.h //////////////////////////
> class ClassA
> {
> public:
>   void ClassA(int nItems); // constructor
>   virtual ~ClassA();
> protected:
>   double *myTable;    // My table of nItems double values

> }

Even with VC6, I would use std::vector<double> myTable
and get rid of the dynamic memory issues. But if you don't...

> /////////////////////////////////////

> // ClassA.cpp ///////////////////////
> void ClassA(int nItems)
> {
>   // Allocate memory for the table of double values
>   myTable=(double*)calloc(nItems, sizeof(double));

> }

Assume this is:
void ClassA::ClassA(int nItems)
  : myTable( (double *)calloc(..) )
{
  if( myTable == NULL )
    throw std::bad_alloc();

}

if you were using std::vector

void ClassA::ClassA(int nItems)
  : myTable(nItems,0.0)
{}

> ~ClassA()
> {
> // Should I free the memory allocated for myTable here?
> // free(myTable);
> // myTable=NULL;

> }

Yes you should free memory here. The myTable=NULL can
be omitted.

If you are using std::vector, you don't even need the dtor.

> /////////////////////////////////////

> // ClassB.h //////////////////////////
> #include "ClassA.h"
> class ClassB
> {
> public:
>   void ClassB();
> protected:
>   ClassA classA(int nItems);

This is invalid. Should just be ClassA classA;
Or given the code below
  ClassA *classA;

> /////////////////////////////////////

> // ClassB.cpp ///////////////////////
> void ClassB()
> {
> classA=new ClassA(10);
> // Do some processing here with classA
> delete classA;

> }

classA is not declared as a pointer in your example.
This should refuse to compile.

    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google