Logo

Programming-Idioms

Set n to the number of bytes of a variable t (of type T).
New implementation

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
N : Integer := (T'Size + 7) / 8;
n = sizeof (t);
#include <cstddef>
std::size_t n = sizeof(t);
using System.Runtime.InteropServices;
int n = Marshal.SizeOf(t);
int n;
unsafe
{
    n = sizeof(T);
}
int n = sizeof(T);
auto n = T.sizeof;
use iso_c_binding
n = c_sizeof(i)
import "reflect"
var t T
tType := reflect.TypeOf(t)
n := tType.Size()
<T> int bytes(T t) {
    return switch (t.getClass().getSimpleName()) {
        case "Boolean" -> 1;
        case "Byte" -> Byte.BYTES;
        case "Short" -> Short.BYTES;
        case "Character" -> Character.BYTES;
        case "Integer" -> Integer.BYTES;
        case "Float" -> Float.BYTES;
        case "Long" -> Long.BYTES;
        case "Double" -> Double.BYTES;
        default -> -1;
    };
}
n := SizeOf(T);
use Devel::Size qw(total_size);
my $n = total_size $t;
import pympler.asizeof
n = pympler.asizeof.asizeof(t)
require 'objspace'
n = ObjectSpace.memsize_of(t)
let n = std::mem::size_of::<T>();