Chapter 6 - Pointers

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

include directive

#include

default parameter value

A function can have a default parameter value for the last parameter(s), meaning a call can optionally omit a corresponding argument.

function prototype

A function declaration is also known as a _

function stub

A function definition whose statements have not yet been written

parameter

A function input specified in a function definition. Ex: A pizza area function might have diameter as an input.

recursive function

A function that calls itself or other functions, either directly or indirectly.

side effects

A function updates a global variable, the function has effects that go beyond its parameters and return value, known as _

Linked List

A linked list consists of items that contain both data and a pointer - a link - to the next list item. Thus, inserting a new item B between existing items A and C just requires changing A to point B's memory location, and B to point to C's location. No shifts occur.

assert()

A macro (similar to a function) that prints an error message and exits the program if assert()'s input expression is false. (Must insert the #include <cassert> library to use)

Pointer Variable (*)

A pointer is a variable that holds a memory address, rather than holding data like most variables. A pointer has a data type, and the data type determines what type of address is held in the pointer. Ex. An integer pointer holds a memory address of an integer. int* ItemPointer

Null Pointer

A pointer that does not point to anything. A pointer that is assigned with the keyword nullptr is said to be null. Ex: int *maxValPointer = nullptr; makes maxValPointer null

incremental development

A process in which a programmer writes, compiles, and tests a small amount of code, then writes, compiles, and tests a small amount more (an incremental amount), and so on.

Function Name Overloading or Function Overloading

A program that has two functions with the same name but differing in the number or types of parameters.

void

A return type means the function returns no value. Ex: A function that outputs data may have no need to return a value. The function typically has no return statement (but may have: return; with no expression)

testbench

A separate program whose sole purpose is to check that a method returns correct output values for a variety of input values.

preprocessor directive

A statement that starts with a #

algorithm

A step-by-step procedure for solving a problem

preprocessor

A tool that scans the file form top to bottom looking for any lines that begin with #

global variable

A variable declared outside any function

Pointer

A variable that contains a memory address. The pointer 'points' to the memory location with that address.

reference

A variable type that refers to another variable. Ex: int usrValInt; int& usrValRef = usrValInt;

Dynamically Allocated Array

An array whose size can change during runtime

function call

An invocation of a function's name, causing the function's statements to execute.

The this pointer

Enables access to an object's data members within the object's class member functions. ex: this ->variable

unusable memory

Memory locations that have been dynamically allocated but can no longer be used by a program.

modular functions for mathematical expressions

Modularity allows more complex functions to incorporate simpler functions. Complex mathematical functions often call other mathematical functions. Ex: A function that calculates the volume or surface area of a cylinder calls a function that returns the area of the cylinder's base, which is needed for both calculations.

pass by value

Passing the value of an argument to a method. The type of data passed depends on whether the argument is a primitive or an object.

header file guards

Preprocessor directives, which cause the compiler to only include the contents of the header file once.

strlen()

Returns the length of a string

recursive algorithm

Solves a problem by breaking that problem into smaller subproblems, solving these subproblems, and combining the solutions.

border cases

Test vectors that represent any extreme inputs that might cause the method to fail. ex. 0 , 999999999, negative numbers

unit testing

Testing of individual components of code (subroutines, modules, and programs) to verify that each unit performs as designed.

hash symbol

The # symbol in #include

Delete[] Operator

The Delete[] operator is used to free memory that was allocated with the new operator.

garbage collection

The automatic process of reclaiming memory when the data of a program no longer need it.

base case

The condition under which a recursive function returns without calling itself, thereby ending the recursive calls.

The Delete Operator

The delete operator deallocates (or frees) a block of memory that was allocated with the new operator. The statement delete pointerVariable; deallocates a memory block pointed to by a pointerVariable. if pointerVariable is null, delete has no effect.

function declaration

The function's return type, name, and parameters, ending with a semicolon where the opening brace would have gone.

stack frame

The locations in the stack area used to store the values referring to one invocation of a routine.

scope

The name of a defined variable or function item is only visible to part of a program, known as the item's _

New Operator

The new operator allocates memory for the given type and returns a pointer to the allocated memory. If the type is a class, the new operator calls the class's constructor after allocating memory for the class's member variables.

modular development

The process of dividing a program into separate modules that can be developed and tested separately and then integrated into a single program.

The Stack

The region where a function's local variables are allocated during a function call. A function call adds local variables to the stack, and a return removes them, like adding and removing dishes from a pile: hence the term "stack." Because this memory is automatically allocated and deallocated, it is also called automatic memory

Static Memory

The region where global variables (variables declared outside any function) as well as static local variables (variables declared inside functions starting with the keyword "static") are allocated. The name "static" comes from these variables not changing (static means not changing); they are allocated once and last for the duration of a program's execution, their addresses staying the same.

The Heap

The region where the "new" operator allocates memory, and where the "delete" operator deallocates memory. The region is also called free store.

Code

The region where the program instructions are stored.

The strchr() and strrchr() Functions

The strchr() and strrchr() functions find a character within a string, and thus have a char as the second parameter. strchr() finds the first occurrence of the character within the string and strrchr() finds the last occurrence.

Vector Class

The vector class internally dynamically allocates an array with an initial size, such as the size specified in the constructor. If the number of elements added to the vector exceeds the capacity of the current internal array, the vector class will dynamically allocate a new array with an increased size, and the contents of the array are copied into a new larger array.

const

This keyword can be prepended to a function's vector or string parameter to prevent the function from modifying the parameter

pass by reference

When a parameter is passed by reference, the memory location originally referred to by the actual parameter is passed to the formal parameter, such that any changes made to the object by the formal parameter also affect the original actual parameter.

The Member Access Operator (->)

When using a pointer to an object, the member access operator (->) allows access to the object's members with the syntax a->b instead of (*a).b. Ex. If myPoint is a pointer to a Point object, myPoint->Print() calls the Print() member function

function

a grouping of predefined statements for repeatedly used operations.

argument

a value provided to a function when the function is called. This value is assigned to the corresponding parameter in the function. Ex: A pizza area function might be called as CalcPizzaArea(12.0) or as CalcPizzaArea(16.0).

function definition

consists of the new function's name and a block of statements. Ex: double CalcPizzaArea() { /* block of statements */ }

test vector

each unique set of input values

Dereference Operator (*)

is prepended to a pointer variable's name to retrieve the data to which the pointer variable points. Ex: If valPointer points to a memory address containing the integer 123, then cout << *valPointer; dereferences valPointer and outputs 123.

Reference Operator (&)

obtains a variable's address. Ex. &someVar returns the memory address of variable someVar.

memory leak

occurs when a program that allocates memory loses the ability to access the allocated memory, typically due to failure to properly destroy/free dynamically allocated memory.

C String Search Functions

strchr(), strrchr(), strstr() are C string library functions that search strings for an occurrence of a character or substring. Each function's first parameter is a const char*, representing the string to search within.

The strstr() Function

strstr() searches for a substring within another string, and thus has a const char* as the second parameter.


Ensembles d'études connexes

U-prep questions Fluid, Electrolyte, and Acid Balance

View Set

(Renal) NCLEX-style review questions from Honan

View Set

History Wk 19: War of the Roses and Tudors

View Set