Introduction to Pointers in C++
Originally written in February 1998. At least one person seems to have found it useful. It’s a little incomplete, because it doesn’t deal with malloc and free, nor with C++ references. If you find this useful, feel free to copy and pass along, with attribution. Thanks!
Basics
All data and code are stored in memory. The location in memory where they are stored is known as the address of that data or code. Usually they are accessed through variable names that represent them, such as counter, printf, etc. We can, however, also access data using its address, rather than a formal name. This is done using pointers, special variables which store the address of data. Following are several annotated examples of simple pointers at work.
int* x; // Declare x, a pointer to an integer. int y; // Declare y, an integer. float* r; // Declare r, a pointer to a float. float s; // Declare s, a float. x = &y; // x gets y's address -- it points to y. r = &s; // r gets s's address -- it points to s.
The next few are a tad trickier. We use the “*” to dereference the pointer. Basically, this means to access whatever it is the pointer is pointing to. You can think of it as counteracting the “*” used in the declaration of the pointer; they neutralize each other, making the result a regular variable.
*x = 15; // Set value pointed to by x -- y -- to 15. cout << *r; // Print value pointed to by r: s.
Complications
If it were that simple, of course, then nobody would have trouble with pointers. The fact of the matter is, however, that there are a number of complications — extensions to the idea of pointer — that can be hard to keep track of.
Pointers as Lists
The first is the idea of pointers being equivalent to lists. This is a crucial idea in C and C++. Essentially, instead of thinking of a pointer as pointing to a single variable, you can think of it as pointing to the first variable in a list of variables. Likewise, a list can be accessed without any subscripts to find the pointer to the first element in the list. It works like this:
int* x; int y[15]; x = new int[8]; // Allocate array of 8 integers. *y = 8; // Set first element of y-list to 8. x[3] = 7; // Set fourth element of x-list to 7.
Note how pointer notation can be used for the list y, and how list notation can be used for the pointer x.
This brings up a similar topic: pointer arithmetic. Since a pointer is a memory address, you might think that adding 1 to a pointer would simply make it point to the next byte of memory. The C compiler, however, is smarter than that; it realizes that you’re adding something to a pointer, you probably want to make it point to the next element of whatever you’re pointing at. So instead of adding whatever you specified to the pointer, it adds that times the size of the object the pointer points to. For example:
int* x = new int[8];
x++; // Add four to x pointer, to
// point at next integer.
x[0] = 5; // This was originally the second
// element in the array.
x--; // Subtract 4 again from pointer.
*(x + 2) = 6; // Set third element (second after the
// first) to 6.
cout << x[1]; // Will now print "5".
cout << x[2]; // Prints "6".
Pointers to Pointers (aka “The Middleman”)
Another pointer curiosity that C throws our way is pointers that point to other pointers. This may seem like a needless feature, but it comes in very handy when you have multidimensional data whose size you don’t know before-hand. You can then use these pointers to pointers to set up an arbitrary-sized multidimensional array.
It works like this: you can think of a pointer to a pointer as being essentially a list of lists. It’s kind of like the words in the dictionary: the first pointer tells you where to find each of the lists for the letters of the alphabet. Each letter is then itself a pointer that forms a list (by pointing to the first element) of all of the words beginning with that letter. If you add another dimension (and make a pointer to a pointer to a pointer), you can have each word also be a list, pointing to the first out of several different meanings for the word.
Here’s how it works in C++. The following program reads in a table of numbers and finds the average for each row and column. The first two numbers it reads in tell how many rows and columns there are. The rest of the numbers are the ones in the table.
#include <iostream.h>
void main(void)
{
int** table; // *Two*-dimensional pointer.
int rows, cols, i, j, sum; // Dimensions of table.
cin >> rows >> cols; // Find out the number of rows & cols.
// What we'd really like to do here is say:
// table = new int[rows][cols];
// Unfortunately, this doesn't work in C++, since it instead tries to set up
// a one-dimensional array like this:
// table = new int[rows * cols];
// And a one-dimensional array (a pointer to integers) is absolutely
// incompatible with a two-dimensional array (a pointer to a pointer to
// integers), so our program will crash. Note that the syntax above will
// work, however, in Java.
table = new (int*)[rows]; // Allocate the rows.
for(i = 0; i < rows; i++)
table[i] = new int[cols]; // Allocate each row's columns.
for(i = 0; i < rows; i++) // Find row sums.
{
sum = 0;
for(j = 0; j < cols; j++)
sum += table[i][j];
cout << "Row sum for row " << i << ": " << sum << endl;
}
for(j = 0; j < cols; j++) // Find column sums.
{
sum = 0;
for(i = 0; i < rows; i++)
sum += table[i][j];
cout << "Col sum for col " << j << ": " << sum << endl;
}
}
Notice how we had to explicitly allocate both dimensions of the array, starting with the first dimension, the rows; and then for each row we allocated its columns. You may be wondering why the outer dimension is allocated using “new (int*)[n]”, while the inner dimension is allocated using “new int[n]”. This is because each dimension but the last is a pointer to the next dimension. The final array dimension is obviously simply a list of integers, so in the inner loop we merely allocate a list of integers. The next dimension up, however, is not a list of integers; it’s a list of lists of integers. As such, each entry in this list will itself be a pointer to a list of integers — the final dimension. Therefore, the code allocating the outside dimension must allocate a list of pointers to integers.
If we had additional dimensions, for each column in each row we would then have to allocate the list of cells, and so forth. Here’s how a three-dimensional array allocation might look like. Assume that x, y, and z are the size of each array dimension. Notice how the outer dimension is now a list of (int**)’s — a list of lists of lists — and the second dimension is now the one that is (int*) — a list of lists — while the final dimension is still a list of integers.
int i, j;
int*** array3d;
array3d = new (int**)[x];
for(i = 0; i < x; i++)
{
array3d[i] = new(int*)[y];
for(j < 0; j < y; j++)
array3d[i][j] = new int[z];
}
Python persistence
I’ve implemented a rudimentary persistent object store in Python. It is implemented as a Python extension module that implements a set of persistent types: strings, integers, floats, lists, and dictionaries. Each of these are backed by the persistent object store, which is implemented using a memory-mapped file.
In addition, using a specially crafted Python base class for Python objects, Python objects may be stored in the object store (as dictionaries) and instantiated out of the object store.
The result is an persistent object graph (the root of which is a persistent dictionary) whose objects and attributes may be manipulated in-place using native Python syntax. Rudimentary locking is provided so that multiple Python threads / processes may concurrently manipulate the object store.
Details
Some aspects of this system are:
- It is a Python extension module written in C and C++.
- It is tested on Linux. It will likely work on *BSD systems, though it is possible that the location of the mapped storage may need to be moved.
- It is implemented in a hierarchical manner:
- A page manager handles the allocation of 4kB pages within a memory-mapped file. It is multi-process safe. It is, in a sense, a glorified sbrk() for a memory-mapped file.
- A heap manager abstracts the page manager’s services to manage the allocation and deallocation of arbitrary-sized storage segments within the memory-mapped file. It is essentially a malloc() and free() for a memory-mapped file. This is also multi-process safe.
- An object manager manages five new base types (persistent int, float, string, list, and dictionary) backed by persistent storage, using the heap manager’s services. It also provides rudimentary locking facilities for concurrency-safeness.
- The persist Python extension uses the object manager’s services to implement persistent types that mimic the equivalent Python types. Additionally, it has the ability to reinstantiate a Python object that was stored as a dictionary (using the appropriate Python base class). The object manager’s locking facilities are made available for application usage.
- Only one file may be mapped at a time (because it is mapped to a fixed logical address).
- It is available for use under the MIT license. Contact me if you are interested in using it.
Examples
Some examples of its use are a simple counter:
import persist root = persist.root() root.lockExcl() try : root['x'] += 1 # Increment counter except : root['x'] = 1 # First pass; initialize print "Content-type: text/html\n" print "<p>You are visitor " + str(root['x']) + " to visit this site!</p>" root.unlock()
and rudimentary objects:
import persist
from pbase import pbase
class person (pbase) :
def __init__(self, name = "", age = 0) :
pbase.__init__(self)
self.name = name
self.age = age
def printAge(self) :
print "<p>" + self.name + " is " + str(self.age) + " years old</p>"
root = persist.root()
root.lockExcl()
if not root.has_key('Joe') : # First time through
root['Joe'] = person('Joe', 27)
if not root.has_key('John') : # First time through
root['John'] = person("John", 29)
# On subsequent passes we will retrieve the objects stored on the first pass.
print "Content-type: text/html\n"
root['Joe'].printAge()
root['John'].printAge()
root.unlock()
The Puritans
Lloyd-Jones, The Puritans: Their Origins and Successors. Carlisle, PA: Banner of Truth, 1987.
These are a series of lectures Lloyd-Jones delivered at the Puritan Studies and Westminster Conferences. This was my first encounter with Lloyd-Jones, and I enjoyed it very much.
The burdens of Lloyd-Jones that stand out in my memory are:
- The stark difference between revival and revivalism, and a need to appeal to God to bring true revival.
- The need to avoid dead academic intellectualism in our study of the Puritans and our pursuit of God.
- The importance of full-bodied faith as opposed to mere intellectual assent to propositions.
- The role of the Holy Spirit in empowering, encouraging, and assuring believers, and the need to earnestly desire that.
- The need to break down barriers between Christians that are over unimportant matters (while upholding and defending those matters that are of vital importance).
- The need for continued fresh analysis and application of God’s word, rather than unthinking adherence to tradition and habit.
- The importance of ”application” in preaching.
Moderation
The devil has driven the pendulum far beyond its proper point of rest; and when he has carried it to the utmost length that he can, and it begins by its own weight to swing back, he probably will set in, and drive it with the utmost fury the other way; and so give us no rest; and if possible prevent our settling in a proper medium. What a poor, blind, weak and miserable creature is man, at his best estate! We are like poor helpless sheep; the devil is too subtle for us. What is our strength! What is our wisdom! How ready are we to go astray! How easily are we drawn aside into innumerable snares, while in the mean time we are bold and confident, and doubt not but we are right and safe! We are foolish sheep in the midst of subtle serpents and cruel wolves, and do not know it. Oh how unfit are we to be left to ourselves! And how much do we stand in need of the wisdom, the power, the condescension, patience, forgiveness, and gentleness of our good Shepherd!
— Jonathan Edwards, as quoted in Jonathan Edwards: A New Biography, p.246
Hosea
Discussion questions on Hosea. This material owes much to Phil Sasser, Mark Dever, and John Piper.
Summary
- Hosea and Gomer as a type for God and Israel. Israel’s odious sin, God’s judgment, and God’s loving restoration.
- God’s restraining love shown in pleading for Israel’s repentance, and prophesying and bringing judgment.
- God’s redeeming love shown in his promise to buy Israel back for himself.
- God’s restoring love shown in his promise to bring healing to Israel.
Our sin
- Phil talked about how the imagery of adultery reminds us how shocking sin is. Does this come as a surprise? Do you easily forget how personally our sins are taken by God?
- Phil. 3:13-14 cautions us to “[forget] what lies behind”. When we contemplate our sin we should not let ourselves become burdened by condemnation; Romans 8:1 comforts the Christian that there is no condemnation thanks to Jesus’s sacrifice.
At the same time, Eph. 2:11-13 calls us to “remember that [we] were separate from Christ” so that we may rejoice at having been brought near. And Paul recalls that he is “the worst of sinners” for the purpose of exulting in the grace and mercy he found in Christ (1 Tim. 1:12-17); he is moved by this consideration to proclaim that “to the King eternal, immortal, invisible, the only God, be honor and glory for ever and ever. Amen.”
- What good can we gain from considering our sin?
- A right notion of our humble position before God, “evangelical humiliation.”
- A right notion of our position relative to others (we have no advantage or merit, and cause only for compassion and not pride).
- A better understanding of the greatness of God’s love and mercy to us.
- What should a right understanding of the weight of our sin provoke in us?
- Humility.
- Contrition and repentance.
- Dever reminds us that Christians must be always confessing sin and repenting of it. Not that God’s mercy is unsure — but our hearts are wandering and need continuous examining.
- Phil reminds us to bring words of repentance, as in Hos. 14:2-3. Do you urgently confess to God your need for his mercy?
- Gratitude and joy.
- Are there any practical ways that you find helpful in cultivating a heart of humility and contrition before God?
God’s mercy and holy love
- God’s holy displeasure with sin is on display in Hosea, but so is his great redeeming love. What great hope and comfort can we take from Hosea?
- God’s love is jealous and holy; he desires to restrain his people from sin.
- We stand in the shadow of the cross; we have received this great promised redemption.
- What of the “healing” promised in 14:4? Sanctification; God has delivered us from bondage to sin! Phil reminded us that sin is no longer our master.
- We have no hope apart from God. But he is a sure hope.
- What should an understanding of God’s great mercy and love provoke in us?
- Phil reminded us that our love and devotion toward God must be pure and not mixed or half-hearted.
- The great price at which we were bought (1 Peter 1:17-19) should inspire much gratefulness and affection.
- We ought to be provoked to a similar self-giving love (1 John 4:10).
Closing
Possibly close with a quotation from Charles Simeon biography, on humility and enjoyment of God’s glory and love. Simeon says:
With this sweet hope of ultimate acceptance with God, I have always enjoyed much cheerfulness before men; but I have at the same time laboured incessantly to cultivate the deepest humiliation before God. . . . There are but two objects that I have ever desired for these forty years to behold; the one is my own vileness; and the other is, the glory of God in the face of Jesus Christ: and I have always thought that they should be viewed together; just as Aaron confessed all the sins of all Israel whilst he put them on the head of the scapegoat. The disease did not keep him from applying to the remedy, nor did the remedy keep him from feeling the disease. By this I seek to be, not only humbled and thankful, but humbled in thankfulness, before my God and Saviour continually.
Piper writes:
the remarkable thing about humiliation and adoration in the heart of Charles Simeon is that they were inseparable. Simeon was utterly unlike most of us today who think that we should get rid once and for all of feelings of vileness and unworthiness as soon as we can. For him, adoration only grew in the freshly plowed soil of humiliation for sin. So he actually labored to know his true sinfulness and his remaining corruption as a Christian.
For the Christian, contemplation of our sin brings not condemnation but tearful joy at the greatness of the mercy shown to us, and the awesome greatness of the holy giver of mercy!
Genesis
Chapter summaries in Genesis.
Genesis 1 – God creates all from nothing, by His spoken word, and all very good; man as image-bearer; man’s mandate.
Genesis 2 – God rests; formation of Adam and Eve; Eden; covenant of life.
Genesis 3 – Man’s temptation, sin, fall, curse, judgment, banishment, and hope.
Genesis 4 – Abel’s offering accepted; Abel’s murder; Cain’s judgment; Cain’s offspring; Seth’s birth.
Genesis 5 – The line of Adam through Seth to Noah and his sons; Enoch translated.
Genesis 6 – Wickedness multiplies; God is grieved and plans to destroy and to save; Noah obeys.
Genesis 7 – The ark is entered and the earth is flooded; every living thing is blotted out.
Genesis 8 – The water subsides, the earth dries, and the ark is exited; Noah worships God; God’s promise.
Genesis 9 – God blesses Noah and establishes covenant with all living creatures; Ham’s sin and Noah’s curse on Canaan.
Genesis 10 – Genealogies of Noah’s sons.
Genesis 11 – Tower built; God confuses language; genealogy from Shem to Abram; Terah journeys from Ur to Canaan but settles in Haran.
Genesis 12 – God’s promise to Abram; journey to Canaan; deceit in Egypt over Sarai.
Genesis 13 – Abram and Lot separate; God promises land to Abram forever.
Genesis 14 – Abram recues Lot, tithes to Melchizedek, and refuses reward from Sodom.
Genesis 15 – God promises innumerable offspring; Abram believes; prophecy of captivity in Egypt; Abram sacrifices.
Genesis 16 – Offspring through Hagar, who flees but returns after God’s instruction and blessing.
Genesis 17 – Covenant to bless and multiply ”Abraham” and ”Sarah”; promise of land; circumcision as sign of covenant.
Genesis 18 – Three men visit; Sarah laughs at prophecy; Abraham intercedes for Sodom and Gomorrah.
Genesis 19 – Sodom and Gomorrah destroyed; Lot rescued; Lot’s incest.
Genesis 20 – Deceit in Negev over Sarah.
Genesis 21 – Isaac is born and circumcised; Hagar is sent out; Abraham covenants with Abimelech.
Genesis 22 – God tests Abraham, provides a sacrificial ram, and promises to bless Abraham.
Genesis 23 – Sarah dies in Hebron; Abraham buys a field and buries her in a cave.
Genesis 24 – God guides Abraham’s servant to find Rebekah as a wife for Isaac.
Genesis 25 – Abraham dies; Ishmael’s offspring; Jacob and Esau born; Esau sells birthright.
Genesis 26 – God blesses and prospers Isaac; deceit over Rebekah; conflict over wells; Esau grieves parents.
Genesis 27 – Jacob steals Esau’s blessing.
Genesis 28 – Jacob journeys to Haran, is blessed by God in a dream, and builds an altar.
Genesis 29 – Jacob serves Laban for Rachel and Leah. Leah is fruitful but Rachel barren.
Genesis 30 – Jacob’s children; Rachel conceives; Jacob schemes for the best of Laban’s flocks.
Genesis 31 – Jacob flees toward Canaan; Laban pursues, searches for idols Rachel stole; Jacob and Laban covenant together.
Genesis 32 – Jacob seeks to appease Esau, and wrestles with God for a blessing; Jacob called Israel.
Genesis 33 – Jacob and Esau meet; Jacob builds an altar in Shechem.
Genesis 34 – Shechem rapes Jacob’s daughter Dinah; Simon and Levi deceive and kill every man in the city.
Genesis 35 – Jacob builds altar at Bethel; Jacob blessed and called Israel; Rachel dies in labor; Isaac dies.
Genesis 36 – The line of Esau; Esau journeys to Edom.
Genesis 37 – Joseph is favored; Joseph’s dreams; Joseph sold into Egyptian slavery.
Genesis 38 – Judah’s sons killed by God; Judah commits adultery.
Genesis 39 – Joseph finds favor with Potiphar, is tested by Potiphar’s wife, is imprisoned, and finds favor with jailer.
Genesis 40 – Joseph interprets Pharaoh’s servants’ dreams.
Genesis 41 – Joseph interprets Pharaoh’s dreams; Joseph exalted and given authority to prepare for famine.
Genesis 42 – Joseph’s brothers visit him; he imprisons Simeon so they will bring Benjamin.
Genesis 43 – Joseph’s brothers bring Benjamin and dine with Joseph.
Genesis 44 – Joseph makes it appear Benjamin stole a cup; Judah pleads to be held as surety.
Genesis 45 – Joseph reveals himself; Pharaoh invites Jacob to Egypt.
Genesis 46 – Jacob and his family travel to Goshen.
Genesis 47 – Pharaoh greets Jacob; Pharaoh accrues money, livestock, and wealth during famine; Jacob to be buried in Canaan.
Genesis 48 – Jacob blesses Manasseh and Ephraim; Ephraim to be greater than his brother.
Genesis 49 – Jacob blesses his sons; Jacob dies and is to be buried with his fathers.
Genesis 50 – Egypt mourns; Joseph buries Jacob; Joseph’s bones to be carried to Canaan; Joseph dies.
Loftness on Work
John Loftness of Covenant Life Church gave a message on work which I listened to recently. Here’s my chicken-scratch notes:
- God gave work to bless us as we serve as His image-bearers. We exercise royal dominion over the earth as God’s representatives.
- “God hides himself at work, waiting for us to find Him there.”
“We sanctify our work by looking for God in our daily responsibilities.”
“Whenever we face a situation that is ‘not good’, we can believe that God is behind it and wants to meet us in it.”
Luther: work is God’s “mask”.
sin — seeking to find good apart from God. God creates lacks, prompts for us to seek Him.
- We sanctify work by embracing it as a place of suffering for God. The cross makes sense of the toil, and sometimes futility, of work. The world is under judgment; the kingdom has come, but not in full. Yet it has come to our hearts. The cross is to inform how we walk — in our own behavior and in our view of others.
Ontological Argument
with one comment
Anselm’s Argument
pp. 87-88. Plantinga, Alvin. God, Freedom, and Evil. Grand Rapids, MI: Eerdmans Publishing Company, 1978.
Objections
Transitivity
Anselm makes the implicit assumption that his definition of greatness is transitive, without demonstrating this to be true. Without transitivity, a set does not necessarily possess a greatest element. Consider, for example, a chess club consisting of three players: A, B, and C; for which A consistently defeats B, B consistently defeats C, and C consistently defeats A. There is in this case no greatest chess player.
Upper Bound
Anselm also assumes that his set under consideration (all entities existent or conceivable) contains its upper bound, without demonstrating this to be true. Without an upper bound, one can always produce an element greater than any other element. Consider, for example, the set of natural numbers: 1, 2, 3, . . ., with the traditional greater-than metric. This set has no greatest element, because it does not contain its upper bound, which is infinity.
Reality as Greater than Thought
Anselm makes the assumption that an entity that exists in reality is implicitly greater than one that exists in thought. Though a plausible assumption, I believe Anselm should spend more time defending it. Consider, for example, the proposition that the greatest entity is a thought about a thought. (Or a thought about a meta-thought, . . . and once again we see the problem of upper bounds.) This, too, is a valid assumption, one that is also quite tempting to assert.
Anselm also fails to adequately define a metric for the differentiation between reality and thought. While he assumes a God in reality is greater than a God in the imagination, would a worm in reality also be greater than a God in the imagination?
Attributes of God
Anselm assumes, without any prior explanation, that the greatest entity must necessarily have those properties which he attributes to God, such as omnipotence and omniscience. This is an entirely arbitrary assumption.
By changing these assumed properties, one can manipulate this argument to say the exact opposite. Consider this reasoning:
Or consider the following “proof”:
Infinite versus Greatest
Anselm also assumes that, for each of the properties he attributes to God, the notion of greatest automatically entails total. His proof claims to demonstrate that God exists, possessing the greatest power, knowledge, etc.; but then he jumps to the conclusion that God possesses exhaustive power, knowledge, etc.
Time Invariance
Anselm’s argument assumes without prior cause that the greatest entity is itself invariant with respect to time, and is never superceded by some other greatest entity over time.
Conclusion
I believe that Anselm has at most succeeded in proving that the universe (as a whole greater than any portion thereof) exists.
I believe that his primary mistake is in trying to subject God to physical reality, rather than realizing that reality and logic are themselves reliant upon God for their existence. He assumes that God exists in reality, whereas reality, in a sense, exists in God. Thus, his argument fails in that it is circular; by assuming God’s existence, he has claimed to prove it. Sadly, though the God of his assumption is the glorious and transcendent God that is the author of reality, the God of his proof is a lesser God, subject to and entirely contained within material reality.
Written by Scott Moonen
May 24, 2004 at 5:18 pm
Posted in Commentary, Essays
Tagged with Anselm, ontological argument