5

I was reading about the two things and got confused, what are the differences between the two?

user2614516
  • 387
  • 4
  • 8

3 Answers3

7

Binding is a word that is used in more than one context. It always has to do with the connecting of one thing to another however when the act of binding happens can vary.

There is a concept of Binding Time or the point at which some component is bound to some other component. A basic list of binding time is: (1) binding at compile time, (2) binding at link time, (3) binding at load time, and (4) binding at run time.

Binding at compile time happens when the source code is compiled. For C/C++ there are two main stages, the Preprocessor which does source text replacement such as define replacement or macro replacement and the compilation of the source text which converts the source text into machine code along with the necessary instructions for the linker.

Binding at link time is when the external symbols are linked to a specific set of object files and libraries. You may have several different static libraries that have the same set of function names but the actual implementation of the function is different. So you can choose which library implementation to use by selecting different static libraries.

Binding at load time is when the loader loads the executable into memory along with any dynamic or shared libraries. The loader binds function calls to a particular dynamic or shared library and the library chosen can vary.

Binding at run time is when the program is actually running and makes choices depending on the current thread of execution.

So linking is actually just one of the types of binding. Take a look at this stackoverflow static linking vs dynamic linking which provides more information about linking and libraries.

You may also be interested in std::bind in C++ so here is a stackoverflow article std::function and std::bind what are they when they should be used.

The longer you wait before something is bound to something else can provide a needed degree of flexibility in how the software can be used. However often there is a trade off between delaying binding and run time efficiency as well as complexity of the source.

For an example of bind time consider an application that opens a file and reads from the file then closes it. You can pick a couple of different times when the file name is bound to the file open.

You might hard code the file name, binding at compile time, which means that it can only be used with that one file. To change the file name you have to change the source and recompile.

You might have the file name entered by the user such as with a user prompt or a command line argument, binding the file name to the file open at run time. To change the file name you no longer need to recompile, you can just run the program again with a different file name.

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
0

Suppose you have a function declared as:

void f(int, char);

and also as:

void f(int);

And you call the function f(4) with right signature. This is the binding. The linker will link with the available definition of the function body for f matching with signature void f(int);

Dr. Debasish Jana
  • 6,980
  • 4
  • 30
  • 69
0

Actually both are having same meaning in the context of c programming. Some people use binding and others are use linking.

If you want ti know what linking is then here is a short explaination.

Suppose you have made a user defined function called sum() whose declaration is as under int sum(int, int); then whenever function is called from program, your program should know where to jump in memory to execute that function. In simple terms, called function's address should be known to your program inorder to reach to its body which is called binding.

Now sum is user defined function so it will be present in your source code itself. If it is called from main() then it will be linked to main at compile time because at that time compiler will know that where your function will be present in executable. This is called static binding.

Now think about printf() which is library function and its body is not present in your program. So when program is compiled, printf's body will not be present in your compiled executable. It will be loaded into memory when you execute your program and its address will be known to main at run time and not at compile time as it case of sum(). This type of linking is called dynamic linking.

Chintan Patel
  • 310
  • 1
  • 2
  • 11