Problems adding offset to raw pointer

Hi, trying to implement ring buffer. I need to add offset of 1 * size_of::<T>() to get next free address where i can write value. Here is push implementation

    pub fn push(&mut self, val: T) -> Result<(), Error> {
        if size_of::<T>() == 0 {
            panic!("No zero sized type");
        }

        println!("{}, {}", self.len, self.cap);

        if self.len < self.cap {
            unsafe {
                self.head.as_ptr().write(val);
                println!("wrote val {}", val);

                println!("ptr * cap {:#?}", self.ptr.add(self.cap));
                println!("head {:#?}", self.head);
                if self.head.as_ptr().add(1) > self.ptr.as_ptr().add(self.cap) {
                    self.head = self.ptr;
                } else {
                    self.head = self.head.add(1);
                }
            };

            self.len += 1;
        } else {
            return Err(Error {
                err_message: "Ringbuffer is full",
            });
        }

        Ok(())
    }

It should work on paper. At least looks good to me, but it prints this:

0, 4                                                                                                                                                                      
wrote val 22                                                                                                                                                              
ptr * cap 0x00007ca8e4000b90                                                                                                                                              
head 0x00007ca8e4000b70                                                                                                                                                   
1, 4                                                                                                                                                                      
wrote val 44                                                                                                                                                              
ptr * cap 0x00007ca8e4000b90                                                                                                                                              
head 0x00007ca8e4000b78                                                                                                                                                   
2, 4                                                                                                                                                                      
wrote val 77                                                                                                                                                              
ptr * cap 0x00007ca8e4000b90                                                                                                                                              
head 0x00007ca8e4000b80                                                                                                                                                   
3, 4                                                                                                                                                                      
wrote val 99                                                                                                                                                              
ptr * cap 0x00007ca8e4000b90                                                                                                                                              
head 0x00007ca8e4000b88                                                                                                                                                   
4, 4 

Why do addresses go like 8 + 2 + 8 + 2 etc? How is that even possible?

The addresses are written in hexadecimal, and in hexadecimal 8 + 8 = 10. You are adding 8 to head each time.

1 Like

Ohhhhhhhhh that was so stupid of me xd. I even used i128 to check what was happening and noticed that