-1

I'm working on this code where I want to create a 256-byte aligned vector. In my constructor, I have the following code:

BitVector(long bitSize) {
    vector<long long> temp(bitSize, 0LL);

    int size = sizeof(temp);
    
    void* p2 = aligned_alloc(256, size);

    void* p3 = memmove(p2, &temp, size);

    vector<long long>* t = reinterpret_cast<vector<long long>*>(p3);
    cout<<"t address: "<<t<<endl;
    this->bits = *t;
    cout<<"bits address: "<<&bits<<endl;
};

I'm not sure why, but the output is coming out like this:

t address: 0x55b8623fcf00
bits address: 0x7fff9c7fc6f0

I need to get the exact object from the pointer t, but accessing it by * isn't working.

I also tried storing a pointer to a vector in my class, but ran into issues where random values were being placed into the vector, so I've switched it to this implementation.

Is there a way to get the object from the pointer without changing the address?

More details on what I'm trying to do for clarification:

I'm building a Bloom filter, and I've found that storing long long objects is more efficient from memory than storing bits, so I'm changing single bits in each long long when I need to set a bit. However, for all of this memory optimization to be useful, I need my vector (or whatever storage mechanism) to be 256-byte aligned. I've finished implementing the rest of the Bloom filter methods, but I've been struggling on how to accomplish this alignment.

gigiprab
  • 11
  • 2
  • 2
    `sizeof(vector_name);` is almost always not what you want. A vectors size (via `sizeof`) is a constant and will never change. `vector_name.size()` will give you the number of elements in the vector. – NathanOliver Jun 27 '22 at 20:06
  • 1
    Do you want the `vector` structure to be aligned or the data to be aligned? Sometimes, the `std::vector` is split into two pieces: the contiguous data and the overhead. – Thomas Matthews Jun 27 '22 at 20:08
  • 2
    Looks like you want to align the buffer contained by the `vector`, not the `vector` itself. If this is the case, look into allocators. What you currently have looks like it could be a timebomb. – user4581301 Jun 27 '22 at 20:08
  • @NathanOliver Because I'm allocating memory, I need to get the size of the vector itself, not the number of elements that it contains. – gigiprab Jun 27 '22 at 20:09
  • See also: `std::bitset`. – Thomas Matthews Jun 27 '22 at 20:10
  • 1
    @gigiprab you do understand that `std::vectora (1); sizeof(a);` is the same as `std::vectorb (1000); sizeof(b);`, right? They both just measure `sizeof(std::vector)` – PeterT Jun 27 '22 at 20:11
  • Do you _need_ to use `std::vector` here? This might be one of those rare cases where allocating and freeing memory explicitly is justified. (Wrap it all up in a nice class, of course :) – Paul Sanders Jun 27 '22 at 20:12
  • UB galore. p3 is not a ptr to a vector<> – doug Jun 27 '22 at 20:16
  • @user17732522 how would I do that instead? I agree that I need the first element aligned because the rest of the elements should be stored consecutively, but I'm not sure how to approach writing that. – gigiprab Jun 27 '22 at 20:18
  • 1
    @gigiprab you may want to rethink how you are learning C++. `memmove` is **not** how to copy a `std::vector`. `sizeof` will _not_ tell you how much storage is needed to hold the elements of a `std::vector`. `reinterpret_cast` will _not_ construct a new `std::vector` object. I suspect that your learning materials are not reliable. – Drew Dormann Jun 27 '22 at 20:20
  • 5
    Sounds like [this might be what you need](https://stackoverflow.com/questions/8456236/how-is-a-vectors-data-aligned) – NathanOliver Jun 27 '22 at 20:20
  • @PaulSanders I just posted an update to the question for more details – gigiprab Jun 27 '22 at 20:37
  • You need to realize that the vector doesn't contain the data, it has a pointer to a separate buffer with the data. It's that buffer that you want to be aligned. – Mark Ransom Jun 27 '22 at 20:42
  • @MarkRansom I understand now that I don't need to check the address of the vector, but the address of the first value in the vector to check whether it's 256-byte aligned. However, I still don't understand how to make that buffer be aligned. – gigiprab Jun 27 '22 at 20:45
  • @gigiprab Follow the link that Nathan posted. It looks like that should work for you., – Paul Sanders Jun 27 '22 at 21:00
  • One simple solution is to make the vector larger than you need, then simply ignore the unaligned elements at the front and pretend they don't exist. – Mark Ransom Jun 27 '22 at 21:21

1 Answers1

1

For anyone else who looks at this, I was able to solve it using this, thanks @NathanOliver. My final solution ended up looking like this:

unique_ptr<vector<long long>> arr{new(std::align_val_t{256}) vector<long long>(bitSize)};
gigiprab
  • 11
  • 2