Top Banner
UBC104 Embedded Systems Variables, Structures & Pointers
23

UBC104 Embedded Systems Variables, Structures & Pointers.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: UBC104 Embedded Systems Variables, Structures & Pointers.

UBC104Embedded Systems

Variables, Structures & Pointers

Page 2: UBC104 Embedded 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

Page 3: UBC104 Embedded Systems Variables, Structures & Pointers.

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)

Page 4: UBC104 Embedded Systems Variables, Structures & Pointers.

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 }

Page 5: UBC104 Embedded Systems Variables, Structures & Pointers.

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

Page 6: UBC104 Embedded Systems Variables, Structures & Pointers.

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;

Page 7: UBC104 Embedded Systems Variables, Structures & Pointers.

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”;

Page 8: UBC104 Embedded Systems Variables, Structures & Pointers.

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);}

Page 9: UBC104 Embedded Systems Variables, Structures & Pointers.

UBC104 Embedded Systems 9

Hups, we’ve used pointers

H0x00001000

e0x00001001

l0x00001002

l0x00001003

o0x00001004

\00x00001005

char *str = “Hello”;

0x00000A00 0x00001000str

Page 10: UBC104 Embedded Systems Variables, Structures & Pointers.

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

Page 11: UBC104 Embedded Systems Variables, Structures & Pointers.

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?

Page 12: UBC104 Embedded Systems Variables, Structures & Pointers.

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!!!

Page 13: UBC104 Embedded Systems Variables, Structures & Pointers.

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); }

Page 14: UBC104 Embedded Systems Variables, Structures & Pointers.

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;

Page 15: UBC104 Embedded Systems Variables, Structures & Pointers.

UBC104 Embedded Systems 15

Overview

Structures Types Type casting Memory allocation Linked list

Page 16: UBC104 Embedded Systems Variables, Structures & Pointers.

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};

Page 17: UBC104 Embedded Systems Variables, Structures & Pointers.

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};

Page 18: UBC104 Embedded Systems Variables, Structures & Pointers.

UBC104 Embedded Systems 18

Types

Defines a new type (added to native types) typedef <type-name> <type>; Example:

typedef struct point point;point p;

Page 19: UBC104 Embedded Systems Variables, Structures & Pointers.

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);

Page 20: UBC104 Embedded Systems Variables, Structures & Pointers.

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);

Page 21: UBC104 Embedded Systems Variables, Structures & Pointers.

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;

Page 22: UBC104 Embedded Systems Variables, Structures & Pointers.

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);

Page 23: UBC104 Embedded Systems Variables, Structures & Pointers.

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