Pointers and their Application.
Moderator: Coders of Rage
Pointers and their Application.
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.
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"
Dad, "Yea well I have a fan belt in street fighting"
1.1 - abstract
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.
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"
Dad, "Yea well I have a fan belt in street fighting"
1.2 - stack and heap memory
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
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"
Dad, "Yea well I have a fan belt in street fighting"
1.3 - terminology
1.3 - terminology
i am leaving this black. i think this will be redundant. and unnecessary.
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"
Dad, "Yea well I have a fan belt in street fighting"
1.4 - declaring and casting pointers
1.4 - declaring and casting pointers
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.
very important : if you planning on making code portable, try to avoid casting pointers. different infrastructures in OS handles cast differently.
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.
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)
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
1.5 - dereferencing pointers and address operator
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.
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;
in this example *p is exactly like a int variable
Code: Select all
int i;
&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"
Dad, "Yea well I have a fan belt in street fighting"
1.6 - pointer assignments
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).
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 following is correct at compile time, but not run-time.
Code: Select all
*iPtr = *jPtr;
1. Never use uninitialized pointers.
2. To increment a dereferenced pointer, use
Code: Select all
(*p)++
Code: Select all
*p++
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
1.7 - qualified pointers
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.
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;
Code: Select all
const int *const p;
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;
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"
Dad, "Yea well I have a fan belt in street fighting"
1.8 - generic and null pointers
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
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.
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*
Example
consider the following declarations
Code: Select all
void *p;
char c = 'c';
char *cp = &c;
Code: Select all
p = cp;
Code: Select all
some_function(*p);
Code: Select all
some_function(*(char*)p)
Code: Select all
(void*)0
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"
Dad, "Yea well I have a fan belt in street fighting"
- ansatsusha_gouki
- Chaos Rift Newbie
- Posts: 37
- Joined: Fri Jul 04, 2008 1:19 am
- Location: Cleveland
- Contact:
Re: Pointers and their Application.
i hate pointers......still dont understand the concept for it
- ultimatedragoon69
- 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.
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.
just hang on. im gonna get to the point where you will see.ansatsusha_gouki wrote:i hate pointers......still dont understand the concept for it
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: Pointers and their Application.
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.ansatsusha_gouki wrote:i hate pointers......still dont understand the concept for it :roll:
(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.
1.9 - conversions
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!
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;
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"
Dad, "Yea well I have a fan belt in street fighting"
1.10 STD type defs
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.
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"
Dad, "Yea well I have a fan belt in street fighting"