473,657 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Are unused class methods removed from exe?

I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.

Jun 16 '06 #1
9 3181
ThazKool wrote:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.


May I suggest that you ask in a forum dedicated to your compiler?
That's strictly an implementation issue, beyond the scope of the language.
Jun 16 '06 #2
I said: If it is only specific compilers could you name which ones.
Thanks
again.

This comment was directed to all C++ users. If you are looking to
flame someone, then take a look inside yourself and find out where that
inner child was wounded. Then nurture that inner child and tell him
everything is going to be okay and that he does not have to lash out at
people.

Ideally this should be cross-compiler and cross platform, but I am not
sure and I need the comments of all c++ users. Hence comp.lang.c++.

red floyd wrote:
ThazKool wrote:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.


May I suggest that you ask in a forum dedicated to your compiler?
That's strictly an implementation issue, beyond the scope of the language.


Jun 16 '06 #3
Hi,
This is the job of the linker.
If a function is not called, its code is not included in the final exe.

You can experiment this :
define a method, but don't implement it and never call it
link --> no error
now, call it somewhere, and link --> unresolved external
So, you see that the linker ignores functions that are never called.

For the "specific compiler" part, i dont really know, but IMHO, no
linker is so stupid to include code parts there are never called.
ThazKool wrote:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.

-- -----------------------------
Samuel Lacroix

Jun 16 '06 #4
ThazKool wrote:

Please don't top post, it's considered rude in these parts.
ThazKool wrote:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.


May I suggest that you ask in a forum dedicated to your compiler?
That's strictly an implementation issue, beyond the scope of the language.


I said: If it is only specific compilers could you name which ones.
Thanks
again.

This comment was directed to all C++ users. If you are looking to
flame someone, then take a look inside yourself and find out where that
inner child was wounded. Then nurture that inner child and tell him
everything is going to be okay and that he does not have to lash out at
people.

Ideally this should be cross-compiler and cross platform, but I am not
sure and I need the comments of all c++ users. Hence comp.lang.c++.

I don't think any compiler would do this, it isn't the compiler's job.

That's why the question is OT here, you should ask on a platform
specific group, the platform's tools will look after linking.

--
Ian Collins.
Jun 17 '06 #5
In article <1150497951.701 731.267900
@i40g2000cwc.go oglegroups.com> , Ch**********@gm ail.com
says...
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:
[ ... ]
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?


Most linkers are smart enough to ignore non-virtual
functions that aren't called. It's more difficult to
figure out whether a virtual function is called, so
they're much more likely to be included even if they're
not called.

Except strictly from a viewpoint of the size of
executable, virtual memory makes this less important than
it once was. With demand-paged virtual memory, the code
for the function won't normally be loaded into memory
until or unless it's called (unless it lives in the same
page with something else that's called, in which case it
doesn't usually make a lot of difference).

For anything more specific, you'll probably need to ask
in a newsgroup specific to the platform you're targeting.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 17 '06 #6
ThazKool wrote:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?


What you can typically expect from the crufty compiler and linker
technology is roughly this:

1. Individual object files that are not referenced are removed during
linking.
2. If an object file contains any external symbol that is referenced
somewhere, the
entire object file is linked to the target, bringing in all the
unused parts with it.
3. The compiler itself may do a good job removing unused functions and
objects
which are private to a compilation unit (declared static).
4. Unused inline functions are usually treated well.

The reason for 2 is that many object file formats simply cannot be
edited to delete a function. They may contain internal references among
the functions which are already resolved: for instance,
program-counter-relative branches from one function to another. The
compiler emits the translation unit image all at once, and knows the
displacement between all the instruction. Calling a function can then
be done using a relocatable relative branch: ``call the function that
is 1549 bytes up from this point''. To delete some code in between
would require all those references which jump across that code to be
decoded and patched, which would require the linker to understand that
machine language.

The last time I looked at the sources for the GNU C compiler, the build
system used a gross hack in order to build the run-time support library
libgcc.a in order to make the library as small as possible. #ifdef
statements were used to divide up the library source file into sections
containing a single function, and it was passed through the compiler
many times, with different preprocessor symbols defined to select
individual functions for compilation, resulting in a separate .o file
for each function. Thus if that run-time function is not used, it
doesn't end up linked to the executable.

If your compiler doesn't support function-level linking, and you
really, really want to remove unused functions, what you can do is put
them into separate source files. Have the class declaration in a single
..h of course, but the implementations of the member functions can be
split into multiple files.

Watch out for virtuals. If a function is virtual, then typically, it's
as good as called. Even if you don't use that function, most compilers
generate object code that requires the functions to be there (e.g. a
virtual containing a symbolic reference to the function which must
resolve).

Jun 17 '06 #7
Excellent post. This was the insight that I was in search of. I
understand that there are many ways in which compilers implement these
features, but you gave me a great understanding of the way object code
can bloat. I guess some linkers may try to scan for dependencies and
remove unused code, but this can be tricky if the code contains
function pointers which would take us back to the point that object
code can bloat.

Thank you very much. I will still have to experiment some.

I guess making API calls with inline c-like functions is the
meanest-leanest way to go short of assembly.

Kaz Kylheku wrote:
ThazKool wrote:
I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?


What you can typically expect from the crufty compiler and linker
technology is roughly this:

1. Individual object files that are not referenced are removed during
linking.
2. If an object file contains any external symbol that is referenced
somewhere, the
entire object file is linked to the target, bringing in all the
unused parts with it.
3. The compiler itself may do a good job removing unused functions and
objects
which are private to a compilation unit (declared static).
4. Unused inline functions are usually treated well.

The reason for 2 is that many object file formats simply cannot be
edited to delete a function. They may contain internal references among
the functions which are already resolved: for instance,
program-counter-relative branches from one function to another. The
compiler emits the translation unit image all at once, and knows the
displacement between all the instruction. Calling a function can then
be done using a relocatable relative branch: ``call the function that
is 1549 bytes up from this point''. To delete some code in between
would require all those references which jump across that code to be
decoded and patched, which would require the linker to understand that
machine language.

The last time I looked at the sources for the GNU C compiler, the build
system used a gross hack in order to build the run-time support library
libgcc.a in order to make the library as small as possible. #ifdef
statements were used to divide up the library source file into sections
containing a single function, and it was passed through the compiler
many times, with different preprocessor symbols defined to select
individual functions for compilation, resulting in a separate .o file
for each function. Thus if that run-time function is not used, it
doesn't end up linked to the executable.

If your compiler doesn't support function-level linking, and you
really, really want to remove unused functions, what you can do is put
them into separate source files. Have the class declaration in a single
.h of course, but the implementations of the member functions can be
split into multiple files.

Watch out for virtuals. If a function is virtual, then typically, it's
as good as called. Even if you don't use that function, most compilers
generate object code that requires the functions to be there (e.g. a
virtual containing a symbolic reference to the function which must
resolve).


Jun 17 '06 #8

Ian Collins wrote:
ThazKool wrote:

Please don't top post, it's considered rude in these parts.
ThazKool wrote:

I am just wondering if all the methods that are never called upon
during a programs execution are removed when optimizing. For example
you have:

class FooA
{
void a();
...
void z();
}
class FooB ... class FooZ
You use only one method from FooA.c()
Another from FooB.h() and FooC.e(). Shouldn't only the 3 methods
mentioned become added to the final exe file?

If it is only specific compilers could you name which ones. Thanks
again.
May I suggest that you ask in a forum dedicated to your compiler?
That's strictly an implementation issue, beyond the scope of the language.


I said: If it is only specific compilers could you name which ones.
Thanks
again.

This comment was directed to all C++ users. If you are looking to
flame someone, then take a look inside yourself and find out where that
inner child was wounded. Then nurture that inner child and tell him
everything is going to be okay and that he does not have to lash out at
people.

Ideally this should be cross-compiler and cross platform, but I am not
sure and I need the comments of all c++ users. Hence comp.lang.c++.

I don't think any compiler would do this, it isn't the compiler's job.

That's why the question is OT here, you should ask on a platform
specific group, the platform's tools will look after linking.

--
Ian Collins.

I understand your concern. However, I am looking to draw on the wisdom
of the C++ community and not waste time looking at every
implementation. Thanks for you help.

Jun 17 '06 #9
ThazKool wrote:
I understand your concern. However, I am looking to draw on the wisdom of
the C++ community and not waste time looking at every implementation.


I'm worried you are prematurely optimizing. Hard drive space is cheaper
than air these days. Unless you have a huge program or a tiny embedded
space, you should focus on design techniques that help your team rapidly
develop robust code, without worrying about the size of executable images.
They will be big, and our industry generally just lives with this.

--
Phlip

Jun 17 '06 #10

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

Similar topics

4
8013
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned in "Learning Python" by Mark Lutz and David Ascher. It seems like they are a relatively new feature... It seems to me that any truly OO programming language should support these so I'm sure that Python is no exception, but how can these be...
50
6341
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the method foo. But the initialization of attr2 is wrong
4
3712
by: artooro | last post by:
Hi, I'm using some lines of code like this: ClassName *var = new ClassName(...); and that's it. The class constructor does everything I want (there are methods to do more but I don't need them) But this generates a warning (gcc4) saying it's an unused variable. Is there a better way to do this?
9
1729
by: Martijn Mulder | last post by:
A nice feature of the csc.exe compiler is that it warns you if a variable is declared but never used. But unused methods are not flagged, even when the warning level is set to 4.
5
4794
by: Jake K | last post by:
In VStudio 2005 is there an easy way to find unused methods? I have a application that has been restructures several times and I would like toremove any methos that is not used.
8
12231
by: Frank Rizzo | last post by:
Is there a setting in VS2005 to quickly locate methods that are unused (maybe through compiler warnings)? If not, any utilities out there that do that? Thanks
4
1417
by: Rob Meade | last post by:
Hi all, I'm using Visual Studio 2005 and it has as you are probably aware a very handy feature of reporting to you which variables are not being used - cool thanks... What would be even more handy is a way of finding out which of my methods are being used, and across a whole project. I've entered the horrible realms of a project going out of control and I've added bits for testing, and now I'm having to go through the whole project,...
6
6970
by: nickvans | last post by:
Hi all, I have a table called tblRecords that has "DashNum" as its primary key. The lowest value of this table is 116 and the highest value is 269, though there are some missing values. It is expected that records will be removed in the future, leaving more missing values. I would like to have a text box have as its default value the first unused entry in this table. I found a way to do this, but its not very efficient and requires an...
13
9735
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not needed which causes the compiler to spit out a "helpful" warning. For example: % cat unused.c #include <stdio.h> int foo(char *str, void *data) {
0
8385
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
8723
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...
0
8602
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
7316
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
6162
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
5632
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
4150
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...
1
2726
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
2
1941
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.