Static Variables
Scalars and Arrays
a scalar is a variable that stores a single value
an array stores multiple values named by a single variable and an index
Now for some other types of variables...
Variables that are an instance of a class or struct
Java vs C
Accessing an instance variable
A struct is a
Declarations
struct D d0;
struct D* d1;
Access
d0.e = d0.f;
d1->e = d1->f
xxxxxxxxxx
41struct D {
2 int e;
3 int f;
4}
Static structs are allocated by the compiler
Dynamic structs are allocated at runtime
malloc
runtime allocation of dynamic struct
xxxxxxxxxx
31void foo() {
2 d1 = malloc (sizeof(struct D));
3}
assume that this code allocates the struct at address 0x2000
Static and dynamic differ by an extra memory access
dynamic structs have dynamic address that must be read from memory
in both cases the offset to variable from base of struct is static
The revised load/store base plus offset instructions
dynamic base address in a register plus a static offset (displacement)
ld 4(r1), r2
Machine format for base+offset
The revised ISA
Scalars
(r1)
ld (r1), r0
Arrays
ld (r1,r2,4), r0
Struct Members (Instance Variables)
ld X(r1), r0
The struct variable is
Struct variables can be declared inside other structs
Programs can allocate memory dynamically
In Java, instances of classes are allocated by the new statement
In C, byte ranges are allocated by call to malloc procedure
Memory allocation
void* malloc(int n);
n is the number of of bytes to allocate
returning type is void*
Use sizeof to determine number of bytes to allocated struct Foo* f = malloc(sizeof(struct Foo));
Wise management of memory requires deallocation
In Java:
In C:
The heap is a large section of memory from which malloc allocates objects
all objects are stored in the heap
What free(x) does
What free(X) does not do