Page 1 of 2
Pointers and their Application.
Posted: Mon Nov 03, 2008 2:05 pm
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.
1.1 - abstract
Posted: Mon Nov 03, 2008 2:07 pm
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.
1.2 - stack and heap memory
Posted: Mon Nov 03, 2008 2:17 pm
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
1.3 - terminology
Posted: Mon Nov 03, 2008 2:23 pm
by avansc
1.3 - terminology
i am leaving this black. i think this will be redundant. and unnecessary.
1.4 - declaring and casting pointers
Posted: Mon Nov 03, 2008 2:35 pm
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.
1.5 - dereferencing pointers and address operator
Posted: Mon Nov 03, 2008 2:52 pm
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.
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
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.
1.6 - pointer assignments
Posted: Mon Nov 03, 2008 8:58 pm
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
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.
STOP
1. Never use uninitialized pointers.
2. To increment a dereferenced pointer, use
rather than
which means: first dereference a pointer, and then increment this pointer(and not its value).
1.7 - qualified pointers
Posted: Mon Nov 03, 2008 9:06 pm
by avansc
1.7 - qualified pointers
a const qualifier applied to a pointer can mean on of three things:
.
pointer to a constant integer, the value if p may change, but the value of *p cannot.
constant pointer to integer; the value of *p can change. but the value of p cannot.
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 :
but i will use the first alternative.
pointers can also be qualified as
volatile, mainly to deal with problems encountered with real-time systems.
1.8 - generic and null pointers
Posted: Mon Nov 03, 2008 9:17 pm
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
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
but you still can not dereference it(use *p); for example
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.
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
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.
Re: Pointers and their Application.
Posted: Tue Nov 04, 2008 12:35 am
by ansatsusha_gouki
i hate pointers......still dont understand the concept for it
Re: Pointers and their Application.
Posted: Tue Nov 04, 2008 1:36 am
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.
Re: Pointers and their Application.
Posted: Tue Nov 04, 2008 8:27 am
by avansc
ansatsusha_gouki wrote:i hate pointers......still dont understand the concept for it
just hang on. im gonna get to the point where you will see.
Re: Pointers and their Application.
Posted: Tue Nov 04, 2008 10:45 am
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
1.9 - conversions
Posted: Wed Nov 05, 2008 9:21 am
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.
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!
1.10 STD type defs
Posted: Wed Nov 05, 2008 9:24 am
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.