473,385 Members | 1,356 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,385 software developers and data experts.

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 3157
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.701731.267900
@i40g2000cwc.googlegroups.com>, Ch**********@gmail.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
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...
50
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...
4
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...
9
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
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
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
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...
6
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...
13
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...
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.