473,382 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2018
* "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
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
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
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
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
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
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
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
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
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.