0

Is there any way to find out the length/number of pointers in an array of pointers?

i.e.

class Notifications {   
    iMessage **messages;
public:
    Notifications();
    Notifications(const Notifications&);
    Notifications& operator=(const Notifications&);
    Notifications(Notifications&&);
    Notifications&& operator=(Notifications&&);
    ~Notifications();
    void operator+=(const w5::iMessage&);
    void display(std::ostream&) const;

}

How would I be able to find the number of pointers within the array?

11
  • 3
    simple plain array wont tell you that Commented Nov 20, 2014 at 11:25
  • The address of the iMessage object are held in an array of pointers so iMessage** messages Commented Nov 20, 2014 at 11:26
  • 1
    There is not a single array in your code. Commented Nov 20, 2014 at 11:26
  • 2
    Use vectors and I bet many problems will disappear Commented Nov 20, 2014 at 11:32
  • 3
    You tagged this c++, so std::vector will help you, as well as solving/preventing a lot of problems that might occur... Commented Nov 20, 2014 at 11:33

2 Answers 2

3

You have three options, in order of increasing goodness:

Make the last one null and all the other ones non-null. This is the only solution which requires something of your data, think of it as a c-style string except with pointers instead of chars.

for (iMessage** msg  = messages; *msg; ++msg) { ... }

Store the size along with your messages:

iMessage **messages;
const size_t size;

for (size_t i = 0; i < size; ++i) {
    iMessage* msg = messages[i];
    // ...
}

Use a container specially designed to store arbitrary amounts of stuff in contiguous memory:

std::vector<iMessage*> messages;

for (iMessage* msg : messages) { 
    // ...
}

That's basically it, unless you know the size of messages up front and it's a compile-time constant, in which case one of:

iMessage* messages[100];
std::array<iMessage*, 100> messages; 

gives you the size right in the type.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no standard way to do it. Some compilers have functions, to return the size of the allocated block, e.g. _msize in Visual Studio. If you divide that by the size of your pointers, you will get the number of elements in your "array".

But apart from being non-Standard, this only works, if the "array" has been allocated dynamically and the caller intends to use the whole block for the array - which is not always the case: Some libraries build own allocation functions on top of the standard ones. They allocate big blocks and then distribute that block further internally.

The correct way to do this (rather than guessing the size from the pointer) would be to let the code that created the array pass the size on to you and to store it along with the pointer. Alternatively, you can use C++ classes that do this for you (and maybe bounds checking in addition to that).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.