|FROM ||Ruben Safir
|SUBJECT ||Subject: [NYLXS - HANGOUT] C++ Workshop 2.3
|From lest-hangout-at-mrbrklyn.com Thu Jul 3 20:11:37 2008
Received: from www2.mrbrklyn.com (localhost [127.0.0.1])
by www2.mrbrklyn.com (8.13.1/8.13.1/SuSE Linux 0.7) with ESMTP id m640BYI0015093
for ; Thu, 3 Jul 2008 20:11:36 -0400
Received: (from majordomo-at-localhost)
by www2.mrbrklyn.com (8.13.1/8.13.1/Submit) id m640BYMB015092
for hangout-outgoings; Thu, 3 Jul 2008 20:11:34 -0400
X-Authentication-Warning: www2.mrbrklyn.com: majordomo set sender to lest-hangout-at-nylxs.com using -f
Received: from www2.mrbrklyn.com (localhost [127.0.0.1])
by www2.mrbrklyn.com (8.13.1/8.13.1/SuSE Linux 0.7) with ESMTP id m640BVfo015087
for ; Thu, 3 Jul 2008 20:11:33 -0400
Received: (from ruben-at-localhost)
by www2.mrbrklyn.com (8.13.1/8.13.1/Submit) id m640BVge015086
for hangout-at-www2.mrbrklyn.com; Thu, 3 Jul 2008 20:11:31 -0400
Date: Thu, 3 Jul 2008 20:11:31 -0400
From: Ruben Safir
Subject: [NYLXS - HANGOUT] C++ Workshop 2.3
Content-Type: text/plain; charset=utf-8
2) Pointers and References â€“ Where did I PUT THAT!
When we create data for our program, we ask the program to insert memory
into RAM and to retrieve or assign the data from that memory location
for use. Internally the program keeps track of the symbols and the
memory locations. In fact is you run the program â€œnmâ€ on a C or C++
binary it will tell you all the symbols that it has in that binary.
ruben-at-www2:~/cplus> nm file3|less
0804a210 A __bss_start
08048a84 t call_gmon_start
0804a29c b completed.1
0804a0bc d __CTOR_END__
0804a0b4 d __CTOR_LIST__
0804a204 D __data_start
0804a204 W data_start
08048ed0 t __do_global_ctors_aux
08048ab0 t __do_global_dtors_aux
0804a208 D __dso_handle
But we can also create memory locations that are assignable, and store
a representation of that memory location directly into a variable that
only stores the memory location as data, not the data itself. In c
and C++ this is called pointers and we can use the following syntax to
int *pt = &myint;
This declares the pointer to an int variable called pt which stores the
address for myint. The syntax int * in a declaration (and ONLY in a
declaration) says make a pointer to an int. The & syntax in front of a
variable myint says don't return the value of the variable, but return
the address of the data stored in the variable itself.
There are functions that return only pointer data. Those functions make
it possible to access memory without the declaration of variables at all.
There are also declarations that can be made in C and C++ which can
create variables without variable names either.
This declares a pointer (pt) to an array of 10 integers.
This is a pointer which addresses an array of 10 pointers (implied)
to arrays of 100 chars each. Commonly this is know as a point to an
array of 10 strings. The symbolic variable name for an array often
gets automatically cast as a point type.
Most string functions in C return a char pointer for example
char * strtok(char *s1, const char *s2);
This would return an address of a char, which in theory would represent
an array of chars. In use it would look like this
char * spt;
char wd1 = 'hello world', wd2 = ' ';
spt = strtok(wd1,wd2);
Manual Memory Allocation and the 'new' and delete keywords
C++ makes it very convent to create dynamically allocated memory which
is accessed by pointers. We might call these anonymous pointers because
they do not point to any variables, just defined memory. We do this
with the key word â€œnewâ€.
int *pt = new int(124);
This creates a new int pointer called pt and assigns to the memory
pointed to by pt with the integer value 124.
deletes the anonymous pointer pt.
int *pt = new int;
This declaration creates a new int pointer to an array of 100 integers,
with no data assigned yet to that block of memory.
delete  pt;
deletes the entire array pointed to by pt and then undefines pt.
3) Class Declaration â€“ Type Type Type
When using C++ one of the key reasons for choosing this language involves
our ability to roll our own data types. While with C programming we
can use typedef with unions and structures, the class system with C++ is
much ore extensive and flexible, giving genuine data type ability to the
programmer, a flexibility previously reserved for the elite programmers
who could hack the core C programming interface, probably involving a
bit of assembly to boot.
The first thing you need to do when making a class is to declare it,
similarly to how one needs to declare a function. Declaring the class
alerts the compiler of the new symbols that will be used in further code.
The declaration does not include the definition. that can be done later.
All of this most commonly takes place in an external file so that it
can be reused, or even a .h header file.
once we make this declaration, we can declare objects of this class as
we could with any other data type
or even declare pointers to it such as:
IntArray *pt_array_obj = new IntArray;
After declaring the class itself, we need to now declare the internals
of our class, or its body.
bool operator==(const IntArray&) const; //This is a declaration
bool operator=!(const IntArray&) const; //This is a declaration
bool operator=(const IntArray&); //This is a declaration
int size() const; //This is a declaration
void sort(); //This is a declaration
int min() const; //these are all declarations
int max() const;
int find() const;
All this is so that later we can use the IntArray as a data type
and we can then use the IntArray objects as any other variable
myarray = a;
Then we ca use the
Class Definition dot syntax to gain access to the public interface
myint = myarray.size();
or if we use a pointer as follows
IntArray * parray = new IntArray;
myint = parray->size();
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
Â© Copyright for the Digital Millennium