Help Understanding Pointer Math
Moderator: Coders of Rage
- davidthefat
- Chaos Rift Maniac
- Posts: 529
- Joined: Mon Nov 10, 2008 3:51 pm
- Current Project: Fully Autonomous Robot
- Favorite Gaming Platforms: PS3
- Programming Language of Choice: C++
- Location: California
- Contact:
Help Understanding Pointer Math
What is the most common use for pointer math? I understand pointers and stuff, but I see no real applications when it comes to pointer math, except in arrays. In an array, you might want to add, remove and move some data around in that array, so using pointer math, its achieved. But thats the only thing I can think of... I can't ask my AP Comp Sci teacher this, since he didn't use C++ in ages I bet (we learn Java) so any one know?
Re: Help Understanding Pointer Math
i guess you mean pointer arithmetic.
well there are many things you can do really.
note: just make sure you understand the precedence of operators. for instance what *p + 5 vs *(p+5)
for instance this would be a simple example for finding the string length of a c string, ie, char pointer.
int string_length(const char *str)
{
if(!str) return 0;
char *temp = (char*)str;
while(*(++temp));
return temp - str;
}
most of the time pointer arithmetic is used to do some form of search, or comparison based on locality of data.
ps: you can do pointer arithmetic in java, look into the Unsafe class, i think its something like sun.misc.Unsafe. you basically get direct mem access.
I dont know much about it, but im sure its not recomended, you'll prolly crash the jvm if you screw up.
well there are many things you can do really.
note: just make sure you understand the precedence of operators. for instance what *p + 5 vs *(p+5)
for instance this would be a simple example for finding the string length of a c string, ie, char pointer.
int string_length(const char *str)
{
if(!str) return 0;
char *temp = (char*)str;
while(*(++temp));
return temp - str;
}
most of the time pointer arithmetic is used to do some form of search, or comparison based on locality of data.
ps: you can do pointer arithmetic in java, look into the Unsafe class, i think its something like sun.misc.Unsafe. you basically get direct mem access.
I dont know much about it, but im sure its not recomended, you'll prolly crash the jvm if you screw up.
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"
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: Help Understanding Pointer Math
Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Re: Help Understanding Pointer Math
Lol, PiP you should sit there after class and just go on about the wonderful things you've done in c++ and how you have so much control over your programs, and how it doesn't need a VM to run.
Re: Help Understanding Pointer Math
The day one of my lecturer's bad mouthed C++ was the day I lost all hope for the education system.programmerinprogress wrote:Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
- GroundUpEngine
- Chaos Rift Devotee
- Posts: 835
- Joined: Sun Nov 08, 2009 2:01 pm
- Current Project: mixture
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++
- Location: UK
Re: Help Understanding Pointer Math
/signedGenesis wrote:The day one of my lecturer's bad mouthed C++ was the day I lost all hope for the education system.programmerinprogress wrote:Never ask a java teacher a C++ question, especially a one to do with pointers, they'll segfault on you
...And I say this from experience, one of my lecturers hates C++, slags it off all the time in lectures, and I'm thinking to myself "you're java's bitch"
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: Help Understanding Pointer Math
Pointer arithmetic is used for some kind of array iteration 99% of the time. There isn't any real magic going on.
Keep this in mind. When I have an array like so:
And I set a pointer equal to the memory address of the first object in the array:
Now incrementing the pointer like so:
Has what effect on the pointer? The memory address of iterator is now equal to object[1] (the second element in the array), NOT the next memory address. Pointer arithmetic causes the pointer to move around in increments of the size of whatever object you are pointing to.
Keep this in mind. When I have an array like so:
Code: Select all
Object *object = new Object[5];
Code: Select all
Object *iterator = object;
Code: Select all
iterator++;