programming fundamentals using c arrays and pointers

Post on 12-Jan-2016

80 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

Programming Fundamentals using C Arrays and Pointers. By TungTT2. Training objectives. Arrays Array Definition & Declaration Array Representation Multi-dimensional Arrays Passing arrays to function. Training objectives. Pointers Pointer Arithmetic Swap Example w/Pointers Null Pointers - PowerPoint PPT Presentation

TRANSCRIPT

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Programming Fundamentals using C

Arrays and Pointers

By TungTT2

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Training objectives

Arrays Array Definition & Declaration Array Representation Multi-dimensional Arrays Passing arrays to function

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Training objectives

Pointers Pointer Arithmetic Swap Example w/Pointers Null Pointers

Arrays and Pointers Pointers to Pointers Function Pointers Dynamic Memory Allocation

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays

A data structure is a collection of data types designed to store information in some optimal way. 

Data structures improve readability and simplify coding considerably. 

The simplest example of a data structure is an array. 

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

The solution without array syntax (1)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

The solution without array syntax (2)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

The solution without array syntax (3)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

What are the disadvantages of this solution?

The solution without array syntax (4)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays in C

No bounds checking!Allowed – usually causes no errorarray[10] may overwrite b

Unlike Java, array size in declaration

int array[10];

int b;

array[0] = 3;

array[9] = 4;

array[10] = 5;

array[-1] = 6;

Compare: C: int array[10];

Java: int[] array = new int[10];

All elements of same type – homogenous

First element (index 0)Last element (index size - 1)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Array Representation

Homogeneous Each element same size – s bytes An array of m data values is a sequence of ms bytes Indexing: 0th value at byte s0, 1st value at byte s1, …

m and s are not part of representation Unlike in some other languages s known by compiler – usually irrelevant to programmer m often known by compiler – if not, must be saved by programmer

a[0]

a[1]

a[2]

0x1000

0x1004

0x1008

int a[3];

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Array Representation

char c1;int a[3];char c2;int i;

c1

a[0]

a[1]

a[2]

i

0x1000

0x1004

0x1008

0x100C

0x1014

c20x1010

Could be optimized by making these

adjacent, and reducing padding (by default, not)

Array aligned bysize of elements

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Array Sizes

What is

sizeof(array[3])?

sizeof(array)?

int array[10];

4

40

returns the size of an object in bytes

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Multi-Dimensional Arrays

Multi-dimensional arrays declared with multiple indexesarr[i][j] /* RIGHT*/arr] /* WRONG! */

Array elements are stored by rows The rightmost subscript varies the fastest Array name “points” to 1st element

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Multi-Dimensional Arrays

int matrix[2][3];

matrix[1][0] = 17;

matrix[0][0]

matrix[0][1]

matrix[0][2]

0x1000

0x1004

0x1008

matrix[1][0]

matrix[1][1]

matrix[1][2]

0x100C

0x1010

0x1014

Recall: no bounds checking

What happens when you write:

matrix[0][3] = 42;

“Row Major”Organization

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Passing Arrays To Functions (1)

Arguments The syntax of a function call that passes

an array is functionIdentifier (arrayIdentifier, ... )

Where functionIdentifier is the name of the function and arrayIdentifier is the array name without brackets. 

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Passing Arrays To Functions (2)

Parameters The syntax of a function header that receives an array

address is dataType functionIdentifier ( dataType arrayIdentifier [ ], ... )

The brackets following arrayIdentifier inform the compiler that the parameter holds the address of a one-dimensional array

Multi-dimensional arrays passed to a function must declare the number of elements for every subscript except the first

dataType functionIdentifier ( dataType arrayIdentifier [ ][13], ... )

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointers

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointers

A pointer is a variable that contains an address. With pointers

functions can indirectly access variables. functions can modify the arguments passed by the

caller function. sophisticated data structures can grow and shrink at

run-time. Arrays and pointers are closely related.

Array pointers enable us to conveniently process groups of data such as vectors, lists, and strings.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointers

Special case of bounded-size natural numbersMaximum memory limited by processor word-size232 bytes = 4GB, 264 bytes = 16 exabytes

A pointer is just another kind of valueA basic type in C

int *ptr;

The variable “ptr” is a pointer to an “int”.

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointer Operations in C

Creation& variable Returns variable’s memory address

Dereference* pointer Returns contents stored at

address Indirect assignment

* pointer = val Stores value at address Assignment

pointer = ptr Stores pointer in another variable

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Using Pointers

int i1;

int i2;

int *ptr1;

int *ptr2;

i1 = 1;

i2 = 2;

ptr1 = &i1;

ptr2 = ptr1;

*ptr1 = 3;

i2 = *ptr2;

i1:

i2:

ptr1:

0x1000

0x1004

0x1008

ptr2:

0x100C

0x1010

0x1014

1

2

0x1000

0x1000

3

3

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Using Pointers (cont.)

Type check warning: int_ptr2 is not an int

int1 becomes 8

int int1 = 1036; /* some data to point to */int int2 = 8;

int *int_ptr1 = &int1; /* get addresses of data */int *int_ptr2 = &int2;

*int_ptr1 = int_ptr2;

*int_ptr1 = int2;

What happens?

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Using Pointers (cont.)

Type check warning: *int_ptr2 is not an int *

Changes int_ptr1 – doesn’t change int1

int int1 = 1036; /* some data to point to */int int2 = 8;

int *int_ptr1 = &int1; /* get addresses of data */int *int_ptr2 = &int2;

int_ptr1 = *int_ptr2;

int_ptr1 = int_ptr2;

What happens?

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointer Arithmetic

pointer + number pointer – numberE.g., pointer + 1 adds 1 something to a pointer

char *p;char a;char b;

p = &a;p += 1;

int *p;int a;int b;

p = &a;p += 1;In each, p now points to b

(Assuming compiler doesn’t reorder variables in memory)

Adds 1*sizeof(char) to the memory address

Adds 1*sizeof(int) to the memory address

Pointer arithmetic should be used cautiously

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

The Simplest Pointer in C

Special constant pointer NULLPoints to no dataDereferencing illegal – causes segmentation fault

To define, include <stdlib.h> or <stdio.h>

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Generic Pointers

void *: a “pointer to anything”

Lose all information about what type of thing is pointed to Reduces effectiveness of compiler’s type-checking Can’t use pointer arithmetic

void *p;int i;char c;p = &i;p = &c;putchar(*(char *)p);

type cast: tells the compiler to “change” an object’s type (for type checking purposes – does not modify the object in any way)

Dangerous! Sometimes necessary…

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pass-by-Reference

voidset_x_and_y(int *x, int *y){ *x = 1001; *y = 1002;}

voidf(void){ int a = 1; int b = 2; set_x_and_y(&a,&b);}

1

2

a

b

x

y

1001

1002

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays and Pointers

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays and Pointers

Dirty “secret”:Array pointer to the initial (0th) array element

a[i] *(a+i)

An array is passed to a function as a pointer The array size is lost!

Usually bad style to interchange arrays and pointers

Avoid pointer arithmetic!

Really int *array

int

foo(int array[],

unsigned int size)

{

… array[size - 1] …

}

int

main(void)

{

int a[10], b[5];

… foo(a, 10)… foo(b, 5) …

}

Must explicitlypass the size

Passing arrays:

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays and Pointers

int

foo(int array[],

unsigned int size)

{

printf(“%d\n”, sizeof(array));

}

int

main(void)

{

int a[10], b[5];

… foo(a, 10)… foo(b, 5) …

printf(“%d\n”, sizeof(a));

}

What does this print?

What does this print?

8

40

... because array is reallya pointer

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays and Pointers

Given the previous declarations, each of the following lines are equal.

cptr word &word[0] address of word[0]

(cptr + n) word + n &word[n] address of word[n]

*cptr *word word[0] value of word[0]

*(cptr + n) *(word + n) word[n] value of word[n]

char word[10];char *cptr;cptr = word; // points to word[0]

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Arrays and Pointers

int i;

int array[10];

for (i = 0; i < 10; i++)

{

array[i] = …;

}

int *p;

int array[10];

for (p = array; p < &array[10]; p++)

{

*p = …;

}

These two blocks of code are functionally equivalent

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Strings

In C, strings are just an array of charactersTerminated with ‘\0’ characterArrays for bounded-length stringsPointer for constant strings (or unknown length)

char str1[15] = “Hello, world!\n”;char *str2 = “Hello, world!\n”;

H e l l o , w lo r d !\nlength

H e l l o , w lo r d !\nterminator

Pascal, Java, …

C, …

C terminator: ’\0’

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

String length

Must calculate length:

Provided by standard C library: #include <string.h>

int

strlen(char str[])

{

int len = 0;

while (str[len] != ‘\0’)

len++;

return (len);

}

can pass anarray or pointer

Check for terminator

array access to pointer!

What is the size of the array???

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointers and more

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointers to Pointers

Since pointers are variables themselves, they can be stored in arrays just as other variables can.Example:

char* ptrArr[8];

ptrArr[0]

ptrArr[1]

ptrArr[2]

FSO

abc

123

Pointers to Pointers

ptrArr[0]

ptrArr[1]

ptrArr[2]

FSO

abc

123

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointers of Pointers

int var = 5;

int* ptr;int** pptr;int*** ppptr;int**** pppptr;

ptr = &var; pptr = &ptr;ppptr = &pptr;pppptr = &ppptr;

var, *ptr, **pptr, ***ppptr, ****pppptr all = 5&var=?&ptr = ? *ptr = ?&pptr = ? *pptr = ?&ppptr = ? *ppptr = ?&pppptr = ? *pppptr = ?

0x06000x05fe Return Addr

pppptr 0x05fc 0x05fappptr 0x05fa 0x05f8pptr 0x05f8 0x05f6ptr 0x05f6 0x05f4var 0x05f4 5

0x05f20x05f00x05ee0x05ec0x05ea0x05e8

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Pointer to Pointer (char **argv)

Passing arguments to main:

int

main(int argc, char **argv)

{

...

}

an array/vector of char *

Recall when passing an array, a pointer to the first element is passed

size of the argv array/vector

Suppose you run the program this way

UNIX% ./program hello 1 2 3

argc == 5 (five strings on the command line)

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

char **argv

argv[0]

argv[1]

argv[2]

0x1000

0x1008

0x1010

argv[3]

argv[4]

0x1018

0x1020

“./program”

“hello”

“1”

“2”

“3”

These are strings!!Not integers!

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Function Pointers

In C, a function is not a variable, but it is possible to define pointers to functions which can be: assigned, placed in arrays, passed to functions, returned by functions, and so on.

int function1(int a, char* s) { … }int function2(int a, char* s) { … }int (*f[2])(int, char*);f[0] = function1;f[1] = function2;(*f[n])(10, "hello");

Function Pointers

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Complicated Declarations

C is sometimes castigated for the syntax of declarations, particularly ones that involve pointers to functions:

int *f(); // f: function returning pointer to intint (*pf)(); // pf: pointer to function returning int

What do these do?char **argv argv: pointer to pointer to charint (*daytab)[13] daytab: pointer to array[13] of intint *daytab[13] daytab: array[13] of pointer to intchar (*(*x())[])() x: function returning pointer to array[ ] of

pointers to function returning charchar (*(*x[3])())[5] x: array[3] of pointer to function returning

pointer to array[5] of char

Function Pointers

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Dynamic memory allocation

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Definition — The Heap

A region of memory provided by most operating systems for allocating storage not in Last in, First out discipline

I.e., not a stack

Must be explicitly allocated and released May be accessed only with pointers

Remember, an array is equivalent to a pointer

Many hazards to the C programmer

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Static Data Allocation

0x00000000

0xFFFFFFFF

address space

program code(text)

static data

heap(dynamically allocated)

stack(dynamically allocated)

PC

SP

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Allocating Memory in The Heap

See <stdlib.h>void *malloc(size_t size);void free(void *ptr);void *calloc(size_t nmemb, size_t size);void *realloc(void *ptr, size_t size);

malloc() — allocates size bytes of memory from the heap and returns a pointer to it.

NULL pointer if allocation fails for any reason free() — returns the chunk of memory pointed to

by ptr Must have been allocated by malloc or calloc

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Notes

calloc() is just a variant of malloc() malloc() is analogous to new in C++ and

Java new in C++ actually calls malloc()

free() is analogous to delete in C++ delete in C++ actually calls free() Java does not have delete — uses garbage collection

to recover memory no longer in use

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Typical usage of malloc() and free()

char *getTextFromSomewhere(…);

int main(){char * txt;…;txt = getTextFromSomewhere(…);…;printf("The text returned is %s.", txt);free(txt);

}

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Typical usage of malloc() and free()

char * getTextFromSomewhere(…){char *t;...t = malloc(stringLength);...return t;

}

int main(){char * txt;…;txt = getTextFromSomewhere(…);…;printf("The text returned is %s.", txt);free(txt);

}

© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/4

Definition – Memory Leak

The steady loss of available memory due to forgetting to free() everything that was malloc’ed.

Bug-a-boo of most large C and C++ programs If you “forget” the value of a pointer to a

piece of malloc’ed memory, there is no way to find it again!

Killing the program frees all memory!

top related