ece 103 engineering programming chapter 36 c storage classes herbert g. mayer, psu cs status...

23
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Upload: andrea-booker

Post on 13-Dec-2015

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

ECE 103 Engineering ProgrammingChapter 36

C Storage Classes

Herbert G. Mayer, PSU CSStatus 8/4/2014

Initial content copied verbatim fromECE 103 material developed by

Professor Phillip Wong @ PSU ECE

Page 2: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

Syllabus Variable Storage Classes Memory Storage Layout Storage Classes in Multiple Files Storage Classes for Functions

Page 3: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

3

Variable Storage Classes

Storage class of a variable determines its: Scope Lifetime

C storage classes are: auto static register extern

Page 4: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

4

Scope and Storage: C variable v has scope and storage attributes The storage attribute states, how long v lives The scope attributes states, where v is visible

Storage attributes can be static, auto, or registerScope attributes can be local, global, or external:

Local means, v is only visible inside the current, innermost scope, independent of storage attribute; e.g. there are local static variables in C

Global means, v is visible in the whole compilation unit, from the line of declaration to the end of file

External means, v is visible in this and in other compilation units, and its storage attribute is static

Page 5: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

5

auto Storage Class

auto is the default storage class for a variable defined inside a function body or a statement blockThe auto prefix is optional; i.e. any locally declared variable is automatically auto unless specifically defined to be static

Example:{

auto double x; /* Same as: double x */ int num; /* Same as: auto int num; */

. . .}

Page 6: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

6

Storage for an auto variable is: Allocated when control enters the variable’s

containing block Released when control leaves its containing block

Example:

{

int x;

{

int y = 1;

}

}

enter block

storage created for x

enter block

leave block – storage for y released

storage created for y

leave block – storage for x released

Page 7: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

7

If an auto variable is defined but not initialized: Variable has an unknown value when control enters

its containing block If an auto variable is defined and initialized at the

same time: Variable is re-initialized each time control enters its

containing block An auto variable’s scope is limited to its containing

block (i.e., it is local to the block)

Page 8: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

8

static Storage Class

Storage for a static variable: Is allocated when execution begins Exists for as long as the program is running

A static variable may be defined either inside or outside a function’s body.The static prefix must be included

Example:static double seed;

Page 9: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

9

If a static variable is defined but not initialized: Is set to zero (0) once, when storage is allocated

If a static variable is simultaneously defined and initialized: Is initialized once, when storage is allocated

A static variable defined inside a function body is visible only in its containing block

A static variable defined outside a function body is visible to all blocks which follow it in the current compilation units

If you wish it to be visible in other compilation units, it must be declared extern

Page 10: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

10

Example:

#include <stdio.h>

void weird( int x ){ // weird

static int y; /* Persistent */if ( x == 0 )

printf( "%d\n", y );else if ( x == 1 )

y = 100;else if ( x == 2 )

y++;} //end weird

int main (void){ // main

weird(1); /* Set y in weird to 100 */weird(0); /* Will display 100 */weird(2); /* Increment y in weird */weird(0); /* Will display 101 */return 0;

} // end main

Page 11: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

11

register Storage Class

The fastest storage resides within the CPU itself in high-speed memory cells called registersThe programmer can request the compiler to use a CPU register for storage

Example:register int k;

The compiler can ignore the request, in which case the storage class defaults to autoSome machines –e.g. stack architectures– have no user visible register

Page 12: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

12

extern Storage Class (single source file)

extern is the default storage class for a variable defined outside a function’s body

Storage for an extern variable: Is allocated when execution begins Exists for as long as the program is running

Page 13: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

13

If an extern variable is defined but not initialized: Is set to zero (0) once, when storage is allocated

If an extern variable is simultaneously defined and initialized: Is initialized once, when storage is allocated

An extern variable is visible in all functions that follow its definition (i.e., it is global)

Page 14: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

14

Example:

#include <stdio.h>

float x = 1.5; /* Definition - extern class - global */

void disp (void){

printf("%f\n", x); /* Access global x */}

int main (void){

printf("%f\n", x); /* Access global x */

disp();

return 0;}

Page 15: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

15

Memory Storage Layout

Where are auto, static, and extern variables stored?

Code Segment(Text Segment) Contains the program’s machine code

Data Segment Contains static data (e.g., static class, extern globals)

Stack Segment Contains temporary data (e.g., auto class)

free Unallocated memory that the stack and heap can use

Heap Segment Contains dynamically allocated data (e.g., via malloc)

Page 16: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

16

Storage Classes in Multiple Files

Functions stored in a single source file can be divided into separate source files.

Variables defined in one source file can be accessed from other source files via the extern storage class.

An extern variable can be defined in one file only. However, it may be declared from other files.

Page 17: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

17

An extern variable is defined exactly once in a file by placing it outside all blocks. If an extern variable is not initialized at definition time→ extern prefix must be omitted

If an extern variable is initialized at definition time→ extern prefix is optional

An extern variable is declared in another file by using the extern prefix.

Example:extern int k;

Page 18: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

18

Filename: ex1_A.c

#include <stdio.h>

double pi = 3.141592654; /* Definition of global variable here */

double deg_to_rad (double deg); /* Prototype */

int main (void){

printf("%f\n", deg_to_rad(45.0));return 0;

}

Filename: ex1_B.c

extern double pi; /* Declaration allows it to be used here */

double deg_to_rad (double deg){

return deg * pi / 180;}

Page 19: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

19

Storage Classes for Functions

extern is the default storage class for a function.The extern prefix is usually omitted from the function’s definition.

An extern function can be invoked by any other function in any file.

If a function is invoked in one file but is defined in a different file, then declare its prototype in the file where the function is invoked.

Page 20: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

20

Filename: ex2_A.c

#include <stdio.h>

int a = 123; /* Global in ex2_A.c */

void fun1 (int x); /* Prototype for fun1 in ex2_A.c */void fun2 (void); /* Prototype for fun2 in ex2_B.c */

int main (void){

int a = 2; /* Local to main */

printf("1:main: a= %d\n", a);fun1(a);fun2();printf("8:main: a= %d\n", a);a = 99;printf("9:main: a= %d\n", a);

return 0;}

Page 21: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

21

continued … this code is still part of ex2_A.c

void fun1 (int x)

{

printf("2:fun1: x= %d\n", x);

x = 21;

printf("3:fun1: a= %d\n", a);

{

int a; /* Local to this block in fun1 */

a = 5;

printf("4:fun1: a= %d\n", a);

}

printf("5:fun1: a= %d\n", a);

}

Page 22: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

22

Filename: ex2_B.c

#include <stdio.h>

extern int a; /* Declare variable that resides in ex2_A.c */

void fun2 (void)

{

printf("6:fun2: a= %d\n", a);

a = -99;

printf("7:fun2: a= %d\n", a);

}

Page 23: ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material

23

Compile the source files using gcc:gcc ex2_A.c ex2_B.c or gcc *.c

Actual output:1:main: a= 2

2:fun1: x= 2

3:fun1: a= 123

4:fun1: a= 5

5:fun1: a= 123

6:fun2: a= 123

7:fun2: a= -99

8:main: a= 2

9:main: a= 99