NCL Home > Documentation > Functions > Variable query

sizeof

Returns the total size, in bytes, of the input variable.

Prototype

	function sizeof (
		data       
	)

	return_val [1] :  integer

Arguments

data

A variable of any type or dimensionality.

Description

The sizeof function returns an integer value that is the size in bytes of the data pointed to by the parameter data. For variables like file pointers and graphical objects, this function is not very useful as it just returns the size of the pointer that points to the file or object.

Also, this function does not return the size of string data correctly. If you want the string length of a string, you first need to convert it to a character array, and then subtract 1 to account for the end-of-string character that stringtocharacter adds:

   length = dimsizes(stringtochar(str))-1

Examples

The example below shows how sizeof works for different types and dimensionalities:

Example 1

  carray = new((/2,3/),character)
  sarray = new(5,short)
  iarray = new(1,integer)
  larray = new((/1,2/),long)
  farray = new((/2,3,4,5/),float)
  darray = new((/2,3,4,5/),double)

  print(sizeof(carray))       ; should be 6
  print(sizeof(sarray))       ; should be 10
  print(sizeof(iarray))       ; should be 4
  print(sizeof(larray))       ; should be 8 (or 16 on 64-bit machine)
  print(sizeof(farray))       ; should be 480
  print(sizeof(darray))       ; should be 960

;
; Variable types for which "sizeof" doesn't provide much information.
;
  f    = addfile("$NCARG_ROOT/lib/ncarg/data/cdf/Pstorm.cdf","r")
  plot = new(1,graphic)
  str  = "Comedy is not pretty"     ; quote by Steve Martin

  print(sizeof(f))      ; Should be 4 (or 8 on 64-bit machine)
  print(sizeof(plot))   ; Should be 4 (or 8 on 64-bit machine)
  print(sizeof(str))    ; Should be 4 (or 8 on 64-bit machine)