Hello All,
I am a fresher in c, i need to know about how to find out the size of a function defined & size of stack memory allocated for c compilers

Dani AI

Generated

Short answer: those are two different measurements — the compiled size of a function (how many bytes of machine code it produces) and the stack size/stack usage (how much stack space is reserved for a thread and how much a function actually touches). the trick is to treat them separately. Good pointers already from and ; below are practical, repeatable ways to get concrete numbers on common toolchains.

To find compiled function size (ELF / Unix toolchain)

  • Inspect the symbol table or disassembly of the object or final executable. Symbol tools report each symbol's size; disassembly shows instruction ranges so you can compute function length. Typical commands:
    readelf -Ws myprog | grep myfunction
    nm --size-sort -S myprog | grep myfunction
    objdump -d myprog   # inspect function start/end in output

    For MSVC/PE builds, produce a map file at link time or use dumpbin to show symbols/disassembly and read the Size fields in the map/headers.

To discover stack reservation and per-thread stack size

  • Query the OS/runtime rather than guess. On POSIX, getrlimit(RLIMIT_STACK) shows the stack limit; for the current pthread you can use pthread_getattr_np + pthread_attr_getstack to obtain the thread stack base and size (GNU/Linux). Example (Linux/glibc):
    pthread_attr_t a;
    void *base; size_t size;
    pthread_getattr_np(pthread_self(), &a);
    pthread_attr_getstack(&a, &base, &size);
    printf("stack base=%p size=%zu\n", base, size);

    On Windows the executable PE header contains SizeOfStackReserve/SizeOfStackCommit (settable at link time) and CreateThread accepts an explicit stack size.

Estimating actual stack usage of a function

  • Static: use compiler support (GCC/Clang: -fstack-usage) or static analyzers for embedded targets.
  • Dynamic: stamp the stack with a pattern at startup and, after exercising the code, measure how much pattern was overwritten (common in embedded work).

Practical tips: watch large local arrays and recursion (they increase stack use); for hard limits (embedded) rely on static analysis and testing; for desktop/server code use getrlimit/pthread attributes and toolchain symbol dumps to get reliable numbers.

Recommended Answers

All 3 Replies

An application stack size - see your compiler+linker and OS manuals. For example, Visual C++ linker default is 1M, but you can specify this value:
http://msdn.microsoft.com/en-us/library/tdkhxaks(VS.71).aspx
It's platform-dependent issue - see, for example:
http://www.cs.nyu.edu/exact/core/doc/stackOverflow.txt

Size of compiled code - as usually, you may ask your compiler (and/or linker) to produce module map.

You will scarcely obtain these values (especially function body size) in run time.
Don't get these questions firmly fixed in your head, they are absolutely unimportant on a fresher in C stage...

Don't get these questions firmly fixed in your head, they are absolutely unimportant on a fresher in C stage...

You're right about that -- there was a time 20 years ago when those two questions were very important for programmers because programs were limited to 640K of memory. But times changed and made the two questions nearly obsolete. They are however still relivent on a few obscure operating systems such as embedded systems where memory is very limited. But on a PC running either *nix, MS-Windows, or MAC you don't need to be concerned much about program size or stack size unless you want to write a really gigantic program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.