Home > Enterprise >  Name binding in C
Name binding in C

Time:02-14

This is more of a theoritical question.Is it possible to bind multiple names to a single object?(either in the stack or heap memory).I know that pointers are not actually considered binding,since the assigned value is a memory address.

CodePudding user response:

Objects residing in the heap are only available as pointers returned by allocation functions, hence bindings do not really apply to this type of object, and it is quite obvious that multiple pointers can point to the same object.

Objects defined with automatic storage, aka on the stack, cannot have multiple bindings as each object definition defines a single identifier. Parts of union objects can alias the same memory area, and can be referenced via different members, but again, not exactly bindings either.

Global objects (data or code) have the same restrictions, but the linker may map different names to the same location and therefore create different bindings for the same object, possibly with different types. These are usually called aliases and can be controlled with compiler specific pragmas and attributes. This is mostly used for library functions and is beyond the scope of the C Standard, where term binding is not defined, nor used for this purpose.

If the compiler is not aware of these aliases, it may assume that objects defined with different names do not alias one another, hence generate code that may produce unexpected results.

CodePudding user response:

In C, an object is a:

region of data storage in the execution environment, the contents of which can represent values (§3.5) Thus,

union {
    int a;
    int b;
} u;

binds u.a and u.b to the same object. (Both names happen to have the same type, but that's not required.)

We could argue about whether a and b are strictly speaking bindings, but the standard seems to say that they are:

An identifier can denote an object; a function; a tag or a member of a structure, union, or enumeration; a typedef name; a label name; a macro name; or a macro parameter.

  • Related