开发者

Beginner question: What is binding?

开发者 https://www.devze.com 2022-12-28 10:46 出处:网络
I was trying to understand the difference between early and late binding, and in the process realized that the concept of binding is nebulous to me.I think I understand that it relates to the way data

I was trying to understand the difference between early and late binding, and in the process realized that the concept of binding is nebulous to me. I think I understand that it relates to the way data-as-a-word-of-memory is linked to type-as-a-set-of-language-features but I am not sure those are the right concepts. Also, how does under开发者_如何转开发standing this deeply help people become better programmers?

Please note: This question is not "what is late v. early binding" or "what are the trade-offs between the 2". Those already exist here.

Thanks,

JDelage


At its simplest, binding is the association of a symbol within a program with an address in memory.

For example: a function call in C. When you declare a function, the compiler records the function's name and the location of its code within the object file. When you call a function from a separately compiled file, the compiler records a reference to that name in the place where the call occurs. The linker is responsible for binding these two references, so that the call will reference the correct memory location.


Binding in general is associating a name with some value. The value need not be data but can be anything that has a name, e.g. a function or a class.

As for tradeoff:

  • Early binding makes the value available earlier. So for example compilers may apply optimization based on the known value, like evaluating constant expressions. This may result in better performance, and it requires no runtime support for retrieving the value.

  • Late binding requires the value later, so it provides greater flexibility. It becomes easier to change parts of the code or reconfigure a system.

The general trend is towards later binding, because with faster processors and better techniques (e.g. JIT compilation) it becomes feasible more often.


"Binding" is basically saying "this function/property name corresponds to this bit of code".


In the context of compilers, binding is the phase where the address references are changed into actual absolute addresses.

When the program is compiled, the address of each symbol (variable, function) is stored in symbol table for example as a relative offset from the beginning of the object module, together with the symbol name. The symbol name is needed since the symbols may be called from another object module.

When the program is linked, the object modules are combined into a single program file and the symbol names are no more needed. If the program is linked into known absolute address, all the address references can be bound into absolute addresses already at this phase.

However, in workstations such as PC:s, the program can be loaded to any address, so the address is not known at link time. Therefore, additional relocating information is stored in the program file so that the loader can bind the addresses at load time.

Binding is done at load time using the relocation information. When the address where the program is going to be run is known, the loader replaces the relative addresses with absolute addresses using the relocation information that tells where in the code the changes need to be done.

For dynamic objects/variables, binding can be done at runtime. (I think this is what is usually called late binding.)

Usually you do not need to care much about binding, at least not when early binding is used. (However, binding at runtime may have negative impact to performance and security.)


If you are talking more about databinding, then I'd say the definition used by Microsoft in their Data Binding Overview works quite well:

"Data binding is the process that establishes a connection between the application UI and business logic. If the binding has the correct settings and the data provides the proper notifications, then, when the data changes its value, the elements that are bound to the data reflect changes automatically. Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change."

In other words, it's a way of getting data from a data-source (such as a database or XML file) and applying that data to an interface or display element. For instance, a form might have a drop-down list of values that are stored in a table within a database. Data-binding is the process of 'binding' the values from the database to the list. One-way data-binding is usually 'read-only' but two-way data-binding allows the user to update the values back to the underlying data-source.

0

精彩评论

暂无评论...
验证码 换一张
取 消