Thursday, January 24, 2008

CSE 2031 Lecture 6

Arrays and pointers

  • An array name by itself is
    • An address
    • A pointer value
  • A pointer is
    • A variable taking addresses as values
  • An array name is
    • A particular fixed address
    • Like a constant pointer
  • When an array is declared, the compiler allocates
    • A base address
      • The address of element 0
  • And sufficient memory allocation for the rest of the elements

Array indexing and pointer arithmetic

#define MAXSIZE 1024

int A[MAXSIZE], *p;

/*

*space allocated for A

* but p – has not been given a value , even if it did here wouldn't necessarily be any space allocated *where it pointed

*/

Equivalent Statements

  • P = A;
  • P = A+I;
  • P = &A[0];
  • P=&A[i];
  • P[0]
    • is the same as saying *p
  • p[3]
    • is the same as saying *(p+3)

Summing the Array

  • 4 forms
  • 1

    sum =0;

    int i;

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

    sum += A[i];


     

  • 2

sum = 0;

for(p=A; p<&A[MAXSIZE];p++)

sum+= *p;

  • 3

sum = 0;

For(i=0;i<MAXSIZE;i++

sum += *(A+i);

  • 4

Sum = 0;

P=A;

For(i=0 i<MAXSIZE;i++)

Sum += p[i]; /*no dereferencing*/

  • Practice adding pointers and casting them
  • When you minus the two you get how many blocks of allocated memory exist between them
  • If you cast them and minus them you get the mem address casted to ints then you get the difference between that

Arrays as function parameters

  • In a function header
    • An array isn't declared w/ a fixed size in a function header
    • Int A[] is equivalent to int *A
      • This ONLY applies to a function parameter
    • Otherwise Int A[] is is NOT equivalent to int *A
      • Int *A; creates a pointer variable
      • Int A[] creates a constant pointer and no storage

Pointers and strings

  • Strings are arrays of char and are ended with \0
    • \0 is a null character
  • "ABC"
    • Type is char*
    • Value is address of the 'A'
  • Char word[] = "xyz"
    • Is the same as char word[] = {'x','y','z','\0'};
  • Char *p = "xyz";
    • Word is an array of length 4.
    • P is a pointer and points (for now) to an array with 4 elements
    • P can change reference
    • *p doesn't allocate new space

Dynamic Memory Allocation

  • Memory allocated while the program is running
  • Stack
    • When there is a function call there are parameters and local variables
    • Space set aside to hold the values for returns when functions are called
      • Stacks grow during recursion
      • Goes from the top down
      • LIFO
    • A stack grows and shrinks automatically
  • Heap
    • Declares space and the space remains allocated

Malloc

  • Void *malloc ( size_T n) /* memory allocation*/
    • Returns pointer to n bytes
    • Parameter is an integer type
      • Signed or unsigned
    • Sets aside an amount of space
    • Set aside contiguously , like an array
    • What is returned is a pointer to the space allocated
    • If space cannot be allocated a null pointer is returned
    • The storage is uninitialized
      • Not necessarily emptied space
    • Returns NULL if it can't be done
  • Void *calloc (size_t n, size_t element_size)
    • Allocate enough space for this many elements that take 'so much space'

No comments: