MESSAGE
DATE | 2008-07-03 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop 2.3
|
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__ U __cxa_atexit-at--at-GLIBC_2.1.3 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 create them.
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.
int (*pt)[10];
This declares a pointer (pt) to an array of 10 integers.
char (*p)[10][100]
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[100] = 'hello world', wd2[100] = ' ';
spt = strtok(wd1,wd2); printf(“%s\nâ€, spt);
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.
delete pt;
deletes the anonymous pointer pt.
int *pt = new int[100];
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.
class IntArray;
once we make this declaration, we can declare objects of this class as we could with any other data type
IntArray array_object;
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.
class IntArray{ public: 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;
private: //nothing yet }
All this is so that later we can use the IntArray as a data type
IntArray myarray;
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
|
|