Pointers and their Application.

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Pointers and their Application.

Post by avansc »

Disclaimer : Let me start by saying im not a educator, i taught some C and Java classes in college, but by no means should you take what i say yo be the most correct thing. "also i'll try to use caps in I and start of sentence but don't guarantee it, my grammar will be somewhat decent", also i am working with a book, so i do not take credit of any of this, just taking the important parts and putting it in term that are more easy to understand.

This will have multi-posts that build on one and other and hope that you wont get confused. shoot, i hope i dont get confused.

1.1 - abstract
1.2 - stack and heap memory
1.3 - terminology
1.4 - declaring and casting pointers
1.5 - dereferencing pointers and address operator
1.6 - pointer assignments
1.7 - qualified pointers
1.8 - generic and null pointers
1.9 - conversions
1.10 STD type defs
1.11 - heap managment
---1.11.1 - allocation
---1.11.2 - deallocation
1.12 - Pointer Arithmatic
---1.12.1 - the sum of a pointer and an integer
---1.12.2 - Difference of a pointer and an integer
---1.12.3 - pointer comparison : <, <=, > ,>= <==, !=
---1.12.4 - pointer subtraction

more will be added.
Last edited by avansc on Fri Nov 07, 2008 9:32 pm, edited 1 time in total.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.1 - abstract

Post by avansc »

1.1 - abstract

Pointers are at the heart of any C/C++ Program, and will be a fundamental concept that you will have to master
to become a proficient programmer.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.2 - stack and heap memory

Post by avansc »

1.2 - stack and heap memory

a program can topically get memory from one of two places, the run-time stack, and the heap. dynamic memory management means
using memory that is not allocated on the run-time stack.

1. stack based menory : implicitly manages by the function calls and function returns. the programmer can not explicitly invoke opperations that would result in the alloc or dealloc of the blocks of memory on the stack. thus an advantage of stack based memory is that you do not have to worry about deallocation for that memory. however you can face the dangling pointer problem with this, which we will cover later. basically it means referencing a block of memory that was deallocated = program crash most of the time.

(some of this might sound strange but will become clear as we go on, im just mentioning it so you have it in the back of your head.)
2. heap based memory : managed by programmer, you have more power, more responsibility, and more chan ge of breaking things in essence. if the heap gets fragmented you are also in trouble.

C/C++ programmers are responsible for memory management! a serious problem is called memory leakage. which happens when the programmer forgets to deallocate memory.

basic defenition : a pointer is a variable whose value is a memory address representing the location of a chunk of memory located either on the stack or the heap
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.3 - terminology

Post by avansc »

1.3 - terminology

i am leaving this black. i think this will be redundant. and unnecessary.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.4 - declaring and casting pointers

Post by avansc »

1.4 - declaring and casting pointers

Code: Select all

int *p;      // pointer to int
double *q  // pointer to a double
char *s     // pointer to a character, essensially a string as long as its null terminating.
def : "a pointer to int" is the same as saying "an int pointer"

p points to an object of size sizeof(int) "4 bytes on most compilers"
q points to an object of size sizeof(double)

the type of a pointer is specified during declaration, but can be changed using a cast.

Code: Select all

(int*)q  // type int, that points to size sizeof(int)
very important : if you planning on making code portable, try to avoid casting pointers. different infrastructures in OS handles cast differently.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.5 - dereferencing pointers and address operator

Post by avansc »

1.5 - dereferencing pointers and address operator

Once you have a pointer, you may want to start using its contents of the memory location pointer to by it. this is called dereferencing.

Code: Select all

int *p;
p is an int pointer, and *p is the contents of the memory object that p points to.
in this example *p is exactly like a int variable

Code: Select all

int i;
i is an int variable.
&i is like in int pointer, pointing to the variable i.

& is called the address operator and can only be applied to l-values, and it can not be used with register variables.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.6 - pointer assignments

Post by avansc »

1.6 - pointer assignments

okay, some information that might help when you get compile time errors.
have you ever ran into a error at compile time and the error is something like left-hand value bla bla bla..

pointer assignments are the same as any other, they have to be correct at compile and run time.
Compile time correctness means the right hand side and left hand side of the assignments have to be pointers to compatible types.
for portability, compatible usually means identical. run-time correctness mean that you are using the pointer "correctly"; for example
you are not using a pointer that has not been initialized

the following is not correct at compile time

Code: Select all

i = &j;
jPrt = &i;
the problem is that the types on the left and right hand side are not compatible.

the following is correct at compile time, but not run-time.

Code: Select all

*iPtr = *jPtr;
STOP

1. Never use uninitialized pointers.
2. To increment a dereferenced pointer, use

Code: Select all

(*p)++
rather than

Code: Select all

*p++
which means: first dereference a pointer, and then increment this pointer(and not its value).
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.7 - qualified pointers

Post by avansc »

1.7 - qualified pointers

a const qualifier applied to a pointer can mean on of three things:

Code: Select all

const int *p;
.
pointer to a constant integer, the value if p may change, but the value of *p cannot.

Code: Select all

int *const p;
constant pointer to integer; the value of *p can change. but the value of p cannot.

Code: Select all

const int *const p;
constant pointer to constant integer.

there is an alternative syntax for a pointer to constant data; the data type and the qualifier const may appear in different order such as :

Code: Select all

int *const p;
but i will use the first alternative.
pointers can also be qualified as volatile, mainly to deal with problems encountered with real-time systems.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.8 - generic and null pointers

Post by avansc »

1.8 - generic and null pointers

this is where things start to become a little more fun.
conisider an application where you want to be a reference to "any" kind of object.
people that are familiar with java know about this. the Object type. in C there is a special type of pointer
called

Code: Select all

void*
this can safely be assigned to any type of pointer.

Example

consider the following declarations

Code: Select all

void *p;
char c = 'c';
char *cp = &c;
it is legal to assign a generic pointer to a typed pointer

Code: Select all

p = cp;
but you still can not dereference it(use *p); for example

Code: Select all

some_function(*p);
is illegal. in order to dereference a generic pointer, you have to use a cast to indicate the type of value it is pointing to, for example.

Code: Select all

some_function(*(char*)p)
i will now describe the NULL macro. this is a special "zero" value that can be assigned to any pointer, no matter what its type, NULL is defined as

Code: Select all

(void*)0
in stddef.h and some other like stdlib.h, the NULL value is usually ised to initialized pointers to a well-defined "zero" value.

STOP
programs that assume a particular architecture such as little or big-endian are not portable.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
ansatsusha_gouki
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 37
Joined: Fri Jul 04, 2008 1:19 am
Location: Cleveland
Contact:

Re: Pointers and their Application.

Post by ansatsusha_gouki »

i hate pointers......still dont understand the concept for it :roll:
User avatar
ultimatedragoon69
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Tue Oct 28, 2008 1:57 pm
Current Project: Pangea's quest (text ~tile~ based rpg)
Favorite Gaming Platforms: Dreamcast, PC, playstation 1, Virtual Boy, Snes
Programming Language of Choice: c++
Contact:

Re: Pointers and their Application.

Post by ultimatedragoon69 »

honestly i think the best way to learn pointers is the assembly programming, I learned it that way and it gave me a great understanding into it. I won't say i'm good or anything with using them just saying i understand it.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Pointers and their Application.

Post by avansc »

ansatsusha_gouki wrote:i hate pointers......still dont understand the concept for it :roll:
just hang on. im gonna get to the point where you will see.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Pointers and their Application.

Post by MarauderIIC »

ansatsusha_gouki wrote:i hate pointers......still dont understand the concept for it :roll:
A pointer is a variable that stores a memory address (of another variable, usually) enabling you to directly access that memory location. Since all variables are stored in memory, editing memory at the right address changes the value of the variable.
(Cracking works this way, by directly editing memory.)

Some uses: Modify original variable inside a function -- you define a pointer to a variable as a function parameter.
Iterate through a collection of variables -- using the "++" operator on a pointer will move it through memory according to the size of the variable type it is defined as pointing to. So if you have a bunch of stuff stored in memory in one big chunk, like happens with arrays and vectors, you can
myPointer++
(*myPointer) = someNewData;

Also you may want to consider talking about auto_ptr
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.9 - conversions

Post by avansc »

1.9 - conversions

in practice this should be avoided at all costs, im not going to give this a big section. i almost omitted it.

storage alignment means that storage units must begin on cetain addressing boundries. for example, on a byte-oriented machine, a 16-bit work may have to start on a multiple of bytes, such as 4( for examlple, adress 100, 104, 108 etc.). in order to satisfy this requirment, compilers insert pad bytes. storage alignment is one of the reasons for the lack of portability. any data type has an alignment modulus. any address of a specific type must be a multiple of its aligment mudulus. no, consider the following.

Code: Select all

char *pc;
int  *pi;
and assume that pc is initialized in some manner to 1001. given this information, what is the value of (int*)pc?

well 1001 has to be a multiple of 4 (if int is of size 4, which it usually is), so it would have to be scaled up to 1004 or down to 1000.

im not gonna go further. just try and avoid this!
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

1.10 STD type defs

Post by avansc »

1.10 STD type defs

when using pointers allways use sizeof() when in combination of malloc

C provides a header file stddef.h that defines several universal data types.

using malloc(sizeof( . . . )) is save and will allways be portable.
using malloc(v) where v is type long is not.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply