Memory As A Programming Concept in C and C Frantisek Franek PDF Download
Memory As A Programming Concept in C and C Frantisek Franek PDF Download
https://ebookultra.com/download/memory-as-a-programming-concept-
in-c-and-c-frantisek-franek/
https://ebookultra.com/download/programming-in-c-a-primer-3rd-edition-
e-balagurusamy/
https://ebookultra.com/download/programming-concepts-in-c-2nd-edition-
robert-burns/
https://ebookultra.com/download/programming-in-c-2-e-second-edition-
dey/
https://ebookultra.com/download/functional-programming-in-c-first-
edition-ivan-cukic/
Algebraic Geometry A Volume in Memory of Paolo Francia
Mauro C. Beltrametti (Editor)
https://ebookultra.com/download/algebraic-geometry-a-volume-in-memory-
of-paolo-francia-mauro-c-beltrametti-editor/
https://ebookultra.com/download/c-programming-an-introduction-rajiv-
chopra/
https://ebookultra.com/download/c-programming-5th-edition-mike-
mcgrath/
https://ebookultra.com/download/programming-c-1st-edition-jesse-
liberty/
https://ebookultra.com/download/c-programming-in-linux-1st-edition-
edition-haskins-d/
Memory as a Programming Concept in C and C
Frantisek Franek Digital Instant Download
Author(s): Frantisek Franek
ISBN(s): 9780521520430, 0521520436
Edition: Paperback
File Details: PDF, 1.59 MB
Year: 2003
Language: english
This page intentionally left blank
MEMORY AS A PROGRAMMING CONCEPT
IN C AND C++
FR ANTISEK FR ANEK
McMaster University
CAMBRIDGE UNIVERSITY PRESS
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo, Delhi
A catalog record for this publication is available from the British Library.
Acknowledgments
page ix
1
Introduction
page 1
2
From Source File to Executable File
page 7
Transformation of a source file to a load (executable) module. Why
we can and do discuss source programs and their behavior as if they
were executing somewhere in memory in their source form. Concepts
of static memory allocation, dynamic memory allocation, program
address space, and program system stack.
3
Variables and Objects; Pointers and Addresses
page 21
Variables as “data containers” with names. Values as data – simple (in-
nate or elementary) data, structures, and objects. Referencing variables
v
CONTENTS
4
Dynamic Allocation and Deallocation of Memory
page 45
Fundamentals of dynamic allocation and deallocation of memory: free
store (system heap); per-process memory manager; C memory allocators
malloc(), calloc(), and realloc(); and C deallocator free(). How to
handle memory allocation/deallocation errors.
5
Functions and Function Calls
page 59
System stack, activation frame, activation frame as the storage for local
auto objects and for function arguments. Passing arguments by value
as opposed to by reference. Calling sequence. Recursion and its relation
to activation frames and the system stack. The price of recursion.
6
One-Dimensional Arrays and Strings
page 81
Static one-dimensional arrays and their representation as pointers.
Array indexing as indirection. Why an array index range check cannot
be performed in C /C++. The price of run-time array index range check-
ing; the “compile-time checking” versus “run-time checking” philoso-
phies. Passing static one-dimensional arrays as function arguments.
Definition versus declaration of one-dimensional arrays. Dynamic one-
dimensional arrays. Strings as static or dynamic one-dimensional char
arrays terminated with NULL . How to add a custom-made run-time
index range checker in C++.
7
Multi-Dimensional Arrays
page 97
Static multi-dimensional arrays and their representation. Row-major
storage format and the access formula. Passing multi-dimensional
arrays as function arguments. Dynamic multi-dimensional arrays.
vi
CONTENTS
8
Classes and Objects
page 106
Basic ideas of object orientation; the concepts of classes and objects.
Operators new, new[], delete, and delete[], and related issues. Con-
structors and destructors.
9
Linked Data Structures
page 132
Fundamentals, advantages, and disadvantages of linked data struc-
tures. Moving a linked data structure in memory, or to/from a disk, or
transmitting it across a communication channel – techniques of com-
paction and serialization. Memory allocation from a specific arena.
10
Memory Leaks and Their Debugging
page 159
Classification of the causes of memory leaks. Tracing memory leaks
in C programs using location reporting and allocation/deallocation
information-gathering versions of the C allocators and deallocators.
Tracing memory leaks in C++ programs: overloading the operators new
and delete and the problems it causes. Techniques for location tracing.
Counting objects in C++. Smart pointers as a remedy for memory leaks
caused by the undetermined ownership problem.
11
Programs in Execution: Processes and Threads
page 187
Environment and environment variables, command-line arguments
and command-line argument structure. A process and its main at-
tributes – user space and process image. Spawning a new process (UNIX
fork() system call ) from the memory point of view. Principles of inter-
process communication; SystemV shared memory segments and “shared
memory leaks”. Threads and lightweight processes; advantages and dis-
advantages of threads over processes. The need to protect the “common”
data in threads. Memory leaks caused by careless multithreading.
A
Hanoi Towers Puzzle
page 210
vii
CONTENTS
B
Tracing Objects in C++
page 216
C
Tracing Objects and Memory in C++
page 227
D
Thread-Safe and Process-Safe Reporting
and Logging Functions
page 234
Glossary
page 239
Index
page 255
viii
ACKNOWLEDGMENTS
Every book is to a significant degree a team effort; there are always many
people essential for the book’s publication, from the author(s) all the way
to the editors and publisher. This book is no exception, and my sincere
gratitude goes to all the people who contributed to its publication. My
special thanks go to George Grosman, a musician and man of letters,
for his help with the style and the grammar (English is not my mother
tongue), and to Dr. Jan Holub, a postdoctoral Fellow in the Department of
Computing and Software at McMaster University, for his careful reading
of the manuscript and checking of the technical aspects of the text.
ix
To my parents
Prof. Dr. Jiří and Zdeňka Franěk for everything;
and my mentors and best friends
Dr. B. Balcar DrSc., Czech Academy of Sciences,
Prof. Emeritus Dr. A. Rosa, McMaster University,
Honorable V. L. Rosicky, Consul of the Czech Republic,
formerly president of Terren Corp.
for everything I know about computers and mathematics;
and my wife Marie and children Jacob and Nicole,
for their love, support, and understanding
INTRODUCTION
The motivation for this book came from years of observing computer
science students at universities as well as professional programmers work-
ing in software development. I had come to the conclusion that there
seemed to be a gap in their understanding of programming. They usu-
ally understood the syntax of the programming language they were using
and had a reasonable grasp of such topics as algorithms and data struc-
tures. However, a program is not executed in a vacuum; it is executed in
computer memory. This simple fact exerts a powerful influence on the
actual behavior of the program – or, expressed more precisely, a subtle
yet powerful influence on the semantics of the particular programming
language. I had observed that many students and programmers did not
fully understand how memory affected the behavior of the C and C++ pro-
grams they were designing. This book is an attempt to fill this gap and
provide students and programmers alike with a text that is focused on
this topic.
In a typical computer science curriculum, it is expected that students
take courses in computer architecture, operating systems, compilers, and
principles of programming languages – courses that should provide them
with a “model” of how memory matters in the behavior of programs.
1
MEMORY AS A PROGRAMMING CONCEPT
However, not all students end up taking all these courses, and even if
they do, they may not take them in the right order. Often the courses are
presented in a disjointed way, making it difficult for students to forge a
unified view of how memory affects the execution of programs. Addition-
ally, not all programmers are graduates of university or college programs
that feature a typical computer science curriculum. Whatever the reasons,
there seems to be a significant number of computer science students and
professional programmers who lack a full understanding of the intricate
relationship between programs and memory. In this book we will try to
pull together the various pieces of knowledge related to the topic from all
the fields involved (operating systems, computer architecture, compilers,
principles of programming languages, and C and C++ programming) into
a coherent picture. This should free the reader from searching various
texts for relevant information. However, in no way should this book be
viewed as a programming text, for it assumes that the reader has at least
an intermediate level of programming skills in C or C++ and hence sim-
ple programming concepts are not explained. Nor should this book be
viewed as an advanced C/C++ programming text, for it leaves too many
topics – the ones not directly related to memory – uncovered (e.g., virtual
methods and dynamic binding in C++). Moreover, it should not be seen
as an operating system book, for it does not delve into the general issues
of the discipline and only refers to facts that are relevant to C and C++
programmers.
Unfortunately, there seems to be no curriculum at any university or
college covering this topic on its own. As a result, students usually end
up with three or four disjointed views: programming syntax and (an in-
complete) C/C++ semantics; algorithms and data structures, with their
emphasis on the mathematical treatment of the subject; operating sys-
tems; and possibly compilers. Although my ambition is to fill the gaps
among these various views – at least from the perspective of C/C++ pro-
gramming – I hope that the book proves to be a valuable supplement to
any of the topics mentioned.
My own experience with software development in the real world shows
that an overwhelming number of computer program bugs and problems
are related to memory in some way. This is not so surprising, since there
are in fact few ways to “crash” a program and most involve memory. For
instance, a common problem in C/C++ is accessing an array item with
an index that is out of range (see Chapter 6). A program with such a sim-
ple bug can exhibit totally erratic behavior during different executions,
2
INTRODUCTION
3
Random documents with unrelated
content Scribd suggests to you:
so that one side of the propeller is not heavier than the other. The
shaft is pushed through the bearing tube and should project about
¼ inch beyond it. A strip of tin is wound around this projecting end
of the shaft and soldered to it in such a manner that the shaft is free
to turn in the tube.
When the plane is completely assembled try it to find the point at
which it balances when rested on the finger under the fuselage. A
hole should be punched at this point large enough to admit the iron
rod or piece of heavy wire that is to be used for the spike on which
to mount the weathervane. A second hole is punched directly above
the first one; this hole is considerably smaller than the hole beneath
it. The top of the iron spike that supports the aeroplane
weathervane is filed down to a smaller diameter so that when the
spike is pushed through the larger hole the smaller or filed part of
the spike will go through the hole in the upper part of the fuselage.
The weathervane will then rest on the shoulder formed on the spike
as shown in the illustration. A block of wood may be nailed to the
roof peak of the house or barn and a hole bored into it the size of
the supporting spike, and the spike may be pushed into this and the
aeroplane weathervane mounted on the spike. It should be well
painted in bright colors and if well made will prove a very pleasing
toy.
CHAPTER XIX
Candlesticks
WALL SCONCES, AND A LANTERN
A fighting tank made by the author. The tank is made of two mackerel tins.
Parts of pepper boxes, bottle caps and a few nails used as guns
The lantern is not made from a rectangular can, but it is made
from two square pieces of tin used for the top and bottom, part of a
can being fitted in a hole cut in the square piece used for the top of
the lantern. The four corner pieces of the lantern are made of strips
of tin cut at a right angle.
Fig. 94.
A sliding door is made from a flat sheet of tin, this door sliding
between two folded strips of tin which are soldered to the
framework of the lantern. Three pieces of glass are used for the
lantern, as these are held in place by small pieces of tin folded at an
angle, one part of which rests against the glass and the other part is
soldered to the tin work of the lantern. These pieces are put in place
as each piece of glass is placed in the lantern, one at the top and
the other at the bottom of each piece of glass.
CHAPTER XX
Camp and Kitchen Equipment
A COFFEE POT—BOILING PAILS—FRYING PAN—
TOASTER—A CAMP SHOWER BATH—
CANTEEN OR HOT WATER BOTTLE—A
MATCH BOX
Fig. 95.
Fig. 96.
The top of the can is removed and a strong handle fixed to the
can. A small nipple of tin is soldered to the side of the can, near the
bottom. This nipple is simply a flat piece of tin rolled into a
cylindrical shape and of a suitable size so that a piece of rubber hose
may be fitted tightly over it.
A second nipple of the same size should be made for the spray
nozzle. The spray nozzle is made from a shoe paste or salve box. A
number of fine holes are punched in the box lid and the tin pipe or
nipple is soldered in a hole made for it in the bottom of the box.
A wire hook is provided at the rim of the pail to hold the spray
nozzle in place when it is not desired to have the water run out of it.
It will be found convenient to have a double pulley and rope
rigged to hoist the pail to a convenient height after filling.
The Match Box.—The match box is made of two cigarette boxes,
one for good matches and the other for burned matches. These
boxes are of ample size to hold the paper drawer of a large box of
parlor matches.
The hinged top is left on the box that is to hold the unburned
matches. This box is soldered to two supporting brackets in such a
manner that it is held away from the piece of tin forming the back
for the two boxes and so that the lid of the upper box may be
raised. The lower box is simply soldered to the back piece. Three
folded strips of tin are soldered to the front of this second box to
form a holder for a strip of sand paper to strike the matches on.
CHAPTER XXI
Preparing the Toys for Painting
REMOVING SURPLUS SOLDER WITH SCRAPERS
—MAKING A HOE SCRAPER—PLUMBERS’
AND ROOFERS’ SCRAPERS—SCRAPING AND
FILING—BOILING THE TOYS IN A LYE BATH
—VENT HOLES
A
Aeroplane weathervane, 187-191
Ambulance body, 154, 155
Anvils, 40, 41
Appliances, shop, 39-43
Ash trays, 86-93
Assembling auto truck, 139-145
Auto truck, making, 107-145
army, 153-155
axles, 114
bodies, 146-156
cabs, 165
chassis, 118-126
coal truck, 152, 153
dash board, 135-138
drilling holes in wheel centers, 113, 114
fire engine, 155, 156
fittings, 161-165
hood, 127-134
horns, 163
lights, 161-163
mud guards, 160
radiator, 125-134
soldering filler cap on radiator, 132-134
soldering seat, 138
soldering wheels to axles, 141-143
starting crank, 157, 158
steering wheel, 159
street sprinkler, 151-153
tank truck, 151
tool boxes, 163, 164
vents or louvers in hood, 130-132
washers for axles, 143-145
Auto truck wheel centers, finding, 37, 38
wheel making, 107-118
wheel, making hole in center, 112-114
B
Bench, work, 41, 42
Bending strips of tin to design, 81-83
wire in vise, 157
Biscuit cutter, 44-53
Block, punching and forming, 50
Bodies, ambulance, 154, 155
auto truck, 146-156
coal, 152
different bodies fitted to same chassis, 149-156
fire engine, 155, 156
street sprinkler, 151, 152
tank, 151
Boats, 166-175
battleship, 171, 172
ferry, 172, 173
row, 166, 167
sail, 167, 168
scow, 167
tug, 169-171
Burns, remedy for, 26
C
Cabs, truck, 165
Cabs, locomotive, 176, 177
Camp equipment, 195-199
canteen, 196
coffee pot, 195
cooking pails, 196
frying pan, 196
hot water bottle, 196
Camp equipment, match box, 199
shower bath, 196-199
toaster, 196
Candlesticks, 94-99, 192, 193
Cars, railroad, 178-181
Charcoal and wood fires for heating soldering coppers, 59, 60
Chassis, forming truck, 118-126
Cleaning and scraping tin for soldering, 71-73
cans, 21-25
Connecting rods for locomotive, 177, 178
Cooky cutters, 79-85
Cuts, remedy for, 26
Cutting away surplus tin at can rims, 101, 102
holes in tin with chisel, 96, 97
into cans, 22-24
narrow strips of tin, 80, 81
Cylinders, locomotive, 177, 178
D
Dash boards, truck, 135-138
Dividers, spring, 33
Double cutting shears, 24
Drilling hole in wheel centers, 113, 114
E
Electrical soldering coppers, 60, 67, 68
F
Filing tin, 45, 46
the soldering copper, 63-65
Fire engine, 155, 156
Fittings for truck, 161-165
Flux, applying, 74
Fluxes for soldering, 55, 62-70
Folding tin by hand, 50, 51
hatchet stake, 123-125
vise, 123
wooden roofing folder, 120-123
Forming chassis for truck, 118-126
mallet, 42, 43
using, 87-91
Forming a wire handle, 105, 106
Frying pan, 196
G
Galvanized wire, sizes, 30
used for axles, 114
Gas furnace for heating soldering coppers, 58, 59
Gasoline torch for heating soldering coppers, 58, 59
H
Handle, forming for biscuit cutter, 49-53
Hatchet stake used for folding, 123-125
Heating apparatus for soldering coppers, 55-60
soldering coppers, 65-68
Hood, truck, 127-134
Horns, truck, 163
Hot water bottle, 196
I
Ice pick used as punch, 112, 113
K
“Killed” or soldering acid, making, 68-70
L
Laying out work, 32-39
Lantern, 192, 193
Lights, truck, 161, 162
Locomotive, 174-179
boiler, 176
cab, 176
connecting rods, 177, 178
cylinders, 177, 178
fittings, 178
frame, 174-177
Locomotive wheels, 177
Lugs for pail handle, 102-105
Lye bath, description, 20
used for cleaning cans, 72, 73
M
Marking awl, 32
off work, 32, 33
line around a can, 22
Match box, 199
holder and ash tray, 91-93
Materials needed aside from cans, 30
Mechanical toys, 182-191
aeroplane weathervane, 187-191
sandmills, 182
steam turbine and boiler, 182, 183
water wheels, 182
windmill, 187
Melting off can lids, 110, 111
Metal shears, 25
Mud guards for truck, 160
N
Notes on painting the toys, 206-210
O
Oil stove used for heating soldering coppers, 56-61
P
Painting tin can toys, 206-210
Paints used for tin can toys, 206
Points to remember about soldering, 75-77
Preparing cans for toy making, 20-22
toys for painting, 200-205
Punches, 47-49
Punching a hole in tin, 46-49
Punching holes with ice pick, 112, 113
holes in radiator, 129
R
Radiator, truck, 127-134
Riveting, 100, 103-105
Rivets, 30
Running boards, truck, 160, 161
S
Sandmills, 182
Sconces, wall, 192
Scrapers, 201, 202
home-made, 201
plumbers’ and roofers’, 201, 202
Scraping away surplus solder, 200-202
Seat, truck, 138
Sheet tin, 54
Shower bath, 196-199
Smokestack for locomotive, 178
Soft solder, 54
other methods of applying, 78
Soldering, 54-70
candlestick, 99
cleaning and scraping for, 71-73
cooky cutter, 83-85
filler cap on radiator, 132-134
heating apparatus for, 55-60
hood and radiator to truck chassis, 139
“Killed Acid,” making, 68-70
narrow strips of tin, 81-83
paste, 62, 63
other methods of applying, 78
points to remember about, 75-77
practice piece, 73-78
process, 55
Soldering, scraping away surplus solder, 200-202
soft solder, 54
strips of tin to flat piece 83
wheels to axles, 141-143
copper or “iron,” 60-62
cleaning with “killed acid,” 63-67
electrical, 60, 67, 68
filing, 63-65
fitting handle to, 62
heating, 65-68
tinning, 63-68
Springs, truck, 140, 141
Squaring up a piece of tin, 34, 35
Starting crank, 157, 158
Steam turbine, 182, 183
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookultra.com