UBC104 Embedded Systems Variables, Structures & Pointers.
-
date post
20-Dec-2015 -
Category
Documents
-
view
250 -
download
0
Transcript of UBC104 Embedded Systems Variables, Structures & Pointers.
UBC104Embedded Systems
Variables, Structures & Pointers
UBC104 Embedded Systems 2
Memory Task for Today:
Understanding that memory is accessed by addresses
mov 0x1, 0x0001323
set contents that is specified by address to 0x1
0x0000000
0x0001000
0x0002000
0x0003000
0x0003FFF
0x00
0xFF
3
UBC104 Embedded Systems 3
Variables Name for a memory address
int a= 3;
a= 4;
mov 4, 0x000053A4
a 33
0x00000000
0x00001000
0x00002000
0x00003000
0x00004000
0x00005000
0x00006000
0x00007000
(0x000053A4)
UBC104 Embedded Systems 4
C into Assembler
<main+0>: push %ebp<main+1>: mov %esp,%ebp<main+3>: sub $0x8,%esp<main+6>: and $0xfffffff0,%esp<main+9>: mov $0x0,%eax<main+14>: sub %eax,%esp
<main+16>: movl $0x4,0x80493cc
<main+26>: leave<main+27>: ret
1 int a= 3;23 int main(int argc, char** argv) {
45 a= 4;67 return 0;89 }
UBC104 Embedded Systems 5
Basic Types
char 8 bits [-128;127] short 16 bits [-32768;32767] long 32 bits [-231;231-1] int 32 (or 16) bits [-231;231-1] float 32 bits approx. 10-38;1038
double 64 bits ditto
UBC104 Embedded Systems 6
Variable Declarations
unsigned int Number;
char c;
double pi = 3.14159;
float this_is_a_very_long_name;
int n1, n2, n3;
Some invalid declarations:
int 7Sons;
float wrong-identifier;
short #name;
double int;
UBC104 Embedded Systems 7
Strings
Strings are a set of characters terminated by a “0” character
H0x00001000
e0x00001001
l0x00001002
l0x00001003
o0x00001004
\00x00001005
char *str = “Hello”;
UBC104 Embedded Systems 8
Printing strings
#include <stdio.h>
void main(){ int number;
char *format = ”The result is %d.\n“;
number = 33*77;
printf(format, number);}
UBC104 Embedded Systems 9
Hups, we’ve used pointers
H0x00001000
e0x00001001
l0x00001002
l0x00001003
o0x00001004
\00x00001005
char *str = “Hello”;
0x00000A00 0x00001000str
UBC104 Embedded Systems 10
Pointing to an integer
#include <stdio.h>
void main(){ int number; int *nptr;
number = 10*15; nptr= &number; }
0x00000A00 0x0
0x00000A04 0x0
0x00000A00 150
0x00000A04 0x00000A00
UBC104 Embedded Systems 11
Printing the pointer
#include <stdio.h>
void main(){ int number; int *nptr;
number = 10*15; nptr= &number;
printf(“Number: %d\n”, nptr); }
what would this print?
UBC104 Embedded Systems 12
Printing the contents
#include <stdio.h>
void main(){ int number; int *nptr;
number = 10*15; nptr= &number;
printf(“Number: %d\n”, *nptr); }
Dereference!!!
UBC104 Embedded Systems 13
Pointer Arithmetic#include <stdio.h>
void main(){ int number; int *nptr;
number = 10*15; nptr= &number; nptr++; nptr++; nptr++;
printf(“Number: %d\n”, *nptr); }
UBC104 Embedded Systems 14
Summary for Pointers
Declaration:type *variablename; e.g.: int *nptr;
Assignment of address:pointer= &othertype; e.g.: nptr= &number;
Dereference:othertype= *pointer; e.g.: number= *nptr;
UBC104 Embedded Systems 15
Overview
Structures Types Type casting Memory allocation Linked list
UBC104 Embedded Systems 16
Structures
Structures are a collection of variables
struct point {int x;int y;
};100x00001000
150x00001004
…0x00001008
…0x0000100C
…0x00001010
…0x00001014
struct point p= {10, 15};
UBC104 Embedded Systems 17
Structures (cont.)
struct <struct-name> {
<type-name_1> <variable-name_1>;
…
<type-name_n> <variable-name_n>;
} <variable_name>
= {value_1, …, value_n};
UBC104 Embedded Systems 18
Types
Defines a new type (added to native types) typedef <type-name> <type>; Example:
typedef struct point point;point p;
UBC104 Embedded Systems 19
Access to elements
Two ways to access elements: <variable-name>.<element-name> <pointer-name>-><element-name>
Examples:p.x= 10;struct point *ptr;ptr= &p;printf(“x-coordinate: %d\n”,ptr->x);
UBC104 Embedded Systems 20
Type casting void* is a general pointer;
void* p= 0xABCD1234;int x= ((struct point *) p)->x;
(<type>) <variable_name>
Example:printf (“point(%d, %d)”,
((struct point *) p)->x,((struct point *) p)->y);
UBC104 Embedded Systems 21
Memory allocation malloc reserves given number of bytes
Defined as: char *malloc(int size);
Example:#include <malloc.h>
struct point *ptr;ptr= (struct point *) malloc(sizeof(struct point));if (ptr==NULL) {exit(-1);}p->x= 1;p->y= 1;
UBC104 Embedded Systems 22
De-allocation free releases memory associated with a
pointer
Defined as: char *free(void *);
Example:#include <malloc.h>
struct point *ptr;ptr= (struct point *) malloc(sizeof(struct point));if (ptr==NULL) {exit(-1);}free(ptr);
UBC104 Embedded Systems 23
Summary &var returns the address of a variable “var” *ptr returns the contents of the memory location “ptr” points to
Variables are used as memory references Pointers provide another form of references
Space for variables is handled automatically Space for pointers has to be allocated & freed manually
Structures are additional information for the compiler to arrange memory accesses