Download as pdf or txt
Download as pdf or txt
You are on page 1of 45

Programming

in Artificial Intelligence
Ansgar Fehnker
Primitive Type vs Data Structures in
Processing

Primitive Types Data Structures


Primitive Type vs Data Structures in
Processing

Primitive Types Data Structure


§ This are types such as int, § Classes,
but also arrays, lists, etc
float, char, bool, color. define data structures.
§ A variable of those types contain §A variable of those type contains
a value of that type. A number, a a reference to a data structure,
character, a boolean. not the data itself.
§ They behave mostly like § Beforeyou can use them you
variables as you know them from have to create a data structure
mathematics. (new) and assign it to them.
class Account{
float balance=0;

Primitive Type vs Data Structures in


Account(float deposit){
balance= deposit;
}
Processing void add(float deposit){
balance += deposit;
}
}
Primitive Types Data Structure
float paulsMoney = 100;
Account pauls = new Account(100);
float marysMoney = paulsMoney;
Account marys = pauls;
paulsMoney+= 100;
pauls.add(100);
println("Paul's
money:",paulsMoney); println("Paul's balance:",pauls.balance);
println("Mary's println("Mary's balance:",marys.balance);
money:",marysMoney);
Primitive Type vs Data Structures in
Processing

Primitive Types Data Structure


float paulsMoney = 100;
Account pauls = new Account(100);
float marysMoney = paulsMoney;
Account marys = pauls;
paulsMoney+= 100;
pauls.add(100);
println("Paul's
money:",paulsMoney); println("Paul's balance:",pauls.balance);
println("Mary's println("Mary's balance:",marys.balance);
money:",marysMoney);

Output Output
Paul's money: 200.0 Paul's balance: 200.0
Mary's money: 100.0 Mary's balance: 200.0
Pointers

§ C++ provides pointer types to allow programmers to


manipulate memory space.
§ Pointers are variables that hold addresses in C and C++.
§ Provide much power and utility for the programmer to
access and manipulate data.
§ Useful for passing parameters into functions,
allowing a function to modify and return values
to a calling routine.
Pointers

Careful:
§ Writing in C or C++ is like running a chain saw with all the
safety guards removed. Bob Gray
§ In C++ it's harder to shoot yourself in the foot, but when
you do, you blow off your whole leg. Bjarne Stroustrup.
Pointers and Memory

How values for variables are stored in computer


memory?
§ Whenever a variable is declared, a memory location is
associated with the variable.
§ Whenever the variable is accessed the data value is taken from
that particular memory location.
§ A variable contains a value, but a pointer specifies
where a value is located.
§ A pointer denotes the memory
location of a variable.
Pointer and Memory

§ A pointer is a variable that contains an address. The


analogy we use here is the number of a coat as you get it,
e.g., from the checkroom at the theater.
§ The checkroom number is an address.
§ It points at your jacket at the checkroom.
§ In our analogy the checkroom represents the memory.
Memory and Addresses
Every time the
program is executed
the memory location
may change.
Here’s a picture of RAM.
§ All variables are stored in memory, each at its own unique
address or location.
§ For example: int count = 5;
§ The variable count is associated with
memory address 0x241FF54
§ The value “5” is stored in memory.
§ It can be accessed by using the
variable name “count”.
§ Or it be accessed by reference to its
address.
Addresses and References

Accessing memory addresses


§ To access the memory address of a variable, we use the
unary operator &
§ & is called an address operator (or address-of) operator.

Example
std::cout<< &count << std::endl;

This prints address: 0x241FF54


Addresses and Pointers

A pointer is a special type of variable


§ Normal variables contain a data value.
§ Pointer variables contains a memory address as its
value.
§ Data is modified when a normal
variable is used.
§ The value of the address stored in a
pointer is modified when a pointer is
used.
Addresses and Pointers

How to define a pointer


§ datatype* pointername;
§ Example: int* ptr;
§ This declares a pointer variable named ptr of type int*

§ Note: Some people prefer to write


int *ptr
§ It’s the same. Use one, consistently.
Addresses and Pointers

Usually, the address stored in the pointer is the address of


some other variable.
§ Example
int count = 5;
int* ptr = &count;

§ This means:
§ count is a variable with value 5
§ ptr is a pointer with value
0x241FF54
Dereferencing Pointers

§ To get the value that is stored at the memory location


pointed by the pointer, one would need to dereference
the pointer.
§ Dereferencing means to read the data value at the
address the pointer points to.
§ Dereferencing allows
manipulation of the data
contained at the memory
address stored in the pointer.
Dereferencing Pointers

§ Dereferencing is done with the unary operator * called


a dereference operator.
§ Syntax
*variablename
§ Example
§ int count = 5;
§ int *ptr = &count;
§ cout << *prt <<endl;
§ This prints the value at address
0x241FF54. Which is 5.
Assigning to Pointers

§ Assign an address to a pointer


int count = 5;
int *ptr1 = &count;
Int *ptr2 = ptr1;
§ Both pointers, ptr1 and ptr2, point to the same address.
§ Set value pointed to directly
*ptr1 = 6;
cout << *ptr1; //prints 6
cout << *ptr2; //prints 6
§ Because both pointers, ptr1 and
ptr2, point to the same
address.
Assigning to Pointers

§ Normal variable
§ Contains a data value
int count = 5;
§ Use & to get its address:
cout << count; //prints 5
cout << &count;//prints 0x241FF54

§ Pointers:
§ Contain an address
int *ptr = &count;
§ Use * to get the value at that address
cout << ptr; //prints 0x241FF54
cout << *ptr;// prints 5
Exercise

§ What are the values of the variables firstvalue and


secondvalue are in the end.
int main (){
int firstvalue = 5, secondvalue = 15;
int *p1, *p2;
p1 = &firstvalue;
p2 = &secondvalue;
*p1 = 10;
*p2 = *p1;
p1 = p2;
*p1 = 20;
return 0;
}
The NULL pointer

§ If you don’t initialize a pointer it will contain some random value.


§ Dereferencing a random pointer can cause an arbitrary mistake.
§ The NULL value can be used to initialize a pointer
§ int *p = NULL; // NULL is the null pointer
§ Modern C++ int *p = nullptr; // nullptr is the null pointer

§ Dereferencing a NULL pointer can cause an arbitrary mistake.


§ However, you can now check whether it is not NULL
if(p!=nullptr){ ...//do something with p
§ It is a good habit to initialize pointers. At least to NULL.
The NULL pointer
Common Error: Confusing Data And
Pointers
Common Error: Confusing Data And
Pointers

double* account_pointer = &joint_account;


account_pointer = 1000;

§ The assignment statement does not set the joint


account balance to 1000.
§ It sets the pointer variable, account_pointer, to
point to memory address 1000.
Remember this?

Primitive Types Data Structures


Call by value

§ Call by value refers to the int maximum(int x, int y){


“classical” way to do function if (x>y){
calls.
return x;
§ When a function is called, the }
value of all parameters is
copied and assigned to the return y;
parameters in the parameter }
list of the function.
...
c = maximum(a,b);//call
...
Call by reference

§ When a function is called by void increment(int *x){


reference, the parameters are *x = *x + 1;
not copied, but a reference This type of
(pointer) to the calling } functions/methods are
variables is passed. poor practice, since
... they rely needlessly on
§ The difference to “call by int main() { side effects
value” is, that the calling
parameter can be changed by int a=3;
the called function (side increment(&a);
effect). For simple returning a
} single value use the
return type.
Summary

§ Pointer are variables that refer to addresses in memory


§ Use datatype* to define pointers.
§ You obtain the value stored at the location a pointer points to by
dereferencing it.
§ The dereferencing is done using the * operator.
§ Careful about NULL and undefined pointers. Very careful.
§ You can get the address of a normal variable by using the address-
of operator &
§ You can assign addresses to pointers. With appropriate type.
Side effects

§ A function is without side effect § A function is with side effect, if


if all that matters is what going variables/objects that are not
in and what is coming out mentioned as parameters may
still be changed.
§ The rest of the world, i.e.
other variables, are not § It is necessary to use side
changed. effects very carefully, because
we can easily loose overview
§ This supports a clear
about what a function is doing.
programming style and helps to
write correct programs.
§ When
Arrays and Pointers
Arrays and Pointers

This means: an array


variable is actually a
pointer.
Arrays and Pointers
Arrays and Pointers
Arrays and Pointers
Pointer Arithmetic

§ A pointer contains an address. § It points to the next address in


memory.
§ An address is a number.
§ Where that is depends on the
§ We can do some arithmetic
type.
with pointers, but we have to
be careful. § That is why pointers belong to
a certain type.
§ Assume we have a pointer p.
§ What does p++ do?
Pointer Arithmetic

If we have
char *mychar; // char needs one byte
short *myshort; // short takes 2 bytes
long *mylong; // long takes 4 bytes

The following increments have the effect


illustrated in the graphics to the right.
mychar++;
myshort++;
mylong++;
Pointer Arithmetic

§ A pointer contains an address.


§ An address is a number.
§ We can do some arithmetic
with pointers, but we have to
be careful.
§ Assume we have a pointer p.
§ What does p++ do?
Array Pointer Duality
Array Pointer Duality
Array Pointer Duality

• BTW: In C++ you cannot ask an


array what it size is.
• It is just a pointer. It has no
method .size() or field length.
• You have to keep this
information yourself.
Array Pointer Duality

Alternatives to address array fields.


int numbers[5];
int * p = numbers; // pointer p points at the first array element
*p = 1; // is the same as numbers[0]=1;
p++; *p = 2 // has the same effect as numbers[1]=2;
p = &numbers[2]; // p points now to numbers[2]
*p = 3; // has the same effect as numbers[2]=3
p = numbers + 3; // p points now to numbers[3]
*p = 4;
p = numbers;
*(p+4)= 5; // has the same effect as numbers[4]=5
Program Clearly, Not Cleverly
Common Error: Returning a local pointer
References and Pointers

§ C++ makes a difference between pointers and references.


§ In this lecture we did not highlight or address the
difference, but it is explained in the reader.
Summary

§ In C++ The name of an array is a pointer


§ It points to the first element of the array.
§ a[n] is identical to *(a + n), where a is a pointer into an
array and n is an integer offset.
§ Don’t try to be too clever.
Quiz

§ http://tinyurl.com/createpointers

You might also like