473,785 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does a ctor/dtor occupy memory

When a class is instantiated, will the ctor and dtor occupy memory?
IF ctor and dtor do occupy memory of an instantiated class where would
they be in memeory?
Jul 23 '05 #1
1 1536

"pmastroian ni" <pm**********@s helbrook.com> wrote in message
news:70******** *************** ***@posting.goo gle.com...
When a class is instantiated, will the ctor and dtor occupy memory?
IF ctor and dtor do occupy memory of an instantiated class where would
they be in memeory?


A good book should tell you this.

Member functions, including the constructor and destructor, obviously have
to exist "somewhere" (unless, I suppose, they do nothing, in which case an
optimizing compiler could remove them). Where they exists is entirely up to
the compiler and the operating system.

But member functions exist on a "per class" basis, not a "per instance"
basis. There's no reason to have copies of those functions lying around for
every instance of the class that your code creates. Each instance simply
refers to the same set of member functions.

Member data, obviously, needs to exist for every instance, because the
member variables likely take on different values for each instance. (At
least for non-static member data, that is.) But the functions are just sets
of instructions, and don't need separate copies.

-Howard
Jul 23 '05 #2

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

Similar topics

2
1770
by: Eric Liu | last post by:
Hi, Can anyone explain why the following code works? The static object creation is via the private ctor. If I try to define a X object in the mainline, of course I will get complaint about calling the private ctor. I ran the code with CW8.3, it works fine. the output is: >Private ctor >dtor
10
1372
by: richardclay09 | last post by:
The output of this: #include <iostream> #include <vector> using namespace std; struct X { int i; X(const X& x) : i(x.i) { cout << "ctor copy: " << i << endl;
7
1923
by: Thomas | last post by:
I have a class foo whose ctor might throw an exception. When I create a foo object using "foo* myfoo = new foo;", and the ctor throws an exception, do I need to delete myfoo to free its memory?
6
2000
by: puzzlecracker | last post by:
When ctor throws an exception, dtor is not called for that object (correct me if I am wrong) - then how would already allocated memebers de-allocated? Thanks.
9
2473
by: ziman137 | last post by:
Hi all, The results from following codes got me a bit confused. #include <stdio.h> #include <iostream> using namespace std; struct A {
6
2029
by: junw2000 | last post by:
Below is a simple code: #include <iostream> class base{ public: base(): i(11){std::cout<<"base constructor"<<'\n';} virtual void f() = 0; virtual ~base(){ std::cout<<"base destructor"<<'\n';} int i;
5
8092
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
8
2742
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/cb014f4ba9df614a Can anybody answer? Who can't read in russian:
5
1802
by: peifeng_w | last post by:
Hi, try the following code with flag=0/1/2. #include<iostream> using namespace std; #define flag 2//option:0,1,2 class C {
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10319
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
10147
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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...
1
7496
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
5380
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...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4046
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 we have to send another system

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.