06-arrays
06-arrays
progress so far: seven students showed up and their task is to write simple apps
and games for dualPanto in Unity
next step: Unity game jam on dualpanto with Eva Krebs and Corinna Jaschek
tomorrow Friday 1pm (or join at 3pm)
anyone else feels like they need a boost to get into a coding job? join now
it would be sufficient to
#include <stdio.h>
<30sec brains>
}
<30sec brains>
memory
let’s talk about accessing RAM
random-access memory ::
allows data items to be read or written in
almost the same amount of time irrespective
of the physical location of data inside the memory.
00001 inside:
enumerated memory cells
00000
00016
this gives a name to one cell of idea: address n cells with one variable…
memory
let’s use a single variable name, say a,
if we want to store prime plus a length, say a[135],
numbers, we need to access to say that we want to use the name ‘a’
n memory locations… to refer to 135 cells of memory
with a single variable name?
<30sec>
arrays
array (general term) ::
a systematic arrangement of similar objects, usually in rows and columns.
each element will hold an int reserve memory for 8 elements
int a[8]; 7
a[0] = 7;
address the first element write a 7 into it
german: feld
array (computing) ::
a data type that is meant to describe a collection of elements,
each selected by one or more indices.
#include <stdio.h>
int a[8];
a[0] = 7;
int main(void) { quick warm-up exercise
1. define an int array with 10 elements
2. write a 0 into element 0, a 1 into element 1…
3. print the contents of the array
return 0;
}
<3min REPL>
#include <stdio.h>
int main(void) {
start at 0 not ‘<=’ because array started at 0
int a[10];
for (int i = 0; i < 10; i++)
a[i] = i;
for (int i = 0; i < 10; i++)
printf("a[%d] = %d\n", i, a[i]);
return 0;
}
<30sec brainstorming>
666
writing past the end
of the array you mean: you
what will happen? expect C to check
an error message? bounds for you?
int a[100]; nope.
for (int i = 0; i < 666; i++) this is C, after all
a[i] = i;
<30sec brainstorming>
00001
00000
00016
(address of a)
int a[100]
we reserved this…
b[15] = 666;
b[15] = 666; I am wondering what will happen? oh!
for (i = 0; i <= 9; i++)
for printf("a[%d]= %d\n",
(i = 0; i <= 9; i++)i, a[i]);
printf("done\n");
printf("a[%d]= %d\n", i, a[i]);
return 0;
<30sec brains>
}
segmentation fault ::
says that the program has attempted to access a "restricted" area of memory,
which is pretty much any memory not allocated to the program
one common approach to reducing the risk
give it a try, but (if you need it just once) do not be dogmatic about it
initializing
arrays
note the curly brackets
do you like that this is just a warning (in the light of the error and exit lecture?)
my 2ct: C is great, but you know…
what does it do?
<vote>
#include <stdio.h>
int a[5] = {1,2,3,4,5}; // a = [1,2,3,4,5]
int b[] = {1,2,3,4,5};
int c[10] = {1,2,3,4,5};
d[0] = 1
int d[5] = {1,2,3,4,5,6,7,8,9,10};
d[1] = 2
int main(void) {
int i;
d[2] = 3
// Disable stdout buffering d[3] = 4
setvbuf(stdout, NULL, _IONBF, 0); d[4] = 5
for (i = 0; i <= 10; i++) { d[5] = 0
printf("d[%d] = %d\n", i, d[i]); d[6] = 946082336
fflush(stdout);
d[7] = 32687
}
d[8] = 0
printf("Done\n");
return 0;
d[9] = 0
} d[10] = 0
Done
given what you know about C, which one would you guess it is?
can you write a quick program that allows you to test your assumption?
<30sec brainstorming>
go do it
general approach: pass array into function, modify an element, see if modified.
#include <stdio.h>
void modifyFirstElement(int array[3]){
array[0] = 2;
}
int main (void) {
<3min repl>
int b[3] = {1,1,1};
modifyFirstElement(b);
printf("b[0] = %d\n", b[0]);
b[0] has changed, thus…
return 0;
C passes arrays “by reference”
}
(because it is fast)
this is passed by value
http://stackoverflow.com/questions/33622787/are-pointers-considered-a-method-of-calling-by-reference-in-c
In languages that support true "call by reference", such as C++, we can write
swap(x,y); // does not work in C
http://stackoverflow.com/questions/17423218/diff-between-call-by-reference-and-call-by-pointer
!
int sumUpFiveElements(int a[5]) {
return a[0] + a[1] + a[2] + a[3] + a[4];
}
eratosthenes(sieve, SIEVE_SIZE);
return 0;
}
}
#define SIEVE_SIZE 30000 make 300,000
#define FALSE 0 pretty quick!
#define TRUE 1
<30sec brainstorming>
we turned isPrime() into a …
void push(int i) {
int pop(void) {
int main(void) {
push(10); push(20); push(30);
printf("stack holds %d, %d, %d\n", pop(), pop(), pop());
return 0;
}
#include <stdio.h>
int stack[255];
int top = 0;
// ahhh... leaving out all checks and bounds
void push(int i) {
stack[top++] = i;
}
i++ tends to be
int pop(void) { used together with --i
return stack[top--]; (not with i--)
}
int main(void) {
int stack[255]; versions without global variables
int top= 0;
push(stack, &top, 10); pass stack and top as parameters
push(stack, &top, 20); (look at this version when you have
push(stack, &top, 30);
printf("stack holds %d, %d, %d\n", learned about pointers)
pop(stack, &top),
pop(stack, &top),
pop(stack, &top));
return 0;
}
function calls in C (and most other languages are implemented using a stack)
<60sec brainstorming>
so what is a “frame”…?
(stack here shown as growing upwards)
#include <stdio.h>
int main(void) {
int i;
int a[10] = {0};
int b[10] = {0};
b[15] = 666;
we were wondering what would happen
for (i = 0; i <= 9; i++)they are next to each other on the stack
printf("a[%d]= %d\n", i,stack grows downwards, arrays upwards
a[i]);
printf("done\n");
return 0;
}
how to make program
goto anywhere?
<brains>
more geeky details…
so how big is the stack in the repl machines (in kilo bytes)?
<30sec brainstorming>
if you really want to know
#include <sys/resource.h>
#include <stdio.h>
http://stackoverflow.com/questions/53827/checking-available-stack-size-in-c
there are other ways to manage memory
! malloc(), when we talk about pointers
summary
00001
00000
00016
(00068, but no need to know)
int i
we just gave a name to many cells idea: if we want space for n cells…
of memory
let’s use a variable name, say a,
this allows us to compute bigger plus a length, say a[135],
things faster, as we can keep a lot to say that we want to use the name ‘a’
of partial solutions around to refer to 135 cells of memory
! we could handle a lot of memory
memory "! speed are to a with a single variable
certain extent interchangeable
end
go download the slides
and reprise, practice