#Understanding Pointer Arithmetic

1 messages Β· Page 1 of 1 (latest)

fading tundra
#

I don't really get Pointer Arithmetic that much could someone explain it to me or give me some recources?

floral wagonBOT
#

<@&987246584574140416> please have a look, thanks.

#

Here is an AI assisted attempt to answer your question πŸ€–. Maybe it helps! In any case, a human is on the way πŸ‘. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#
TJ-Bot
I don't really get Pointer Arithmetic that much could someone explain it to me or give me some recources?

Pointer arithmetic in languages like C or C++ involves performing arithmetic operations (addition, subtraction) on pointers. A pointer holds a memory address, and when you add or subtract an integer from a pointer, it moves to the memory address of the next (or previous) element based on the size of the type the pointer references.

For example, if a pointer points to an int (usually 4 bytes), adding 1 to the pointer will move it forward by 4 bytes to the next int in memory. Similarly, subtracting 1 moves it backward by that size. This is useful when iterating over arrays using pointers.

Key points:

  • Pointer arithmetic respects the size of the data type pointed to.
  • Adding 1 to a pointer moves it to the next element, not the next byte.
  • You can subtract pointers to find the number of elements between them.
  • Pointer arithmetic is undefined if pointers don’t belong to the same array or object.

For deeper understanding, you can check these resources:

Useful links:

These explain concepts clearly and provide code examples which can help solidify your understanding.

crude oak
#

This is definitely not a Java question, but in short, it's nothing more than doing math with pointers

#

Usually nothing more than adding an offset, or a size multiplied by a count

crude oak
#

Or finding the size of an item by subtracting pointers etc

#

Nothing more to it

fading tundra
crude oak
#

I guess with memorysegments in Java you can do some actual pointer arithmetic

fading tundra
#

How does pointer arithmetic handle struct types tho

crude oak
#

well, fields have offsets

#

structs have a size

#

If you want a field at offset 40, you do "pointer to start of struct + 40"

crude oak
#

your compiler/runtime does this for you all the time πŸ™‚

#

It's how you can also reverse engineer structs etc

fading tundra
#

does something happens when i subtract two pointers?

crude oak
#

yeah, you get a difference between two pointers

#

pointers are just numbers

fading tundra
crude oak
#

So you can do with them what you want, but some operations don't make sense and some do

fading tundra
#

hm ok

crude oak
#

dividing pointers makes zero fucking sense though πŸ˜„

#

Now doing (p2 - p1) / n kinda does, but then you're not dividing actual pointers

#

plus it works differently depending on the kind of pointer you have

#

if you have a pointer to 32-bit integers, doing pointer++ will move by 4 bytes at a time

spice sorrel
spice sorrel
fading tundra
#

can we do pointer arithmetic with java? no?
we cant add or subtract right?
only compare and dereference?

spice sorrel
#

you do not have pointers in java. that the jvm might implement the references with pointers does not matter

crude oak
#

The closest you can get is maybe memorysegments

fading tundra
crude oak
#

Not about the tag, about the fact that you asked that again lol

fading tundra
crude oak
#

Hence why I said that this is definitely not a Java question

#

Implying that there's no pointers in Java

bitter token
#

I mean. Sure let's do some pointer arithmetic in Java

#
String[] names = { ... };
int i = 0;
IO.println(names[i]);
i++;
IO.println(names[i]);
#

Here the int I is a "logical pointer" into the array of strings

#

So you can add 1 to that pointer and get to the next string

#

C pointers are similar, but it's as if you are looking into a byte[] that summons Satan if you go out of bounds

#

And more often than not you are interpreting chunks of bytes as some other structure

#

So really it needs to be i += 24 or whatever the size of the underlying structure is

#

Which then ends up varying from platform to platform and only the compiler knows

#

Hence why you end up with pointers to specific types in C

#

struct Person*

#

If you look at a C pointer as an int it's basically an index into all of RAM

#

(ignoring things like operating system shenanigans around them "virtual memory" - this is part of why people say C isn't a low level language anymore. This abstraction of pointers into chunks of memory doesn't super hold up with modern hardware)

#

So talking about what it means to do pointer arithmetic is a little different when you don't have "raw pointers" like you have in C

#

Java has a class called MemorySegment which lets you work with foreign memory which in turn can be or be made up of pointers like that

exotic fjord
#

The VM also makes it difficult to get access to the memory address of an object. In hotspot derivatives you can use the jvmci exports. In other vms there are other techniques you can use

floral wagonBOT
#

Changed the category to C|C++.

#

<@&987246683568103514> please have a look, thanks.

lean seal
#

since it is fundamentally an integer, basic integer arithmetic applies to it as well

#

so if you have e.g. an array of 8 byte doubles starting at address 0x1234

#

then the second double would be at address 0x123c

#

etc

#

so that you don't have to track the size of the type yourself, languages like C and C++ do that automatically for you

#

so adding an integer to a pointer actually amounts to adding sizeof(thetype) * the_integer to the actual address

#

Usual warning that dereferencing a pointer with an invalid address is undefined behavior and will in all likelihood crash your program instantly, or (far worse) lead to memory corruption and unpredictable program behavior

#

so be very careful with what kind of arithmetic you do on pointers

crude oak
exotic fjord