Follow us on Twitter!
Ideas are far more powerful than guns.
Friday, April 18, 2014
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Members Online
Total Online: 26
Guests Online: 25
Members Online: 1

Registered Members: 82828
Newest Member: uberscon
Latest Articles

Advanced C++ Pointer(1)

Arrow Image arguably most difficult concept in C++ or Object Oriented programming. Need basic understanding of C++ along with Full understanding of array.

Advance C++ concept,
by : Alka G Vulcan
yake_2002 at hotmail dot com
<!--Pre requisite --!>
Basic understanding of C++ along with Full understanding of array.
Common sense

I wasnít able to find article about pointer in forum, and session over MSN seems not organized method to explain such concept to few friends of mine, so I figure I would submit article about it.

Pointer is arguably toughest concept in C++ to understand and utilize at its full capacity, along with polymorphisms depends on oneís opinion. If you are planning on going to software related field, itís inevitable to learn pointer.
Pointer variable, often called pointer, is a variable that holds addresses, and by through that address you can indirectly manipulate data stored in other variables.

Pointer facts
+ working directly with memory locations that regular variables donít give access to
+working with strings and array
+creating new variables in memory while program is running
+creating arbitrarily-sized lists of values in memory
+pointer holds only one thing, address

DECLARATION of int and double pointer is as follow

int *iptr;
double* dptr;

/* As you can see the spacing between asterisks and variable name doesnít really matter.
Also itís critical to understand variable iptr does not mean that iptr is and integer variable, it means that iptr CAN hold address of an integer variable. */

int *iptr; //declaring pointer
int x = 10; //declaring a variable with a value
iptr = &x; //assigning variableís ADDRESS to a pointer

In order to assign value to pointer, we would have to approach indirect method. First we declare pointer and a variable with value and assign variableís address to pointer, since pointer can only have address.

As you can see we can dereference variable by using Ď&í sign.

/* you can see the address of variable by ďcout << &x; ď. Depends on your compiler or operating system it will show hex decimal or else. */

So after following code if we run this codeÖ.
cout << iptr; // will output something like 0x12345 or whatever
cout << &x; // will output same as above
both will output same address.

In order to see what value is in pointerís address by referenceÖ.

cout << *iiptr; // will show 10 since 10 is in xís address
cout << x; //will output same as above
both will output same value.

/* As you can see asterisks have many job in C++, multiplication, pointer and possibly many more depends on operator over loading. So itís critical to organize your code to not to confuse yourself.*/

RELATION with array
It turns out array and pointer is very similar, in fact array is nothing but a variable that holds address like pointer as well.

For exampleÖ..
int array[] = {10, 20, 30, 40, 50};
int *ptr;
ptr = array; //notice we donít have to dereference array by &, because array is address.

cout << array; // will show address of begging of array, may vary depends on compiler.
cout << ptr; //output same as above.

cout << *array; //will show first value in array, notice Iím using reference, ď*Ē, just like a pointer.
cout << *ptr //will output first value in array just like above.
cout << ptr[0] //this also will output first value in array, notice Iím indirectly accessing array through pointer.

// Also in side note itís good to know pointerís way to access objectís member
cout << *(ptr +1) // will output first element of array
cout << *(ptr + 2) //will output second element of array

/* Pointer work with arrayís method of ď[]Ē, and array work with pointerís method ď *(ptr +1)Ē way. */

I donít know how familiar you guys are with arrays and its arithmetic. But pointerís arithmetic is quite similar to arrayís arithmetic

For example
int array[] = {1, 2, 3, 4, 5};
int *ptr;
ptr = array;
//1. following is array arithmetic and will output 5 elements in array.
For(int x = 0; x < 5; x++)
cout << array[x] << endl;
//end of array arithmetic

// 2. pointer arithmetic and will output 5 elements in array. This also will output all 5 elements
for(int x = 0; x< 5 ; x++)
cout << *(ptr + x) << endl;

// 3. following is also result same output as above
for(int x = 0; x < 5; x++)
cout << *ptr << endl;
ptr++; //notice incrementing pointerís address.
//end of pointer arithmetic

/* becareful using option number 3 because as you can see ptr got incremented 5 times and never got back to original place, there for after option number 3ís execution, if you output ďcout << ptr; ď you will access 6 th member of array which we didnít assign. And yes C++ will compile that no problem with no warning. Thus you may accidently alter or change variable that happens to located next of array.*/

RELATION with function
Now passing pointer to function is quite simple.

Void doubleValue(int *val) // * before val indicating itís a pointer
*val *= 2; // first * for referencing pointer and second * for multiplication

/* notice valís value will change throughout entire program because whatís in address changed. Unlike normal variable when they passed to following function

Void doubleValue(int val) // val in here is variable not a pointer
val *= 2; // first * for referencing pointer and second * for multiplication

val wonít be double its original size in ďmain()Ē. Because COPY of variableís value get passed to function and that copy gets manipulated not the original value. We can fix It by following code.

Void double value(int &val) // val in here is variable not a pointer
val *= 2; // first * for referencing pointer and second * for multiplication

just remember to add & before variable name in argument on function call */

Now returning pointer from function can be trickyÖ.


Dynamic memory allocation is nothing but reserving memory for future use, just like array. And itís most common use is in case of returning pointer from a function.

For example, execution of following code is almost identical to ďint array[5];ĒÖ.
int *start () //notice * before name, it indicates that itís returning a pointer
int *ptr;
ptr = new int [5]; //allocation of memory by using ďnewĒ key word
for(int x = 0; x <5; x ++) //fill every object with 0
ptr[x] = 0;
return ptr; //return pointer

/* if you return pointer without memory allocation, data will be lost. Remember pointer is an address. And variable declared inside of function will be deleted as function exit. There for without allocation data will be lost while with allocation data will be save in safe location.
Although dynamic memory allocation may seem to protect you from buffer overflow or boundary overflow, it wonít prevent from such accident. Memory allocations are only for reserving memory for a pointer.*/

Deallocation is freeing the reserved memory by allocation. If you donít deallocate the memory your computer may never get to use that memory again because it thinks itís reserved. But now days most operating system automatically detects and deallocate such memory but still, it's imperative to develop good habit.

ptr = new int [5];
delete [] ptr; //deallocation, notice I didnít put number in bracket. No matter the number of object allocation you will be able to delete all by empty bracket.

/* Itís critical that you only deallocate what is allocated before. Otherwise either your program will not function as you expected or wonít compile at all. */

I understand Iím not the best teacher and this isnít the best way to illustrate. My purpose of this article is not to give you 100% understanding of pointer, but give you basic concept and direction to practice pointer. Because I believe practice is the only method to fully understand pointer, just mass with pointers and you will understand.
There are several stuff that I didnít mentioned, such as -> operator instead of dot operator, and others. I may write another article later if response is good, or you cans ask me on my email at very beginning.
Any questions contact me by email at above, and plead donít ask me if I can help you hack your friendís computer or whatever.

p.s. no tab thingy kills me!!!! :P


Skunkfooton April 15 2008 - 15:53:34
Awesome! I just started learning about pointers in class, but was having a little trouble understanding them (mainly because I can't understand half of what my teacher says because of his accent Sad) Anyway, now I understand it much better, thanks Smile
Uber0non April 15 2008 - 19:05:36
Not very advanced, I mean this is just the basics of working with pointers... Well written though. I'm sure this will help beginners understanding pointers better :happy:
alkaon April 15 2008 - 20:08:55
@Uber0n ya... i agree, i started write and it got to long so i kinda stopped after basic.... but pointer it self is a advance concept there for i slapped the title of "advance" I will write more about pointer if responses are good.... Grin
alkaon April 15 2008 - 20:10:11
oh ya... there is sudoku solving C++ program code i submitted and that may help understand pointer as well....
Uber0non April 18 2008 - 19:09:18
alka said: "pointer it self is a advance concept" Yes that's true Smile
deletedon May 29 2008 - 13:06:09
pointers ftw! :ninja:
aseembleron June 16 2008 - 13:06:20
Ouch. My eyes are realy hurting. To be honest, it is a good text but the many small eroors and stupid use of different code comment syntaxes makes this completely unreadable. Sorry.
Post Comment


You must have completed the challenge Basic 1 and have 100 points or more, to be able to post.