Week 7: Function Calls in C, Variables (Scope) in C, Program Organization, and Header Files Flashcards
The “STACK” is what?
The stack is the memory set aside as scratch space for a thread of execution. Reserved for each function call
When calling a function, each parameter must…
Reduce down to a single value
What is the difference between pass-by-reference and pass-by-value?
pass-by-reference means passing a variable by its location in memory, which allows a variable to be changed by a function
pass-by-value means literally passing he value stored in the variable with no way of changing the variable stored in main memory
When accessing an array, using the name of the array is essentially….
Using the location in memory of the first item in the array
dbray &(dbray[0]) both mean…
The address of the first element of the array
Arrays are actually…
Pointers
double *ptr = &(array[0]);
is the same as….
double *ptr = array;
The main difference between malloc and calloc is…
Malloc sets aside a certain number of bytes:
double *a;
a = ( double *) malloc (40);
Calloc sets aside a certain number of that type
double *a;
a = (double *) calloc ( 70, sizeof(double) );
Static memory is ____ at the end of each use.
Dynamic memory is ______ at the end of each use unless ____
Static memory is deallocated at the end of each use.
Dynamic memory is not deallocated at the end of each use unless freed
How do you free the following dynamic memory?
double *a;
a = ( double *) malloc (40);
free(a)
Two ways of causing a memory leak are….
Not freeing the memory with free()
Reassigning the pointer variable to another place in dynamic memory before freeing it with free()
What lines of code are required to set aside 70 double variables? Using malloc? Calloc? Calloc another way?
double *a;
a = (double)malloc(708);
or
a = (double)malloc(70(sizeof(double)));
or
a = (double)malloc(708);
or
a = (double *) calloc ( 70, sizeof(double) );
What does a function prototype do?
Tell the preprocessor what to expect as a return type
What is the format of a function protoytype?
float division(int , int , int* , int*);
Function prototypes MUST
End with a semicolon
To avoid including header files more than once…
endif // PROG1_H_INCLUDE
use the following format:
#ifndef PROG1_H_INCLUDED #define PROG1_H_INCLUDED
#define NUMB int float rT; float division(int , int , int* , int*);
What are the two categories of variable types in C?
Compile-time (allocated) variables
(The life time of these kinds of variables is the whole execution period of the C program)
Run-time variables
(The memory space used for these kinds of variables are allocated (reserved) during the
execution (running) of the program)
What are the compile-time (allocated) variables in C?
Global variables — accessible everywhere
Static global variables — accessible within the same C program file
Static local variables — accessible within the same C function
What are the run-time variables in C?
Local variables — accessible only in the block they are declared in
Parameter variables — accessible within the called function
What is a parameter variable?
A variable accessible within the called function
What is a local variable?
A variable accessible only in the block they are declared in
What is a global variable?
A variable accessible everywhere
What is a static global variable?
A variable accessible within the same C program file
What is a static local variable?
A variable accessible within the same C function