Sizes and Compatibility
For example:
char c;
char *pc;
int a;
int *pa;
double x ;
double *px;
sizeof(c)= 1 sizeof(pc)= 4 sizeof(*pc)= 1
sizeof(a)= 4 sizeof(pa)= 4 sizeof(*pa)= 4
sizeof(x)= 8 sizeof(px)= 4 sizeof(*px)= 8
What can you learn:
l Generally speaking Pointers are all the same size
l The contents of a pointer know their size & type
Pointer and Array
For example
int a[5];
/*the name of an array is a pointer value*/
int a[5];
printf(“%p %p”, &(a[0]), a);
l The two values printed are identical
Access Array with any pointer
#include <stdio.h>
int main (void)
{
int a[5] = {2, 4, 6, 8, 22};
int *p;
p = &(a[1]); /* p has address of a[1]*/
printf(“%d %d\n”,a[0],*(p-1));
}
Arrays of pointers
A pointer can point to an array.
int int_array[5] = {2, 6, 4, 7, -1};
// int_array is an array of 5 integers
The following declaration declares an array of 10 pointers to int:
int * ptr_array [10];
int i = 10;
ptr_array[0] = &i;
Pointer Arithmetic and Arrays
Consider the following code:
char a[3];
int b[3];
float c[3];
char *pa = a + 1;
int *pb = b + 1;
float *pc = c + 1;
l a[i] is the pointer arithmetic *(a+i)
l When a pointer points to an array member, arithmetic operations can be used to obtain pointers to other array members. For instance,
int x[10]= {1,2,3,4,5,6,7,8,9,10};
int* p = &(x[5]);
p += 3; // x[8],
p -= 7; // x[1].
p++;
p--;
But, since an array name is not a modifiable l-value, x++ and x = p are illegal.
For example:
int x[10];
x++; //illegal
int* p = x;
p++; //legal
§ 12. Passing Parameters
Pass by value
void f(int x)
{
x = x+1;
}
int main (void)
{
int a=3;
f(a);
printf(“%d”,a); /* prints 3 ; pass by value */
return 0;
}
Pass by pointer
void f(int *x) { x = x+1;}
int main (void)
{
int a[]={1,2}, *pa=a;
f(pa);
printf(“%d”,*pa); /* Again prints 1. Value of ptr passed. */
/* The ptr value is increased - by value */
return 0;
}
Memory Allocation
Types of Memory Allocation:
• Static – done at compile time
• Dynamic – get memory during runtime
Stack: Memory Allocated Pop/Push; Local Vars
Heap: Dynamic Allocation of Vars. (malloc)
Stack:
Declare arrays with variables of larger scope
Problem: Only good in the current scope!
float * func(int n)
{
float array[n]; /* good ;scope n */
…
return array; /* severe mistake */
/* Returns ptr to first element; But Stack is now popped !! */
}
malloc
Prototype:
void * malloc (size_t size);
malloc allocates number of bytes specified in size size_t
l Ask for a block of memory
l How big : Of size size
l malloc returns a pointer to that block of memory
l If unsuccessful returns NULL
For example:
float* pFloat = (float *)malloc (sizeof(float));
Release memory: free
Protptype:
void free(void *ptr);
For example:
float * p = (float *)malloc (sizeof(float));
*p = 10.0;
free(p);
New and Delete (used in C++):
For example:
int *p;
p = new int;
delete p;
p = NULL;
delete p;
or, declaration with initialization of the pointer and the pointee:
int * p = new int(5);
is similar to
int* p = new int;
*p = 5;
In this example, p is a pointer to an integer, which is initialized with a value 5.
l Operation new creates an anonymous variable, stored in a region of the memory called the heap, and returns the address of this variable.
l The space has to be freed by a call to the operator delete.
l It is an error if a program deletes a point that does not have memory allocated, or deletes a heap memory twice.
The following example allocates an array of 10 int on the heap and pointer p points to the first member of this array:
int * p = new int[10];
In this case, the size can be a variable determined at run time. This kind of arrays is called dynamic arrays.
To free the memory allocated for a dynamic array, use
delete [ ] p;
If a pointer has a value NULL, operator delete or delete [ ] does nothing, and this is not an error.
So we use delete like this:
delete [ ]p;
p =NULL;
Example :
#include <iostream.h>
int main()
{
char c, *cptr, x;
c = 'A';
cptr = &c; //cptr is assigned the address of c
x = *cptr; // x is assigned the thing to which c is pointing , so x is assigned 'A'
x = c; // same as statement above
return 0;
}
· new and delete replaces C's malloc and free
· new and delete are more aware of dynamic data type and allow constuctors and destructors
Example :
#include <iostream.h>
int main()
{
// examples
int MAX = 10;
int *pint;
pint = new int[MAX];
for (int i=0; i<MAX; i++)
pint[i] = i*i;
for (i=0; i<MAX; i++)
cout << *(pint+i) << endl;
delete [] pint;
return 0;
}