0% found this document useful (0 votes)
20 views31 pages

Passing An Array To A Function

Uploaded by

Kaasyap Vepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views31 pages

Passing An Array To A Function

Uploaded by

Kaasyap Vepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Passing an array to a function

Figure 8-11 passing arrays


Passing an array to a function
• If array name is a pointer to the first element, we can pass the
array name to a function
• When we pass the array name, no need to use the address
operator
• The array name is a pointer constant, so the array name is
already the address of the first element in the array
• Ex: compute(list) // calling program
• 1) int compute(int ary[]) // 3 different ways
Passing an array to a function

• 2) int compute(int *arySalary) //needs documentation that


array is passed
• 3) for a 3D array int compute(int bigary[][2][5]) //the
compiler needs to know the size of dimension after the first to
calculate the offset for pointer arithmetic
Passing an array to a function

• Program: call a function to multiply each element of a 1D


array by 2
• The variables are shown next
Figure 10-10 Variables for multiply array elements by 2
Passing an array to a function
Memory Allocation Functions
Memory Allocation Functions
• Two choices to reserve memory locations for an object:
• 1. Static memory allocation
• Requires that the declaration and definitions of memory be
fully specified in the source program
• The number of bytes reserved cannot be changed during run
time
• Works fine if the data requirements are exactly known
• This is what we have done so far
• Note: earlier PLs (FORTRAN, COBOL) had this limitation. The data structure need
too be fully specified during compile time
Memory Allocation Functions

• 2. Dynamic memory allocation


• Uses predefined functions to allocate and release memory for
data while the program is running
• Postpones the data definition to run time
• Can be used either with standard data types or with derived
data types
Figure 10-13 Memory Allocation
Understanding dynamic Memory
Allocation
• Conceptually memory is divided into program
memory and data memory
Figure 10-15 A conceptual View of memory

• Note: implementation of memory is left to the designers who design the syste
• Stack and heap can share the same pool of memory
• So the above diagram is only conceptual
Understanding dynamic Memory
Allocation
• program memory:
• Consists of the memory used for main and all called functions
• data memory:
• Consists of permanent definitions such as global data and
constants, local definitions and dynamic data memory

• Note: Exact ways of handling these needs will be decided by the OS and compiler
Understanding dynamic Memory
Allocation
• program memory:
• Consists of the memory used for main and all called functions
• main() must be in memory all the time
• Each called function must be in memory while it is active

• Note: any function has to be in memory when it is running


Understanding dynamic Memory
Allocation
• Memory allocation : stack
• Although the program code for a function may be in memory
at all times
– The local variables for the function are available only when
it is active
• In recursion, more than one version of the function can be
active at a time
– Hence multiple copies of the local variables are allocated
although only one copy of the function is present
• The memory facilities for these capabilities is known as stack
Understanding dynamic Memory
Allocation
• Memory allocation : heap
• Heap is unused memory allocated to the program
• and available to be assigned during its execution
• The memory pool from which memory is allocated when
requested by the memory allocation functions
Understanding dynamic Memory Allocation

//static memory //static memory


int x; int ary[3];

//dynamic memory
int* x; //dynamic memory
x = malloc (. . . ); int* ary;
ary = calloc (. . . );
Figure 10-14 Memory Management Functions

Note: all memory management functions are defined in <stdlib.h>


Block memory allocation (malloc)

• Allocates a block of memory that contains the number of bytes


specified as the parameter
• Returns a void pointer to the first byte of the allocated memory
• void* malloc (size_t size);
• The allocated memory is uninitialized (contains garbage)
• To allocate an integer in the heap
pInt = (int*) malloc (sizeof (int));
• The pointer returned by malloc is shown to be casted as an
integer
• For portability use sizeof()

//pInt = malloc(4);
malloc

• Successful call
• malloc returns the address of the first byte in the memory
allocated
• Unsuccessful call
• malloc returns a NULL pointer
• Overflow – attempt to allocate memory from the heap when
memory is insufficient

• Note: Refer to the memory allocated in the heap only through a pointer
• It does not have its identifier
calloc (contiguous memory allocation)

• To allocate memory for arrays


• 1. allocates a contiguous block of memory to contain an array
of elements of a specified size
• Parameters – number of elements to be allocated, size of each
element
• 2. Returns a pointer to the first element of the allocated array
• Since it is a pointer to an array, the size associated with its
pointer is the size of one element not the entire array
calloc (contiguous memory allocation)

• 3. calloc clears memory. So allocated memory is set to zero


• The result is same for malloc and calloc for overflow and zero
size
• void* calloc(size_t element_count,
size_t element_size);
Figure 10-17 calloc

• Allocating memory for an array of 200 integers


realloc (reallocation of memory )
• Highly inefficient and needs to be used carefully
• Given a pointer to a previously allocated block of memory,
– realloc changes the size of the block by deleting or
extending the memory at the end of the block
– If the memory cannot be extended (due to other
allocations), realloc allocates a completely new block
– Copies the existing memory allocation to the new
allocation and deletes the old allocation
• The programmer must ensure that other pointers to the data
are correctly changed
• void* realloc (void* ptr, size_t newSize);
Figure 10-18 realloc
free (releasing memory)
• When memory allocated by malloc, calloc, realloc are no
longer needed
– They can be freed using free
• It is an error to free memory
– With a null pointer or
– A pointer to other than the first element of an allocated
block
– A pointer that is a different type than the pointer that
allocated the memory
– To refer to memory after it has been released
Figure 10-19 Freeing memory examples

• Ex1: to release a single element allocated with a malloc back to the heap
• Ex2: : to release a 200 element array allocated with a calloc, all 200 elements are
returned back to the heap
free (releasing memory)
• It is not the pointers that are being released but what they
point to
• To release an array of memory that was allocated by calloc,
release the pointer only once
– Releasing memory does not change the value in a pointer
– It still contains the address in the heap
– It is a logical error to use the memory once it is released
– After freeing the memory, clear the pointer by setting to
NULL (to ease debugging)
free (releasing memory)

• It is not necessary to clear memory at the end of the program


• OS will release all memory when the program is terminated
• So, free memory whenever it is no longer needed
• void free (void* ptr);

You might also like