473,396 Members | 2,093 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,396 software developers and data experts.

function size

Is there any way to find function size in C ?

Regards,
Galoma B
Nov 14 '05 #1
9 8790
>Is there any way to find function size in C ?

Only in an extremely unportable manner (like, for example, running
the UNIX command "size" on the object file when only one function
is present in the compilation unit).

Incidentally, if you have 4 separate functions in 4 separate files,
there is no guarantee that the sizes of these match the combined
size when you put all 4 functions in the same file and look at the
size of the combined object file. In this situation, "function size"
as a concept is a bit fuzzy.

Gordon L. Burditt
Nov 14 '05 #2
On 15 Aug 2004 20:22:56 -0700, bg*****@yahoo.com (Paul) wrote in
comp.lang.c:
Is there any way to find function size in C ?

Regards,
Galoma B


No, not in the standard C language.

Your toolset might have some facilities for providing this
information, but that is a tool specific issue, not a language one.

In C types and objects, and only types and objects, have size. A
function is neither of these.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
bg*****@yahoo.com (Paul) writes:
Is there any way to find function size in C ?


Not portably.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Keith Thompson <ks***@mib.org> wrote in message news:<ln************@nuthaus.mib.org>...
bg*****@yahoo.com (Paul) writes:
Is there any way to find function size in C ?


Not portably.


Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.
Nov 14 '05 #5
Paul wrote:
Keith Thompson <ks***@mib.org> wrote in message news:<ln************@nuthaus.mib.org>...
bg*****@yahoo.com (Paul) writes:
Is there any way to find function size in C ?


Not portably.

Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.


I've already researched this issue. The information that
other posters have given you is true: There is no portable
method to find the size of a function using _standard_ C.
(Search the newsgroups for "function size"
author == "Thomas Matthews")

Your options are:
1. Write the whole function in assembly language.

2. Check your compiler and linker manuals to see how you
can place functions into specific areas or segments.

3. Compile the function into a binary file and place it
into a constant array of bytes.

I chose #1 because it was faster than to use #2. Some
of my associates use option #3. You issue is well known
when code has to be copied from a source container in
order to be executed.

Try also news:comp.arch.embedded
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #6
On 16 Aug 2004 06:24:10 -0700
bg*****@yahoo.com (Paul) wrote:
Keith Thompson <ks***@mib.org> wrote in message
news:<ln************@nuthaus.mib.org>...
bg*****@yahoo.com (Paul) writes:
Is there any way to find function size in C ?
Not portably.


Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C.


As stated. Not portably. I.e. the C language provides no mechanism to do
this but some language extensions might. Any such language extensions
are outside the scope of what is discussed here so you will have to ask
in a group dedicated to your implementation and/or OS.
May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???


Not portably. Firstly there is no guarantee that function pointers will
fit in to ANY integer type, including long. Secondly, the compiler is
free to rearrange the functions so that dummy is before foo and main is
between dummy and foo. There *are* implementations which reorder
functions, gcc 3.4 with -O2 being one example, although I don't think it
would put main between dummy and foo.

So go ask in a group/mailing list dedicated to your system if you need
this functionality.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #7
"Paul" <bg*****@yahoo.com> wrote in message
news:d8**************************@posting.google.c om...
Keith Thompson <ks***@mib.org> wrote in message

news:<ln************@nuthaus.mib.org>...
bg*****@yahoo.com (Paul) writes:
Is there any way to find function size in C ?


Not portably.


Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.


While it is not portable, I got the above code to work with VC7 as long as
incremental linking was disabled. I suspect function level linking must also
be disabled (not tested). Further, in release mode, if foo() and dummy() are
both empty they will be optimized to a single RET instruction at the same
address - so the size of foo will come out as zero.

The web page you refer to is obviously for Win32 platforms. However, if you
want to copy a function and execute it you need more information than just
the function address and size. Function calls and references to global
variables must be relocated since relative addressing is used almost
exclusively. When a program or DLL is loaded into memory, the OS relocates
addresses based on the relocation tables inside the Portable Executable
(PE). When copying a function you will need to do the same thing, i.e. read
the relocation info and update the relative addresses. It is not too
difficult but you will need to study the PE format and understand how it
works.

Dag
Nov 14 '05 #8
"Dag Viken" <da*******@earthlink.net> wrote in message news:<WP*******************@newsread2.news.atl.ear thlink.net>...
"Paul" <bg*****@yahoo.com> wrote in message
news:d8**************************@posting.google.c om...
Keith Thompson <ks***@mib.org> wrote in message

news:<ln************@nuthaus.mib.org>...
bg*****@yahoo.com (Paul) writes:
> Is there any way to find function size in C ?

Not portably.


Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.


While it is not portable, I got the above code to work with VC7 as long as
incremental linking was disabled. I suspect function level linking must also
be disabled (not tested). Further, in release mode, if foo() and dummy() are
both empty they will be optimized to a single RET instruction at the same
address - so the size of foo will come out as zero.

The web page you refer to is obviously for Win32 platforms. However, if you
want to copy a function and execute it you need more information than just
the function address and size. Function calls and references to global
variables must be relocated since relative addressing is used almost
exclusively. When a program or DLL is loaded into memory, the OS relocates
addresses based on the relocation tables inside the Portable Executable
(PE). When copying a function you will need to do the same thing, i.e. read
the relocation info and update the relative addresses. It is not too
difficult but you will need to study the PE format and understand how it
works.

Dag


Dag many thanks for your explanation.
Nov 14 '05 #9
Jack Klein wrote:
In C types and objects, and only types and objects, have size. A
function is neither of these.


Specifically, it's "object types", which have sizes.

--
pete
Nov 14 '05 #10

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

Similar topics

35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
9
by: lombrozo | last post by:
Hi, I'd like to ask all of you experienced, well-structured, Object-Oriented programmers out there about function size. I am using Visual C++ to create an application, everything is nicely...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.