How to check if an element in a dynamic array of pointers is nullptr

Hi,

I'm having a bit of difficulty understanding how to check if an element in a dynamic array of pointers is nullptr, mainly i'm not sure how to evaluate the case in an if statement.

How I'm allocating my array:
1
2
3
4
5
6
7
  template <class TYPE>
LPTable<TYPE>::LPTable(int maxExpected): Table<TYPE>(){
    maxExpected_ = maxExpected;
    max_ = maxExpected_ * 1.35;   
    records_ = new Record*[max_];   
    size_ = 0;
}


I've tried to cast records_ to nullptr
records_ = {nullptr}
and I;ve tried to use a while loop to set each element to null but the run-time was far too long for just initialization.

my comparison was just a simple if:
1
2
3
if(records[i] == nullptr){
    //do stuff
}

but this was just skipped over, so I was wondering is there any efficient way to do this check or set all the elements to null without using a loop at initialization?

I know vector is a better solution then **[] but just for curiosity sake I was wondering.

UPDATE:
after a bit of reading in my c++ textbook I found that you can set all elements to nullptr by initializing the array at the time of allocation!
1
2
3
4
5
6
7
  template <class TYPE>
LPTable<TYPE>::LPTable(int maxExpected): Table<TYPE>(){
    maxExpected_ = maxExpected;
    max_ = maxExpected_ * 1.35;   
    records_ = new Record*[max_](); //simply adding the () initializes all elements to null
    size_ = 0;
}
Last edited on
Registered users can post here. Sign in or register to post.