Joseph Elcid
Joseph Elcid

Reputation: 877

Difference between closure and pointer (c)

In functional programming, Ocaml particularly, when a function is created, a reference to all non-local variable is created so that it can be used even if the non-local variable is out of scope and that is called closure.
And in c programming language, there is the notion of pointer which refers to the memory location of a variable.
The difference then between closures and pointers is in the scope. Is that all?

Upvotes: 1

Views: 237

Answers (1)

alex137
alex137

Reputation: 178

To implement closures, the compiler/interpreter of OCaml must allocate a structure that contains a copy of all referenced variables and a pointer to the function. In C, a function pointer is just a pointer, so there is no allocation.

Upvotes: 3

Related Questions