Algorithmics and C programming Language - Nicolas Gaud

Transcription

Algorithmics and C programming Language - Nicolas Gaud
Algorithmics and C programming Language
LO27 –Fall 2016
https://moodle.utbm.fr
http://nico.gaud.free.fr
Nicolas Gaud
Université de Technologie de Belfort-Montbéliard
F-90 000 Belfort, France – [email protected]
N. Gaud
LO27 – F2016 – UTBM
Pointer Type and dynamic variables
Université de Technologie de Belfort-Montbéliard
F-90 000 Belfort, France – [email protected]
Pointer – Definition
Definition
The value of a pointer is the address in memory of the variable it
represents.
Example of a pointer P pointing to a variable X:
N. Gaud
LO27 – F2016 – UTBM
2
Pointer – How to declare a pointer ?
3
Declaration
A pointer is declared by writing the type of the variables that can be
referred and its name.
Example 1: Declaration of a pointer p referring an integer variable:
1
i n t ∗p ;
Example 2: Declaration of a pointer referring a product variable (record):
1
N. Gaud
LO27 – F2016 – UTBM
P r o d u c t ∗p ;
Pointer – Affectation
4
Affectation
Pointers contain addresses
Addresses of variables are obtained with the operator &
Example 1: affectation to the address of a integer.
1
2
3
4
int x ;
int ∗ p ;
p = &x ;
/∗
the
a d d r e s s
of
x
in
memory
is
&x
∗/
Example 2: affectation between pointers
1
2
3
N. Gaud
LO27 – F2016 – UTBM
int ∗ p ;
int ∗ q ;
q = p;
Pointer – Summary
N. Gaud
LO27 – F2016 – UTBM
5
Dynamic variable – Definition
Definition
Pointers allow to manipulate dynamic variables.
Dynamic variables are variables which are created/allocated
during the program execution.
Thus, their name, number and size are unknown during the
coding and the compilation of the program.
N. Gaud
LO27 – F2016 – UTBM
6
Dynamic variable – Allocation
7
Definition
The allocation is performed by calling the malloc function.
(POINTER TYPE) malloc ( SIZE IN MEMORY )
The malloc sub-program creates a dynamic variable that have the
size specified in parameter (using sizeof function).
It returns the pointer (address) of the place allocated in memory, its
type must be specified before malloc keyword.
Example: Allocation of an integer dynamic variable
1
2
i n t ∗p ;
p = ( i n t ∗) malloc ( s i z e o f ( i n t ) ) ;
When malloc is executed an area in memory is allocated in order to
N. Gaud
LO27 – F2016 – UTBM
Dynamic variable – Allocation of a pointer on record
8
1
2
Product ∗ pproduct ;
pproduct = ( Product ∗) malloc ( s i z e o f ( Product ) ) ;
N. Gaud
LO27 – F2016 – UTBM
Dynamic variable – Access
9
Access
The (dynamic) variable referred by the pointer p is accessible by
writing *p.
Example 1: access to a dynamic int variable.
int ∗ p ;
p=( i n t ∗ ) m a l l o c ( s i z e o f ( i n t ) ) ;
∗p = 4 ;
1
2
3
1
2
Example 2: access to a dynamic product record.
Product ∗ pproduct ;
p p r o d u c t =( P r o d u c t ∗ ) m a l l o c ( s i z e o f ( P r o d u c t ) ) ;
3
4
5
( ∗ p p r o d u c t ) . c o d e =3;
( ∗ p p r o d u c t ) . p r i c e =10;
N. Gaud
LO27 – F2016 – UTBM
⇔
⇔
p p r o d u c t −>c o d e =3;
p p r o d u c t −>p r i c e =10;
Pointer – Equality
Equality
Two pointers are equal if they store the same address: the two
pointers refer to the same dynamic variable.
Example
p1 = p2
∗p1 = ∗p2 = ∗p3
but
p2 6= p3 and p1 6= p3
N. Gaud
LO27 – F2016 – UTBM
10
Pointer – Free memory
11
Free the memory
The sub-program free allows the memory used by a dynamical
variable to be available again.
There exists a value allowing the initialization of a pointer
with an undefined address.
⇒ the pointer do not refer to a location in memory : NULL
Example
1
2
3
4
5
6
i n t ∗ p2 ;
p2 = ( i n t ∗ ) m a l l o c ( s i z e o f ( i n t ) ) ;
∗ p2 = 4 ;
...
f r e e ( p2 ) ;
p2 = NULL ;
N. Gaud
LO27 – F2016 – UTBM
Pointer – Free memory
12
Another Example
1
2
3
4
5
6
7
8
Product ∗ p ;
...
p = ( Product ∗) malloc ( s i z e o f ( Product ) ) ;
p−>p r i c e = 4 ;
...
free (p );
p = NULL ;
...
N. Gaud
LO27 – F2016 – UTBM
Thank you for your attention. . .
Université de Technologie de Belfort-Montbéliard
F-90 000 Belfort, France – [email protected]
Appendix
Université de Technologie de Belfort-Montbéliard
F-90 000 Belfort, France – [email protected]
Author: Dr. Nicolas GAUD
Associate Professor
Laboratoire Systèmes and Transport (IRTES-SET)
Institut de Recherche sur les Transports, l’Énergie et la Société
Université de Technologie de Belfort-Montbéliard, France
Topics: Multiagent systems,
Multiagent-based simulation, Agent-Oriented
Software Engineering.
Web page:
Email:
http://multiagent.fr/People:Gaud_nicolas
[email protected]
See also his open-source contributions:
http://www.sarl.io
http://www.janusproject.io
http://www.aspecs.org
http://www.arakhne.org
N. Gaud
LO27 – F2016 – UTBM
i

Documents pareils