473,790 Members | 2,528 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(unsigne d 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(unsigne d 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(unsigne d 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(NUL L);
CB::Set_Mem(NUL L);
}
CMain(const CMain& rMain);
CMain& operator=(const CMain& rMain);
};

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

}

--
Bryan Parkoff
Jul 22 '05 #1
3 2064
* "Bryan Parkoff" <br************ ******@nospam.c om> 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.c om> 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 "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...
6
3273
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 be tested on different servers. Without using the keyword "Shared", how can I share these data to the other 5 classes? Thanks for any ideas! Cesar
5
5379
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 projects in a same solution. It's nice to have a common "set" of Classes. Is this the only way to share classes between projects ?
6
4488
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 systems over the years. I have developed code in more than 30 distinct programming languages for a wide cariety of industries. But this issue of structures in C# is beginning to annoy me. I should also make it clear that I am not a big supporter...
8
2136
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), and writing the result to disk afterwards. Therefore I have created some classes, e.g. WaveFileIO and AiffFileIO and MP3FileIO and AuFileIO for the In/Out operations.
7
4694
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 are the memory layout is contiguous? i.e all the character resides side by side just like array, and all Points side by side insede the
6
2033
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 implementations will share headers that define virtual c++ class interfaces.
6
2181
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 two derived classes. Second class has its own m_Base1 and m_Base2 and third class does the same. I am curious. How can second class and third class share the same m_Base1 and m_Base2? You define second class first and enter data into...
45
3021
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 clsData Private m_objSQLClient As clsSQLClient Private m_objUsers As clsUsers
0
9512
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10413
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9986
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9021
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7530
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5422
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3707
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.