473,498 Members | 1,992 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Three Classes Share One Memory

I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee
Lajoie. I have been studying it for couple months however it does not
provide a valuable information which it is about "friend to class". I am
very disappointed because it is the way how C++ Compiler is designed. I
assume that "friend to class" is not the good option.
If CMain class is initialized before CA class, CB class, and CC class
are initialized inside CMain class' constructor function. It is like m_pCA
= new CA(this). I suspect that it is not a good idea because it might can
leak memory because it is not in the right order. I must manually order to
allocate and deallocate m_pCA myself. It can misled to a minor bug under
class design.
I have mentioned about the nested class, but it is not a good option.
The declaration and definition must be done before nested class can be used.
It can be a problem if I want to have only one class each header file
instead of two or more classes each header file.
I have decided to create multiple inheritance. All CA class, CB class
and CC class can be derived into one CMain class. All functions including
Constructor function and Destructor function are protected rather than
public because I do not allow CA class, CB class, and CC class to be
initialized under void main(void) function unless CMain class has authority
to access CA class, CB class, and CC class.
I can be able to show A.h, B.h, C.h, CMain.h, A.cpp, B.cpp, C.pp, and
CMain.cpp files. Four are headers and another fours are sources. CMain.h
can include A.h, B.h, and C.h while main.cpp can include CMain.h. It is
really a good class design.
I have decided to allow CA class, CB class, and CC class to share one
memory address that contains 10,000 elements in this array. CC class is the
one to allocate and deallocate m_pMem while CA class and CB class do not.
It is done automatically by Constructor function, otherwise, it can be done
manually by Initialize function.
How can CA class and CB class access CC class' m_pMem? I did not want
CA class and CB class to access CC class' m_pMem in order to modify 10,000
elements because it can slow the performance. It would be better to access
m_pMem inside CA class and CB class only. I have decided to use Get_Mem
function and Set_Men function to copy memory address (4 bytes) from CC
class' m_pMem to CA class' and CB class' m_pMem. All three classes' m_pMem
are always private. I do not want to accidently overwrite m_pMem such as
CA::m_pMem = CC::m_pMem. Set_Mem function and Get_Mem function are useful
to prevent from overwritten accidently.
The problem is that m_pMem can't be deallocated two times or three
times, but it must be done once, otherwise, it can leak memory. It is why I
have to use Terminate function to deallocate m_pMem manually inside CMain
class rather than Destructor function inside CA class, CB class, and CC
class. Look at Destructor example below.
CA::Destructor()
{
if (m_pMem != NULL)
delete [] m_pMem;
}
CB::Destructor()
{
if (m_pMem != NULL)
delete [] m_pMem;
}
CC::Destructor()
{
if (m_pMem != NULL)
delete [] m_pMem;
}
CC::Destructor() will be executed first to detect m_pMem before it can
deallocate safely however CB::Destructor() and CA::Destructor() will fail,
because they think that m_pMem is NOT ALREADY deallocated before they
attempt to deallocate second time. It would be safe to put m_pMem = NULL in
both CA::Destructor() and CB::Destructor(), otherwise Terminate() can be
used in CMain class only.
Please provide a safe method to deallocate shared memory from three
classes. I have included my simple source code below. Please mention what
you think about my class design which is neat and better design. Please
advise.
Please note: Multiple Inheritance only allow protected functions rather
than public functions that I have chosen. Only void main(void) function is
allowed to access CMain::Run(), but it is not allowed to access all
functions inside four classes. CMain::Run() is the one that it has
authority to access ALL PROTECTED functions in three classes. Does it make
sense?

#include <stdio.h>

class CA
{
protected:
CA()
{
printf("CA Constructor\n");
}
~CA()
{
printf("CA Deconstructor\n");
}
void Run(void)
{
printf("CA Run\n");
}
unsigned char* Get_Mem(void)
{
return m_pMem;
}
void Set_Mem(unsigned char* pMem)
{
m_pMem = pMem;
}
private:
CA(const CA& rA);
CA& operator=(const CA& rA);
unsigned char* m_pMem;
};

class CB
{
protected:
CB()
{
printf("CB Constructor\n");
}
~CB()
{
printf("CB Deconstructor\n");
}
void Run(void)
{
printf("CB Run\n");
}
unsigned char* Get_Mem(void)
{
return m_pMem;
}
void Set_Mem(unsigned char* pMem)
{
m_pMem = pMem;
}
private:
CB(const CB& rB);
CB& operator=(const CB& rB);
unsigned char* m_pMem;
};

class CC
{
protected:
CC()
{
printf("CC Constructor\n");
m_pMem = new unsigned char[0x10000];
}
~CC()
{
printf("CC Deconstructor\n");
delete [] m_pMem;
}
void Run(void)
{
printf("CC Run\n");
}
unsigned char* Get_Mem(void)
{
return m_pMem;
}
void Set_Mem(unsigned char* pMem)
{
m_pMem = pMem;
}
private:
CC(const CC& rC);
CC& operator=(const CC& rC);
unsigned char* m_pMem;
};

class CMain : public CA, public CB, public CC
{
public:
CMain()
{
printf("CMain Constructor\n");
Initialize();
}
~CMain()
{
printf("CMain Deconstructor\n");
Terminate();
}
void Run(void)
{
printf("CMain Run\n");
CA::Run();
CB::Run();
CC::Run();
}
private:
void Initialize(void)
{
CA::Set_Mem(CC::Get_Mem());
CB::Set_Mem(CC::Get_Mem());
}
void Terminate(void)
{
CA::Set_Mem(NULL);
CB::Set_Mem(NULL);
}
CMain(const CMain& rMain);
CMain& operator=(const CMain& rMain);
};

void main(void)
{
CMain Main;
Main.Run();

}

--
Bryan Parkoff
Jul 22 '05 #1
3 2022
* "Bryan Parkoff" <br******************@nospam.com> schriebt:

Subject: Three Classes Share One Memory


Bryan, a "class" does not occupy storage.

It can have static members that occupy storage, but at your current
level, forget about that.

Forgetting about that, then, only _instances_ of classes, "object"s,
occupy storage.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
One way to solve the problem is to use reference counting.

Whenever an object obtains a reference to the shared object, a counter
is incremented. The counter can then be decremented in the destructor.
When the counter decrement brings you back to 0, the shared object
can be deleted.

Sandeep
--
http://www.EventHelix.com/EventStudio
EventStudio 2.0 - Generate Sequence Diagrams and Use Case Diagrams in PDF
Jul 22 '05 #3
On Tue, 13 Apr 2004 05:27:25 GMT, "Bryan Parkoff"
<br******************@nospam.com> wrote:

The standard solution to the question I think you are asking is "use a
smart pointer". See shared_ptr at www.boost.org, or do a web search
for "C++ smart pointer".

Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
360
by: Bryan Parkoff | last post by:
I have C++ Primer Third Edition -- Author Stanley B. Lippman and Josee Lajoie. I have been studying it for couple months however it does not provide a valuable information which it is about...
6
3249
by: cksj | last post by:
I'm working on a VB.Net DLL project. It has 6 classes. One of the classes has the properties for the database path, database name and server name. The purpose of this class is so that the DLL can...
5
5341
by: cybertof | last post by:
Hi ! What is the common use of sharing a single .cs across multiple project files ? I think it's to share common classes between projects. I have actually a .cs file shared accross multiple...
6
4472
by: Ken Allen | last post by:
OK, I admit that I have been programming since before C++ was invented, and I have developed more than my share of assembly language systems, and even contributed to operating system and compiler...
8
2117
by: Michael | last post by:
Hi, I think my problem deals with class casting and inheritance. I want to deal with various Audio Formats, reading into memory for modification, working with it (done by different classes),...
7
4676
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now...
6
2011
by: bramdoornbos | last post by:
Hello, I am looking for a solution to interface with C++ classes implemented in a dll compiled by gcc. This dll will be however accessed by a visual c++ compiled host (not made by me). Both...
6
2160
by: Immortal Nephi | last post by:
First class is the base class. It has two data: m_Base1 and m_Base2. Second class and third class are derived classes and they are derived from first class. m_Base1 and m_Base2 are inherited into...
45
2959
by: =?Utf-8?B?QmV0aA==?= | last post by:
Hello. I'm trying to find another way to share an instance of an object with other classes. I started by passing the instance to the other class's constructor, like this: Friend Class...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7002
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
7203
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...
1
6885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5462
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
4588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3093
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
1417
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 ...
1
656
muto222
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.