Memory representation of variables

Abstract

In this page, we present how Scilab v5 stores the variables. More precisely, we present the internal storage of doubles, booleans, strings, polynomials, sparse matrices and lists.

How does Scilab store variables in memory ?


In the stack, Scilab mixes integers ( 32 bits ) and doubles ( 64 bits ) so we need to use specific commands to convert addresses.

Commands to access stack values :


Matrix of double

Type

32 bits

1 integer from Scilab Internal Datatypes

Lines

32 bits

1 integer, number of lines in the matrix, for a single number, Scilab store it like a matrix [1,1]

Columns

32 bits

1 integer, number of columns in the matrix, for a single number, Scilab store it like a matrix [1,1]

Complex

32 bits

1 integer, 0 for real number, 1 for complex number

Real part

n * 64 bits

Each real part is store in a double

Imaginary part

n * 64 bits

Each imaginary part is store in a double

iAddrBase is the address ( scilab format ) of Type :

Type = *istk( iAddBase ); // Returns variable type
Lines = *istk( iAddrBase + 1 ); // Returns number of lines in the matrix
Columns = *istk( iAddrBase + 2 ); // Returns number of columns in the matrix
Complex = *istk( iAddrBase + 3 ); // Returns 0 if real, 1 if complex
Real_Part = stk( sadr( iAddrBase + 4 ) ); // Returns a pointer on a real array of double values, size = Lines * Columns
Img_Part = stk( sadr( iAddrBase + 4 + Lines * Columns ) ); // Returns a pointer on a imaginary array of double values, size = Lines * Columns

Sample for a real matrix [2, 3] :

A = [ 1 , 2 , 3 ]
    [ 4 , 5 , 6 ]  

Header
4 * 32 bits

1

2

3

0

Real
n * 64 bits

1.00

4.00

2.00

5.00

3.00

6.00


Sample for a complex matrix [2,3] :

B = [ -i      , 2 - 3i , -4 + 5i  ]
    [ -6 - 7i , -9     , 10 + 11i ]  

Header
4 * 32 bits

1

2

3

1

Real
n * 64 bits

0.00

-6.00

2.00

-9.00

-4.00

10.00

Imag
n * 64 bits

-1.00

-7.00

-3.00

0.00

5.00

11.0



Matrix of boolean

Type

32 bits

1 integer from Scilab Internal Datatypes

Lines

32 bits

1 integer, number of lines in the matrix (a single value is stored as a [1,1] matrix)

Columns

32 bits

1 integer, number of columns in the matrix (a single value is stored as a [1,1] matrix)

Values

n * 32 bits

Each boolean value is stored as an integer (0 for false, 1 for true)

iAddrBase is the address ( scilab format ) of Type :

Type = *istk( iAddBase ); // Returns variable type
Lines = *istk( iAddrBase + 1 ); // Returns number of lines in the matrix
Columns = *istk( iAddrBase + 2 ); // Returns number of columns in the matrix
Values = *istk( iAddrBase + 3 ); // Returns a pointer to integer array of boolean values (0 for false, 1 for true),size = Lines * Columns

Sample for a boolean matrix [2, 3] :

A = [ %T , %T , %F ]
    [ %F , %T , %F ]  

Header
3 * 32 bits

4

2

3

Integer
n * 32 bits

1

0

1

1

0

0


Matrix of strings

Type

32 bits

1 integer from Scilab Internal Datatypes

Lines

32 bits

1 integer, number of lines in the matrix, for a single string, Scilab store it like a matrix [1,1]

Columns

32 bits

1 integer, number of columns in the matrix, for a single string, Scilab store it like a matrix [1,1]

Complex

32 bits

1 integer, 0 for real number, 1 for complex number

Offset

n * 32 bits

Offset between each string, store in a integer

Data

Sum(Sizes) * 32 bits

Each character is store in a Scilab Character Representation (Scicode)


Type = *istk( iAddrBase ); // Returns variable type
Lines = *istk( iAddrBase + 1 ); // Returns number of lines in the matrix
Columns = *istk( iAddrBase + 2 ); // Returns number of columns in the matrix
Complex = *istk( iAddrBase + 3 ); // Returns 0
Offset = istk(iAddrBase + 4 ); // Returns an array of offset, size = Lines * Columns + 1
Data = stk( iAddrBase + 4 + 1 + Lines * Columns ); // Returns a pointer on the first charater of the first string, size = Offset[MAX-1] - 1

A = [ "Scilab"   , "is"
      "a"        , "beautiful"
      "software" , ":)"]  

Header
4 * 32 bits

10

3

2

0

Offset
(n + 1) * 32 bits

String 1

1

String 2

7

String 3

8

String 4

16

String 5

18

String 6

27

End of Offset

29

String 1.1
"Scilab"

'S'

-28

'c'

12

'i'

18

'l'

21

'a'

10

'b'

11

String 2.1
"a"

'a'

10

String 3.1
"software"

's'

28

'o'

24

'f'

15

't'

29

'w'

32

'a'

10

'r'

27

'e'

14

String 1.2
"is"

'i'

18

's'

28

String 2.2
"beautiful"

'b'

11

'e'

14

'a'

10

'u'

30

't'

29

'i'

18

'f'

15

'u'

30

'l'

21

String 3.2
":)"

':'

44

')'

42



Matrix of polynomials

Type

32 bits

1 integer from Scilab Internal Datatypes

Lines

32 bits

1 integer, number of lines in the matrix, for a single number, Scilab store it like a matrix [1,1]

Columns

32 bits

1 integer, number of columns in the matrix, for a single number, Scilab store it like a matrix [1,1]

Complex

32 bits

1 integer, 0 for real number, 1 for complex number

Variable name

4 * 32 bits

Name of the variable in the polynomial

Offset

n * 32 bits

Offset in memory between each polynomial

Real part

n * 64 bits

each real part is store in a double

Imaginary part

n * 64 bits

each imaginary part is store in a double


Type = *istk( iAddBase ); // Returns variable type
Lines = *istk( iAddrBase + 1 ); // Returns number of lines in the matrix
Columns = *istk( iAddrBase + 2 ); // Returns number of columns in the matrix
Complex = *istk( iAddrBase + 3 ); // Returns 0 if real, 1 if complex
Variable Name = *istk( iAddrBase + 4 ); // Returns a pointer on an array of interger, size = 4
Offset = istk(iAddrBase + 8 ); // Returns an array of offset, size = Lines * Columns + 1
Real_Part = stk( sadr( iAddrBase + 8 + 1 + Lines * Columns ) ); // Returns a pointer on a real array of double values, size = Offset[MAX-1] - 1
Img_Part = stk( sadr( iAddrBase + 8 + Lines * Columns + Offset[MAX-1]) ); // Returns a pointer on a imaginary array of double values, size = Offset[MAX-1] - 1

Sample for a matrix of real polynomials :

A = [ x + 2,
      3 * x^2 -4x + 5,
     -6 * x^3 + 7x - 8]  

Header
4 * 32 bits

2

3

1

0

Variable
name
4 * 32 bits

x

Offset
(n +1)* 32 bits

Poly 1

1

Poly 2

3

Poly 3

6

End of Offset

10

Real coefficients
n * 64 bits

Poly 1

x^0

2.00

x^1

1.00

Poly 2

x^0

5.00

x^1

-4.00

x^2

3.00

Poly 3

x^0

-8.00

x^1

7.00

x^2

0.00

x^3

-6.00


Sample for a matrix of complex polynomials :

A = [ (i-2) * x + (-3i + 4),
    (5 - 6i) * x^2 + (8i) * x + (9 - 10i),
    (-11 + 12i) * x^3 + (-13)* x + (15 - 16i)]  

Header
4 * 32 bits

2

3

1

1

Variable
name
4 * 32 bits

x

Offset
(n +1)* 32 bits

Poly 1

1

Poly 2

3

Poly 3

6

End of Offset

10

Real coefficients
n * 64 bits

Poly 1

x^0

-2.00

x^1

4.00

Poly 2

x^0

9.00

x^1

0.00

x^2

5.00

Poly 3

x^0

15.00

x^1

-13.00

x^2

0.00

x^3

-11.00

Imag coefficients
n * 64 bits

Poly 1

x^0

1.00

x^1

-3.00

Poly 2

x^0

-10.00

x^1

8.00

x^2

-6.00

Poly 3

x^0

-16.00

x^1

0.00

x^2

0.00

x^3

12.00



Sparse matrix

Type

32 bits

1 integer from Scilab Internal Datatypes

Lines

32 bits

1 integer, number of lines in the matrix, for a single number, Scilab store it like a matrix [1,1]

Columns

32 bits

1 integer, number of columns in the matrix, for a single number, Scilab store it like a matrix [1,1]

Complex

32 bits

1 integer, 0 for real number, 1 for complex number

Total items

32 bits

Number of item in the sparse

Line items

n * 32 bits

number of item for each line

Column position

n * 32 bits

Column number for each items

Real part

n * 64 bits

each real part is store in a double

Imaginary part

n * 64 bits

each imaginary part is store in a double


Type = *istk( iAddBase ); // Returns variable type
Lines = *istk( iAddrBase + 1 ); // Returns number of lines in the matrix
Columns = *istk( iAddrBase + 2 ); // Returns number of columns in the matrix
Complex = *istk( iAddrBase + 3 ); // Returns 0 if real, 1 if complex
Total Items = *istk( iAddrBase + 4 ); // Returns number of items in the sparse
Line Items = istk(iAddrBase + 5 ); // Number of items in each Line, size = Lines
Column position = istk(iAddrBase + 5 + Lines ); // Number of items in each Line, size = Total Items
Real_Part = stk( sadr( iAddrBase + 5 + Lines + Total Items) ); // Returns a pointer on a real array of double values, size = Total Items
Img_Part = stk( sadr( iAddrBase + 5 + Lines + Total Items + Total Items ); // Returns a pointer on a imaginary array of double values, size = Total Items

Sample for real sparse :

A =

0

1

0

0

0

0

4

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

2

0

0

5

0

3

0

0

6

0

0

A = (4, 10)
    (1,2)  = 1
    (1,7)  = 4
    (3,10) = 2
    (4,3)  = 5
    (4,5)  = 3
    (4,8)  = 6   

Header
4 * 32 bits

5

4

10

0

Sparse
configuration
n * 32 bits

Total Items

6

Line Items

2

0

1

3

Column
position

2

7

10

3

5

8

Real
n * 64 bits

1

4

2

5

3

6


Sample for complex sparse :

A =

0

-i

0

0

0

0

-6+7i

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

0

-2+3i

0

0

8-9i

0

4-5i

0

0

-10

0

0

A = (4, 10)
    (1,2)  = -i
    (1,7)  = 6+7i
    (3,10) = -2+3i
    (4,3)  = 8-9i
    (4,5)  = 4-5i
    (4,8)  = -10   

Header
4 * 32 bits

5

4

10

1

Sparse
configuration
n * 32 bits

Total Items

6

Line Items

2

0

1

3

Column
position

2

7

10

3

5

8

Real
n * 64 bits

0

6

-2

8

4

-10

Imaginary
n * 64 bits

-1

7

3

-9

-5

0



List

Type

32 bits

1 integer from Scilab Internal Datatypes

Items

32 bits

1 integer, number of items in the list

Offset

n * 32 bits

Offset between each item

Type = *istk(iAddrBase); // Returns variable type
Items = *istk(iAddrBase + 1); // Returns the numbers of items in the list
Offset = istk(iAddrBase + 1); // Returns array of offset between each item

In the list all items are store like standard variables.

Sample : List with 3 items

  1. Matrix of double ( 2 x 2 )

  2. List with 2 items

    1. Matrix of double complex ( 2 x 2 )

    2. Matrix of string ( 2 x 2 )

  3. Matrix of double ( 2 x 2 )


A = { 
      Item 1 : [ 1, 2
                 3, 4],
      Item 2 : { 
          Item 2.1 : [ 5 + 6i    , 7 - 8i
                       -9 - 10i   , -11 + 12i ],
          Item 2.2 : [ "Scilab"  , "5.0.1"
                       "is"      , "released" ] },
      Item 3 : [ 13, 14
                 15, 16 ] } 

Header
2 * 32 bits

15

3

Offset
(n + 1) * 32 bits

Item 1

1

Item 2

7

Item 3

9

End of Offset

29

Item 1

Header 1
4 * 32 bits

1

2

2

0

Real
n * 64 bits

1.00

3.00

2.00

4.00

Item 2

Header 2
2 * 32 bits

15

2

Offset
(n + 1) * 32 bits

Item 2.1

1

Item 2.2

11

End of Offset

2.1

Item 2.1

Header 2.1
4 * 32 bits

1

2

2

1

Real
n * 64 bits

5.00

-9.00

7.00

-11.00

Imag
n * 64 bits

6.00

-10.00

-8.00

12.00

Item 2.2

Header 2.2
4 * 32 bits

10

2

2

0

Offset
(n + 1) * 32 bits

String 1

1

String 2

7

String 3

9

String 4

10

End of Offset

29

String 1.1
"Scilab"

'S'

-28

'c'

12

'i'

18

'l'

21

'a'

10

'b'

11

String 2.1
"is"

'i'

18

's'

28

String 1.2
"5.0.1"

'5'

5

'.'

51

'0'

0

'.'

51

'1'

1

String 2.2
"released"

'r'

27

'e'

14

'l'

21

'e'

14

'a'

10

's'

28

'e'

14

'd'

13

Item 3

Header 3
4 * 32 bits

1

2

2

0

Real
n * 64 bits

13.00

15.00

14.00

16.00



public: Memory representation of variables (last edited 2012-08-06 11:29:42 by michael.baudin@contrib.scilab.org)