PDF Pointers in C Programming A Modern Approach to Memory Management Recursive Data Structures Strings and Arrays 1st Edition Thomas Mailund download
PDF Pointers in C Programming A Modern Approach to Memory Management Recursive Data Structures Strings and Arrays 1st Edition Thomas Mailund download
com
https://ebookmeta.com/product/pointers-in-c-programming-a-
modern-approach-to-memory-management-recursive-data-
structures-strings-and-arrays-1st-edition-thomas-mailund/
OR CLICK HERE
DOWLOAD NOW
https://ebookmeta.com/product/functional-data-structures-in-r-1st-
edition-thomas-mailund/
ebookmeta.com
https://ebookmeta.com/product/functional-programming-in-r-4-second-
edition-thomas-mailund/
ebookmeta.com
https://ebookmeta.com/product/microwave-plasma-sources-and-methods-in-
processing-technology-1st-edition-ladislav-bardos/
ebookmeta.com
Losing Face Shame Society and the Self in Early Modern
England 1st Edition Ilana Krausman Ben Amos
https://ebookmeta.com/product/losing-face-shame-society-and-the-self-
in-early-modern-england-1st-edition-ilana-krausman-ben-amos/
ebookmeta.com
https://ebookmeta.com/product/fall-into-savagery-men-of-wrath-
book-3-do/
ebookmeta.com
https://ebookmeta.com/product/the-cultural-heritage-of-nagaland-1st-
edition-g-kanato-chophy/
ebookmeta.com
https://ebookmeta.com/product/hegel-and-modern-philosophy-1st-edition-
david-lamb/
ebookmeta.com
https://ebookmeta.com/product/organic-semiconductors-for-
optoelectronics-1st-edition-hiroyoshi-naito/
ebookmeta.com
Conflict After the Cold War Arguments on Causes of War and
Peace 6th Edition Richard K Betts
https://ebookmeta.com/product/conflict-after-the-cold-war-arguments-
on-causes-of-war-and-peace-6th-edition-richard-k-betts/
ebookmeta.com
Pointers in C
Programming
A Modern Approach to Memory
Management, Recursive Data Structures,
Strings, and Arrays
—
Thomas Mailund
Pointers in C
Programming
A Modern Approach to Memory
Management, Recursive Data
Structures, Strings, and Arrays
Thomas Mailund
Pointers in C Programming: A Modern Approach to Memory Management, Recursive
Data Structures, Strings, and Arrays
Thomas Mailund
Aarhus N, Denmark
Chapter 1: Introduction�������������������������������������������������������������������������������������������� 1
Chapter 3: Pointers������������������������������������������������������������������������������������������������� 33
Call by Reference������������������������������������������������������������������������������������������������������������������������ 36
NULL Pointers����������������������������������������������������������������������������������������������������������������������������� 49
Const and Pointers���������������������������������������������������������������������������������������������������������������������� 53
Restricted Pointers���������������������������������������������������������������������������������������������������������������������� 66
iii
Table of Contents
Character Pointers����������������������������������������������������������������������������������������������������������������� 81
Arbitrary Types����������������������������������������������������������������������������������������������������������������������� 82
Void Pointers������������������������������������������������������������������������������������������������������������������������������� 85
Chapter 5: Arrays���������������������������������������������������������������������������������������������������� 91
Arrays, Indices, and Pointer Arithmetic��������������������������������������������������������������������������������������� 94
Out-of-Bounds Errors���������������������������������������������������������������������������������������������������������������� 100
Pointers to Arrays���������������������������������������������������������������������������������������������������������������������� 101
Arrays and Function Arguments������������������������������������������������������������������������������������������������ 102
Multidimensional Arrays����������������������������������������������������������������������������������������������������������� 106
iv
Table of Contents
v
Table of Contents
vi
Table of Contents
Index��������������������������������������������������������������������������������������������������������������������� 527
vii
About the Author
Thomas Mailund is an associate professor in bioinformatics at Aarhus University,
Denmark. He has a background in math and computer science. For the past decade,
his main focus has been on genetics and evolutionary studies, particularly comparative
genomics, speciation, and gene flow between emerging species. He has published
String Algorithms in C, R Data Science Quick Reference, The Joys of Hashing, Domain-
Specific Languages in R, Beginning Data Science in R, Functional Programming in R, and
Metaprogramming in R, all from Apress, as well as other books.
ix
About the Technical Reviewer
Juturi Narsimha Rao has 9 years of experience as a software developer, lead engineer,
project engineer, and individual contributor. His current focus is on advanced supply
chain planning between the manufacturing industries and vendors.
xi
Acknowledgments
I am grateful to Helge Jensen, Anders E. Halager, Irfansha Shaik, and Kristian Ozol for
discussions and comments on earlier drafts of this book.
xiii
CHAPTER 1
Introduction
Pointers and memory management are considered among the most challenging issues
to deal with in low-level programming languages such as C. It is not that pointers are
conceptually difficult to understand, nor is it difficult to comprehend how we can obtain
memory from the operating system and how we return the memory again so it can be
reused. The difficulty stems from the flexibility with which pointers let us manipulate the
entire state of a running program. With pointers, every object anywhere in a program’s
memory is available to us—at least in principle. We can change any bit to our heart’s
desire. No data are safe from our pointers, not even the program that we run—a running
program is nothing but data in the computer’s memory, and in theory, we can modify
our own code as we run it.
With such a power tool, it should hardly surprise that mistakes can be fatal for a
program, and unfortunately, mistakes are easy to make when it comes to pointers. While
pointers do have type information, type safety is minimal when you use them. If you
point somewhere in memory and pronounce that you want “that integer over there,” you
get an integer, no matter what the object “over there” really is. Treat it like an integer,
and it behaves like an integer. Assign a value to it, and may the gods have mercy on your
soul if it was supposed to be something else and something you need later. You have just
destroyed the real object you pointed at.
If you are not careful, any small mistake can crash your program—or worse. If you
accidentally modify the incorrect data in your program, all your output is tainted. If you
are lucky, it is easily detectable, and you are in for a fun few days of debugging. If you
are less fortunate, you can make business decisions based on incorrect output for years
to come, never realizing that the code you wrote is fooling you every time it runs—or
maybe not every time, just on infrequent occasions, so rare that you can never chase
down the problem. When you have bugs caused by pointers (or uninitialized memory),
1
© Thomas Mailund 2021
T. Mailund, Pointers in C Programming, https://doi.org/10.1007/978-1-4842-6927-5_1
Chapter 1 Introduction
they are not always reproducible. Your program’s behavior might depend on which other
programs are running concurrently on the computer. If you start debugging it, any code
you add to the program to examine it will affect its behavior. Loading the program into a
debugger will definitely change the behavior as well. I hope that you will never run into
such bugs—known as Heisenbugs after Heisenberg’s uncertainty principle—but if you
mess around with pointers long enough, you likely will.
It sounds like pointers are something we should stay away from, and many high-level
programming languages do try to avoid them. Instead, they provide alternative language
constructions that are safer to use but provide much of the same functionality that we
need pointers for in C. They are not as powerful but alleviate many of the dangers that
raw memory pointers pose. In low-level languages such as C, we are programming much
closer to the machine. The computer doesn’t understand high-level constructions; it
understands memory and chunks of bits, and in low-level languages, we can manipulate
the computer at this fundamental level. We very rarely need to, nor do we want to, but
when we choose to program in low-level languages, it is to get close to the machine,
where we can write more efficient programs, measured in both speed and memory
usage. And at this level, we get pointers—more efficient, more fundamental, and more
dangerous. If, however, we approach using pointers in a structured manner, we can
achieve the safety of high-level languages and the efficiency of low-level languages. The
burden is on the programmer, rather than the language designer, but we can get the best
of both worlds for anything that you can do in a high-level language—while maintaining
the real power of pointers in the rare cases where you need more.
In this book, I will explain the basic memory model that C programs assume about
the computer they run on and how pointers let us access data anywhere in memory. I will
explain how you get safe access to memory, by allocating blocks of memory you need, so
they are yours to manipulate, and how you can release memory when you no longer need
it, so you do not run out of memory before your computations are done. I will explain how
pointers are essential for building complex data structures and how you can approach this
in a structured way, so they are safe to use. And I will show you how you can use pointers to
functions to implement higher-order functions and polymorphic data structures.
I will not cover basic C programming. This is not an introduction to programming or
the language. I will assume that you already know the basics and will jump directly into
memory and pointers. I will not cover issues related to concurrency and interruptions
and such either. That would lengthen the book substantially, and there are already
excellent books where you can explore this further.
2
CHAPTER 2
#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}
We don’t need to think about the computer’s memory when we write it (or execute
it). Still, many objects must necessarily be represented in memory before we can run
the program—the program itself, including the main() function we write ourselves
and the printf() function we get from the runtime system. The two arguments we
give to main(), argc and argv, are stored somewhere, as is the constant string "Hello,
world!\n".
3
© Thomas Mailund 2021
T. Mailund, Pointers in C Programming, https://doi.org/10.1007/978-1-4842-6927-5_2
Chapter 2 Memory, Objects, and Addresses
int factorial(int n)
{
if (n <= 1) return 1;
else return n * factorial(n - 1);
}
When we call the function, we must store the argument, n, somewhere. In the
recursive case, we call the function again, and in the second call, we need another
parameter n. We need another one because we need to remember the current n so we
can multiply it to the result of the recursion. Each recursive call must have its own n
stored somewhere in memory.
We don’t have to worry about where the functions, variables, and constants live in
memory when we write this code because the C compiler will generate the necessary
machine code to handle it for us. It will allocate the space for constants and variables,
and it handles writing function parameters and assignments to variables into the correct
memory locations. When we read the value in a variable, it handles getting it from the
right memory location for us as well.
However, when we choose to program in a low-level language, like C, the raw
memory is never too far away. It is possible to hide memory entirely from the
programmer, to pretend that objects are floating around somewhere and never wonder
about where that is. However, it comes at a computational overhead, and it limits what
we can do with a program in some ways. Low-level languages do not do this. They let
us get the memory of objects and manipulate the memory directly. We do not do this
willy-nilly because if we did, we would write unmaintainable software. Still, we have the
power, and when we use this power carefully, and in a structured way, we can build the
features that high-level languages provide using a single mental framework and with
little computational overhead.
4
Other documents randomly have
different content
Kultursprachen übersetzten Werke wirkten, der deutschen Zoologie
eine Anerkennung erzwungen, die von keinem anderen Forscher in
ähnlichem Maße ausging und die höchstens der Wirkung Cuviers zu
vergleichen ist. Als Lehrer hat Haeckel eine ausgedehnte Schule von
Entwicklungstheoretikern sowohl wie von mehr empirisch tätigen
Forschern begründet, der die Vertiefung der Entwicklungslehre mit
ihre wesentlichsten Züge verdankt. Der Rahmen unserer Arbeit,
sowie der Umfang und die Aktualität des Stoffes verbietet uns, mehr
als in diesen Andeutungen die geschichtliche Stellung Haeckels zu
umreißen.
Im Anschluß an Haeckel ist W. P r e y e r (1841-1897) vor allem zu
nennen, als der Vertreter der Entwicklungslehre in der Physiologie.
In zahlreichen gedankenreichen Aufsätzen und Werken ist Preyer für
sie eingestanden und hat ihr gesucht auch auf praktisch wichtige
Fragen Einfluß zu verschaffen. Es sind hier besonders
erwähnenswert: Die Seele des Kindes (1882), Die spezielle
Physiologie des Embryo (1884), Naturforschung und Schule (1887),
worin er im Bunde mit Haeckel der Entwicklungslehre Eingang in die
Schule zu erkämpfen sucht. Eine neue Grundlage für die Systematik
der Physiologie brachte Preyers Einleitung in die allgemeine
Physiologie (1883).
4. Amerikanische Zoologie.
Die amerikanische Zoologie setzt mit Beginn des 19.
Jahrhunderts ein, mit B . S . B a r t o n (1766-1815), der über
Faszination durch die Klapperschlange und über das Opossum
schrieb. 1808-1814 erschien die Ornithologie von A . W i l s o n (1766-
1813), B o n a p a r t e komplettierte 1825-1833 Wilsons Werk.
Gleichzeitig erschien R i c h . H a r l a n s Fauna von Amerika und J . D .
G o d m a n s Werk über nordamerikanische Säugetiere (1826-1828).
1847 tritt die Smithsonian Institution in Tätigkeit und damit beginnen
fortgesetzte zoologisch-systematische Studien. 1846 begründet L .
Agassiz das Studium der vergleichenden Anatomie und
Entwicklungsgeschichte nach europäischem Muster in Cambridge
Mass. Neue Impulse gehen sodann von Darwins Werken aus,
insbesondere tritt der hoch begabte und vielseitige E . D . C o p e
(1840-1897) an die Spitze der amerikanischen
Entwicklungstheoretiker und Paläontologen. Der wesentliche
Bestand der amerikanischen Zoologie gehört der unmittelbaren
Gegenwart an und hat eine Ausdehnung angenommen, die für die
positivistisch zersetzte Wissenschaft Europas eine gefährliche und
ebenbürtige Konkurrenz bedeutet.
IX. Zoographie nach der Mitte des
18. Jahrhunderts.
Abbe, E. 121.
Agassiz, L. 98, 113, 143, 149, 152.
Albert von Bollstädt, der Große 45.
Albert I. von Monaco 150.
Albinus, B. S. 78.
Alderotto 46.
Aldrovandi, U. 20, 52, 53, 66.
Alexander von Myndos 33.
Älian 38, 48, 49, 50.
Alkmäon von Kroton 16.
Amman 77.
Anaximander 15.
Anaxagoras 16.
Antigonos von Karystos 33.
Aristophanes 17.
Aristophanes von Byzanz 33.
Aristoteles 20–32, 33, 34, 36, 37, 44, 45, 47, 49, 51, 57, 61,
68, 71, 76, 91, 94, 96, 152.
Artedi, P. 74, 75.
Aselli 59.
Aßmann F. W. 152.
Asurnasirabal 11.
Aubert 152.
Audouin, J. V. 97.
Augustinus 41, 42.
Autenrieth 108.
Averrhoës 28.
Avicenna 43.
Azara 149.
Caldesi 61.
Campanella 57.
Camper, P. 79.
Carpenter, B. 134, 150.
Carus, C. G. 107, 108, 116.
Carus, J. V. 114, 115, 152, 153.
Cäsalpin 55.
Casserio 56.
Castelnau 149.
Cavolini 101.
Cetti 101.
Chiaje, delle 101.
Chun 150.
Claus 115.
Clift, W. 80, 125.
Clusius von Arras 55, 66.
Coiter 57.
Collins 62.
Colombo 56.
Comte, Ach. 99.
Cope, E. D. 143.
Costa, O. G. 101.
Cuvier, Fr. 91, 96, 99.
Cuvier, Georges 22, 37, 51, 88, 89, 90–96, 93, 94, 95, 96,
97, 98, 99, 112, 113, 116, 124, 125, 126, 127.
Cysat 70.
Edrisi 43.
Ehrenberg, C. G. 113, 149.
Eimer, Th. 139.
Elucidarius 47.
Empedokles 16.
Epicharm 17.
Erasistratos 39.
Eustachius 57.
Eydoux 148.
Fabricius ab Aquapendente 57.
Fay, du 82.
Field, H. 153.
Fitzroy 130.
Flower, W. 127.
Forbes, Ed. 149.
Frantzius 152.
Franz von Assisi 43.
Friedrich II. von Hohenstaufen 44, 46, 52.
Fuchs, L. 47.
Fulvius, Hirpinus 37.
Furlanus 48.
Galenos 15, 18, 39, 40, 47, 49, 50, 51, 57.
Galilei 58.
Garnot 148.
Gaymard 148.
Gaza 48.
Gegenbaur, K. 117, 118.
Geoffroy St. Hilaire, Etienne 88–90, 91, 92, 95, 96, 99, 104,
112, 122, 125, 126, 133, 134.
Geoffroy St. Hilaire, Isidore 100, 101, 117, 151.
Gesner, K. 20, 51, 52, 53, 152.
Giovio 47.
Gmelin 77.
Godmann 142.
Goldfuß 116.
Goethe 104, 105, 108, 125.
Gould, John 149.
Grant 146.
Gray, Asa 134.
Gray, J. E. 126, 127.
Grew, N. 61.
Gronovius 49, 74.
Güldenstadt 77.
Günther, A. 127, 152.
Gyllius 48.
Ingrassias 57.
Isidor von Sevilla 42.