cmpt130_08memory

cmpt130_08memory - Storage Classes Storage Classes Scope...

Info iconThis preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Storage Classes Storage Classes Scope and linkage Storage classes Automatic memory Dynamic memory Memory Model Storage Classes There are five storage classes That specify the lifetime and visibility of a variable This does not include dynamic memory The different storage classes vary in combinations of three categories Scope Linkage Duration Scope and Linkage Scope The scope of a variable is the region of the program in which it is visible Block scope Function prototype scope File scope Block Scope A block is a region of a program enclosed in opening and closing (curly) brackets Function definitions If and switch statements Loops ... A variable defined in a block is visible from where it is defined to the end of the block More About Block Scope Two variables with the same name cannot be declared with the same scope But the scope of variables with the same name may overlap Only the variable with the least scope is visible Function parameters have the same scope as the function block Even though they are declared outside the functions enclosing brackets Loops and C99 Prior to the C99 standard variables had to be declared at the start of a block Compilers compliant with C99 allow variables to be declared anywhere in a block Including in the initialization statement of a for loop If a variable is declared inside a for loop its scope is the loop Function Prototype Scope Applies to variable names used in function prototypes The scope only applies to the function prototype Names given to formal parameters may differ from the names used in the prototype File Scope A variable with its definition outside any function has file scope It is visible from the point it is defined to the end of the file containing its definition It may also be visible to other files Linkage Linkage refers to the file visibility of variables Variables with block scope have no linkage They are private to the block or prototype in which they are defined Variables with file scope have either internal linkage or external linkage A variable with internal linkage can be used Linkage and static By default, a variable with file scope has external linkage If the variable is to be visible only within one file it should be specified as static static int answer = 42; Storage Classes Duration A variable has one of three storage durations Static Automatic Dynamic The duration of a variable determines its lifetime within a program Static Storage Statically stored variables last for the lifetime of a program The number of static variables does not change as a program runs So no special system is required to maintain them They are usually allocated space sequentially in an area of main memory They are initialized to default values Types of Static Variable Static variables can have one of three types of linkage External Internal None External Linkage...
View Full Document

This note was uploaded on 12/23/2011 for the course CMPT 120 taught by Professor Cuikerman during the Fall '08 term at Simon Fraser.

Page1 / 67

cmpt130_08memory - Storage Classes Storage Classes Scope...

This preview shows document pages 1 - 17. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online