2

I had initial apprehensions about posting this question lest it be a duplicate.But even after googling with many keywords,I couldn't find any link on StackOverflow that explains static and dynamic binding for C.There are questions and answers for C++ though,but all involve classes and stuff that are clearly not for C.And the links outside StackExchange were quite dubious.

I need to know the rigorous definition and contrast between these two bindings,exclusively in the context of C.I would appreciate if you can take some time to answer it,or give me the links on StackOverflow for this lest I am mistaken and it has been answered in detail before.

I intend to have a clear idea about:

  1. Binding in C.
  2. Static vs dynamic binding in C.

Edit It would be immensely helpful if you could explain the difference with some simple code snippets.

Mike
  • 47,263
  • 29
  • 113
  • 177
Rüppell's Vulture
  • 3,583
  • 7
  • 35
  • 49

2 Answers2

5

Formally, there are no such terms in "native" C.

Simplified explanation of the terms static binding ("early binding") and dynamic binding ("late binding"): they are most often used in object-orientated design, to determine whether the decision to call a particular inherited member function is done at compile time or in run time.

The meaning of a virtual function is that it's an inherited function which gets called instead of the equivalent function in the base class which was inherited. If the compiler can determine whether an object is of type "base class" or type "inherited class" at compile time, you get static binding, otherwise dynamic binding. So you would need some sort of runtime type information (RTTI).

In the above context, these terms only make sense if you are using object-oriented inheritance/polymorphism in your C program. C has no language support for such mechanisms. It is possible to implement them "manually" in C, but it is tedious and the code tends to be quite a mess. For those who insist, there is a book "Object oriented design in ANSI-C" which demonstrates how it can be done.

(Personally I would not recommend that book, nor to implement polymorphism in C. If you need those OOP feaures, just code in C++.)

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • So until I move to OOPS, shall this thumb rule suffice for me -- `In C,there is only static binding.The compiler takes care of all binding at compile time.No dynamic binding at all.End of story!!`.Right? – Rüppell's Vulture May 07 '13 at 14:26
  • @Rüppell'sVulture: Actually, you *can* fake dynamic binding in C through function pointers (that's the bit about implementing it "manually"), but it typically doesn't "feel" like dynamic binding in C++, if that makes any sense. – John Bode May 07 '13 at 14:59
  • 2
    @Rüppell'sVulture A virtual member function table can be simulated with an array of function pointers etc. You can implement all needed OOP features in C, there is just no fancy language support for them. – Lundin May 07 '13 at 17:15
1

C is a statically compiled language, it doesn't really have "dynamic binding".

You can do it manually using API:s such as POSIX' dlopen(), but I would hesitate to call that "binding" although in a sense I guess it is.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Consider the case of `dynamic` memory allocation using `malloc()`.I may not have used the right term `dynamic`.Still, how would you describe cases like `malloc()` in C? – Rüppell's Vulture May 07 '13 at 14:02
  • 1
    @Rüppell'sVulture There seems to be some kind of terminology mis-match; binding is generally used to refer to the process of "connecting" names (variable names, function names, type names, whatever) with their "meaning", in programming languages. That has nothing to do with memory allocation ... – unwind May 07 '13 at 14:05
  • But if so, isn't that critical and important "connecting" supposed to be done at compile time for ALL programming languages?How possibly,and using what procedure could such "connecting" be allowed to be done at runtime in ANY language?And what possible benefit would accrue from that?And what is the need for that? – Rüppell's Vulture May 07 '13 at 14:18
  • In programming, dynamic typically means "in run-time". We have dynamic binding, dynamic memory, dynamically linked libraries and so on. – Lundin May 07 '13 at 17:17