-1

I am currently trying to make a Transposition Table for my chess engine, and am using a class with a dynamically allocated array member of a struct object.

class TranspositionTable {
private:
    TranspositionEntry* data_;
public:
    TranspositionTable() {
        data_ = new TranspositionEntry[5]; //5 is an example but i'm actually using a number over 16 million --
                                           // that's why I need it dynamically allocated
    }
    ~TranspositionTable() {
        delete[] data_;
    }
}

However, when the object is constructed, I check the Visual Studio debugger, and it only shows that the data_ member contains a single struct object, instead of an array of it. I am using the 'new' keyword and have tried changing whether it's a pointer or not etc. but it still doesn't work. Can anyone explain what is going on?

Miles C.
  • 111
  • 4
  • 3
    What exactly do you mean when you say the debugger shows the `data_` member contains a single object? Recall that `data_` can be interpreted as a pointer to the first element of the array as well as a pointer to the array itself. – Nathan Pierson Sep 03 '21 at 04:49
  • Yep! That was it, thanks! – Miles C. Sep 03 '21 at 05:03
  • 1
    Should be able to put `data_,10` in the watch window to see the first 10 items as an array. You can use larger numbers to see more but it can get a bit slow if there's a lot. – Retired Ninja Sep 03 '21 at 05:53
  • 6
    @MilesC. Why not use `std::vector data_: ` and then `TranspositionTable() : data_(5) {}`? Then you don't even need the destructor. Note that your current `TranspositionTable` class is not safely copyable due to the pointer being used as the member. See [the rule of 3](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) and look at the **Managing resources** section. – PaulMcKenzie Sep 03 '21 at 06:11
  • 1
    A pointer only contains a value that is an address in memory - in your case the address of the first element of a dynamically allocated array with five elements - and that offers no information about whether the address is a for single object or (if it is an array) the number of elements. For a debugger to access such information, it must exist at run time - but that relies on the compiler emitting code (or the standard library containing code) to keep track. It is non-trivial for a compiler/library and debugger to coordinate and keep track of such information, so not all do – Peter Sep 03 '21 at 07:52
  • @PaulMcKenzie You're probably right, but I would rather use this approach, because first of all, I'd prefer not to use the standard library because this way is probably slightly more efficient (I know exactly what's going on with the dynamic allocation, and it's only happening once), and second, I don't ever need to copy the object (the actual program will use a Transposition Table of 256 MB!!). If I would want to copy it I would like it to have a shared address. – Miles C. Sep 04 '21 at 16:40
  • @Peter Are you saying that since I am using a raw pointer the compiler won't know how to optimize it very well? Because all the data is trailing without its explicit knowledge? – Miles C. Sep 04 '21 at 16:44
  • @MilesC. I made no comment about compiler optimisation. – Peter Sep 05 '21 at 00:46

1 Answers1

0

I just realized the problem. The array WAS actually being allocated. But since I was in the visual studio debugger, and the value was a pointer, I was actually only seeing the first value. HTHs for other people! My bad!

Miles C.
  • 111
  • 4