Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking record size in ocaml?

Tags:

ocaml

Is there any way to check the size of a record in Ocaml? Something like sizeof of C/C++?

like image 893
akoprowski Avatar asked Oct 28 '10 12:10

akoprowski


People also ask

What does :: do in OCaml?

It is usually used if you have a function or value that is very similar to some other, but is in some way new or modified. Regarding the :: symbol - as already mentioned, it is used to create lists from a single element and a list ( 1::[2;3] creates a list [1;2;3] ).

What does asterisk mean in OCaml?

The * symbol is used to separate elements of a tuple in data type definitions.

Does OCaml round up or down?

To get from the OCaml representation to the integer, divide by two and round down.

What does some mean in OCaml?

Some is a constructor of the option type. The definition is roughly: type 'a option = Some of 'a | None.


2 Answers

Yes:

# Obj.size (Obj.repr (1,2,3,4,5)) ;;
- : int = 5

But for a record type, the size only depends on the type declaration, so you could just infer it from that.

The actual size occupied in memory is the number returned by Obj.size plus one in words. Words are 32 or 64 bit depending which OCaml version you are using. The additional word is used for book-keeping.

like image 149
Pascal Cuoq Avatar answered Sep 20 '22 17:09

Pascal Cuoq


Besides Obj module, there is also a Objsize library from Dmitry Grebeniuk ( http://forge.ocamlcore.org/projects/objsize/ ). It allows you to get more detailed info about values and its size.

like image 41
larhat Avatar answered Sep 19 '22 17:09

larhat