dSum += dScoreM[i];. } return dSum / iScoreCount;. } void showScores(char *
pszName, double dScoreM[]. , int iScoreCount). { int i; printf("Scores for %s\n", ...
clark
cs1713
syllabus
lecture notes
programming assignments
Functions In C, functions are used to modularize coding. A function has the following syntax: dataType functionName (parameterList) { functionBodyStatements return expression; } If the function doesn’t functionally return a value, we specify void functionName (parameterList) { functionBodyStatements }
homework
set up
double calculateAverage(double dScoreM[], int iScoreCount) { int i; double dSum = 0.0; if (iScoreCount iScoreCount; i++) { if (pstudentScores->dScoreM[i] < *pdMin) *pdMin = pstudentScores->dScoreM[i]; if (pstudentScores->dScoreM[i] > *pdMax) *pdMax = pstudentScores->dScoreM[i]; } }
Storage Classifications auto Automatic variables receive memory when their declaration is encountered at runtime. When the block of code containing an autmatic variable is exited, their memory is freed. Automatic variables can be declared at the beginning of A function A compound statement enclosed in {} In C++ and Java, you can declare variables as you need them.
void funcA () { int iVar1; double dVar2; statements1 if (iVar1 > 5) { char szString1[50]; int iLength; statements2 } statements3 }
// auto allocation of iVar1 // auto allocation of dVar2
// auto allocation of szString1 // auto allocation of iLength // frees szString1 and iLength // frees iVar1 and dVar2
Storage Classifications extern Extern variables receive their memory when the program is loaded. All variables declared at the top of a file (before function definitions) are a type of extern variable. Multiple C files can reference the same external variables. One declaration must not contain the keyword extern. It is considered the basis declaration. The basis declaration can contain initializations. All references to the extern variable other than the single basis declaration must use the extern keyword. Memory is freed when the program exits.
/* file partA.c */ double dVectorM[300] = {100, 200, 150, 300, 20, 5, 100, 30}; int iVectorLength = 8; /* functions in partA.c */
Storage Classifications static Static variables retain their values throughout execution. Additionally, static variables are private to the C function or C file where they are declared. They do not conflict with extern variables in other files.
/* file partD.c */ static dVectorM[200] = {50, 60, 80}; static int iVectorLength = 3;
Storage Classifications dynamic Pointer variables must be automatic, extern or static; however, what they reference can be allocated memory dynamically via malloc() or calloc(). Memory is allocated from a heap.
typedef struct { char szName[30]; double dScore[50]; int iScoreCount; } StudentScores;
/* file partB.c */ extern double dVectorM[300]; extern int iVectorLength; /* functions in partB.c */ /* file partC.c */ extern double dVectorM[300]; extern int iVectorLength;
void myFunc() { static int iRetainValue; int iAutoValue; statements; } // iAutoValue is freed, iRetainValue is not freed
Explicitly allocated with a malloc() or calloc() Must be freed explicitly using a free(). Helpful for managing objects where the size varies If memory is not managed well, memory leaks can occur.
Exercise Consider the code for files exAc. and exB.c. /* file exB.c */ extern int iMyVar; static int iMyVar2; void bfunc() { int iHey; /* statements */ }
Variable iMyVar in exA.c iMyVar2 in exA.c iParm in exA.c (myFunc) iHello in exA.c iElectricity in exA.c iHey in exA.c iKite in exA.c iMyVar in exB.c iMyVar2 in exB.c iHey in exB.c
When does it get allocated?
StudentScores *pstudentScores; pstudentScores = malloc(sizeof(StudentScores)); … free(pstudentScores); /* file exA.c */ int iMyVar; int iMyVar2; void myFunc (int iParm) { int iHello; static int iElectricity; /* statements */ } int anotherFunc () { int iHey; static int iKite; /* statements */ } When is it freed?
Same as another variable? ones?
Which