0% found this document useful (0 votes)
41 views

CS-240 Data Structures in C Arrays: Dick Steflik

The document summarizes key concepts about arrays in C including: 1) Arrays are indexed collections that can store values of a given data type. In C, arrays use zero-based indexing and elements must be contiguous in memory. 2) Common array operations include create, store, retrieve, and destroy which allocate, access, and free the array's memory. 3) Structures allow grouping different data types together. Self-referential structures like linked lists use pointers to connect elements. 4) Multidimensional arrays map to linear memory in row-major order. Sparse arrays optimize storage for arrays with many zero elements.

Uploaded by

sunnysurat
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

CS-240 Data Structures in C Arrays: Dick Steflik

The document summarizes key concepts about arrays in C including: 1) Arrays are indexed collections that can store values of a given data type. In C, arrays use zero-based indexing and elements must be contiguous in memory. 2) Common array operations include create, store, retrieve, and destroy which allocate, access, and free the array's memory. 3) Structures allow grouping different data types together. Self-referential structures like linked lists use pointers to connect elements. 4) Multidimensional arrays map to linear memory in row-major order. Sparse arrays optimize storage for arrays with many zero elements.

Uploaded by

sunnysurat
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

CS-240

Data Structures in C
Arrays

Dick Steflik
Abstract Data Type
• A collection of pairs <index,value>
where index is an ordered set of integers
and are values of some data type that is
constant for the array.
• not all languages require index to be
continuous or contiguous or start at 0 or 1.
• In C arrays are zero based and are
contiguous from 0 to size-1 and can
contain any simple or aggregate data type
ADT (cont.)
• Pascal allows discontinous indicies
– A(2:5, 10:20, 26) other index values are
undefined and take up no memory
• Perl allows indicies that are not integers
but are literal values (called an associative
array)
– A[tom] , A[dick], A[harry]
ADT (cont.)
• Static arrays – arrays allocated at compile
time
• Dynamic arrays – arrays allocated by the
storage management system at program
run time
ADT Operations
• Basic operations:
create(A) – allocates storage
retrieve(A,i) – return v at position i in A
store(A,I,v) – store v at position i in A
destroy(A) – deallocate storage associated
with A
create
• static storage : int a[10]; //40 bytes
char word[25]; //25 bytes
– allocated as part of the program space by the
compiler.
– &a is equivalent to a and is the address of
a[0]
– once allocated cannot be deallocated, will
always take up program space
• can be initialized by compiler using an
initializer (ex. int A[5] = (0,0,0,0,0); )
create
• Dynamic : storage is allocated at run-time using
malloc, cmalloc or realloc
#define SIZE 10
int * myarray;
myarray = (int *) malloc (SIZE*sizeof(int));
makes an array of 10 integers names myarray
• cmalloc works same way but initializes array to 0
initialization to anything else requires a loop
• realloc will resize a previously allocated array to
bigger or smaller
• since this happens at run time, time is expended
store
• done the same way for both static and
dynamic arrays by using the assignment
operator (=)

a[5] = 9;
retrieve
• retrieving a value from some position in an
array is done the same way for both static
and dynamic arrays using the array
position implicitly.
x[3] ; //the value of the 4th element of x
• can be used this way in any assignment,
arithmetic/logical operation or as an
argument in a function call
destroy
• destruction of a statically allocated array
happened when the program is done
• destruction of dynamically allocated arrays is
done using the free(arrayname) function, this
returns the storage to the storage management
system for subsequent allocation for something
else.
• forgetting to deallocate unneeded storage is
called a “memory leak” and can cause a
program terminate abnormally (crash)
memory
• remember, a computer’s memory is really an
array of bytes (indicies 0 to size-1)
• every time an array access (retrieve or store) is
done the machine must make a calculation to see
where in memory the desired location is:
ex int a[5]; a[3]=2;
to calculate the address of a[3]
address=base address+(index*element size)
= 100016 + (316*416) = 100c16
base address is assigned by compiler for static and by SMS for dynamic and kept track
of in a system table for run time
Structures
• Allows us to create and aggregate data
type:
typedef struct person
{ char name[10];
int age;
}
person tom;
- tom takes up 14 bytes of storage; 10
for name and the next 4 for age
Structures
• Structures can be embedded within one
another:

typedef struct date typedef struct student


{ int month; { char name[16];
int date;
date dateOfBirth;
int year;
};
};

date pearlHarborDay; // 12 bytes of storage


student typical; // 18 bytes of storage
student class[30]; // 540 bytes of storage
Unions
• A union is like a structure but the fields don’t
always have to have the same definition
typedef struct sextype typedef struct human
{ enum tag (female, male) sex; { char name[10];
union { short age;
int children; float salary;
char beard; date dob;
} u; sextype sexinfo;
}; };

The compiler will always reserve the maximum number bytes for the union; i.e.
even though sextype for wormen is 4 bytes and only one byte for men the compiler
will always reserve 4.
Self-Referential Structures
• Structure that refers to an item of the
same type.
• used for dynamic data structures like lists
and trees.

typedef struct node


{ int key;
node * next;
}
Array Mapping Functions
• Used by the compiler to help calculate the
effective address of an array element in
memory
• Takes into account:
base address
the dimension
the element size
2 dimensional arrays
• int a[2][2] can be visualized as a 2x2
square matrix but is really an array of two
elements where each element is an array
of two ints
0 1
0 1
0 0

0 1
1 1
cont.
int a[2][2]

0123
0x100000 0,0
0x100004 0,1 This storage arrangement is known as:
0x100008 1,0 Row Major Order
0x10000C 1,1

a[m]
[n])
SMF = base addr + (dim(n) * element size * index m ) + (element size *
indexn )

ex. a[1][1]
addr = 100000 + (2 * 4 * 1) + (4 * 1)
= 100000 + 8 + 4 = 0x10000C
Sparse Arrays
• arrays where many or most of the
elements will have the value zero (or
possibly the same value)
• examples: high order polynomials, bit
mapped graphics, linear algebra
( diagional matricies(identity matrix,
tridiagonal, banded), triangular matrices, )
Polynomial representation
one dimensional array where:
// an array of struct
index represents the exponent
#define MAXTERMS 10
and the stored value is the
typedef struct term {
corresponding coefficient
real coeff;
OR
int expnt;}
2x8 + 4x2 + 1 term poly1[MAXSIZE];
term poly2[MAXSIZE];
0 1 2 3 4 5 6 7 8 9
term poly3[MAXSIZE];
1 4 2
Identity Matrix
• Only has values on the major diagonal
0 1 2

v 0 0 AMF[m][n]
0
if (m == n)
1 0 v 0 return v
else return 0
2 0 0 v

map it on top of a one dimensional array of three elements

v v v AMF[m][n]
if ( m == n ) return A[m] else return 0

You might also like