#Understanding Pointer Arithmetic
1 messages Β· Page 1 of 1 (latest)
<@&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>.
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:
- GeeksforGeeks: Pointer Arithmetic Explained
- TutorialsPoint: Pointer Arithmetic in C
- cplusplus.com: Pointer Arithmetic
These explain concepts clearly and provide code examples which can help solidify your understanding.
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
i selected the wrong tag
nothing more?
I guess with memorysegments in Java you can do some actual pointer arithmetic
How does pointer arithmetic handle struct types tho
well, fields have offsets
structs have a size
If you want a field at offset 40, you do "pointer to start of struct + 40"
hm i see
your compiler/runtime does this for you all the time π
It's how you can also reverse engineer structs etc
does something happens when i subtract two pointers?
bytes?
So you can do with them what you want, but some operations don't make sense and some do
hm ok
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
pointer arithmetic is scaled by the datatype size. a struct is just a datatype and the compiler knows the size.
but you can also cast any pointer to a unsigned char* then you can address every byte
addresses in the virtual memory, a pointer is a variable whichs value is a memory address
can we do pointer arithmetic with java? no?
we cant add or subtract right?
only compare and dereference?
you do not have pointers in java. that the jvm might implement the references with pointers does not matter
ok,
First thing I said: "This is definitely not a Java question", because Java doesn't have pointers
The closest you can get is maybe memorysegments
i chose the wrong tag sorry
Not about the tag, about the fact that you asked that again lol
no i was just ccuroius if we could do pointer arithmetic in java
Hence why I said that this is definitely not a Java question
Implying that there's no pointers in Java
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
You very much can, but doing so with java objects is highly not recommended and VM implementation specific
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
Changed the category to C|C++.
<@&987246683568103514> please have a look, thanks.
A pointer is simply a 64-bit integer containing the memory address of a variable
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
Did you just assume the size of my pointers? π
I will assume the size of your pointers