What is meaning of "nature of entity"? e.g. kind of function, global variable?
Is there any code to check it?

Also, how to detect the memory section in memory map?
e.g. environment, command line arguments, code/, global initialized data, global uninitialized data, stack, heap etc.
Is there any code to check which kind of memory section?

For example, I got a variable named "x1", what kind of code to check the "nature of the entity" + the "memory section"? Thanks.

Dani AI

Generated

is right in spirit: in C the compiler is the authoritative source of an entity's "nature" (function vs object, storage class, type). The language itself provides no built‑in runtime reflection that will say "x1 is a function" or "x1 lives in .bss." "Compile‑time" means the period when the compiler and linker assign types, storage classes and sections; "run‑time" is when the program is executing and most of that metadata is gone from the language level.

Practical approaches you can use:

  • Compile-time / offline: inspect the binary/symbol table. Tools like nm, readelf -s or objdump -t show symbol types (e.g. T for text/code, D for initialized data, B for bss). Ask the linker for a map file or place items in named sections with __attribute__((section("name"))) or a linker script.
  • Run-time (platform specific): take the address of the entity (&x1 for variables, a function pointer for functions) and map it to memory regions. On ELF/Linux you can compare against linker-provided symbols (common names: __executable_start, _etext, _edata, _end) or parse /proc/self/maps; dladdr helps map an address back to a module. On Windows use VirtualQuery. For heap vs stack you can compare the address to a known heap allocation or to the address of a local variable (careful: stack layout and growth direction are platform dependent).

Minimal ELF-style sketch:

extern char __executable_start, _etext, _edata, _end;

const char *section_of(const void *p) {
    const char *a = (const char*)p;
    if (a >= &__executable_start && a < &_etext) return "text";
    if (a >= &_etext && a < &_edata) return "data";
    if (a >= &_edata && a < &_end) return "bss";
    return "heap/stack/other";
}

Notes and cautions for : local (non‑symbol) variables often have no entry in the symbol table. Pointer/address arithmetic and casting to integers is implementation‑defined behavior; for reliable results, prefer platform APIs or maintain explicit bookkeeping (custom allocator, debugging symbols) rather than relying on undefined or fragile heuristics.

Recommended Answers

All 3 Replies

That's not possible unless you are writing a compiler and then it would be a compile-time check, not a runtime check.

That's not possible unless you are writing a compiler and then it would be a compile-time check, not a runtime check.

Hi Ancient Dragon, I am new in C programming area.
May I know what is the so-called "compile-time" and "runtime"?

Thanks for helping... =)

"compile-time" is when the compiler compiles the program. "run time" is when you actually execute the 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.