0

I am creating a deconstructor, and I want to assert that my pointer float *queue is pointing to an array of floats. The compiler does not seem to like

assert([]queue);

and my program seg faults if I use

assert(queue != NULL); delete []queue;

. Thanks for the help.

7
  • 1
    No you cannot. I suggest you ask about your actual problematic code instead, since your question indicates that you have some fundamental design flaw in it. Commented Feb 9, 2020 at 0:16
  • you might research typeof and decltype, but that doesn't seem too helpful with just "a pointer" Commented Feb 9, 2020 at 0:20
  • Okay, well I am creating a queue from scratch for school. The queue's size is decided at run time, so I thought creating a dynamic array would be the best way to implement the code. I created a constructor that takes an argument for the size of queue, however, they want us to use a constructor with no arguments. Now, I know that MY driver code will always make an array, but I did not want to make an assumption that there will always be a new float [capacity] , when calling the deconstructor. @walnut Commented Feb 9, 2020 at 0:25
  • 2
    @Nate In practice, you should always use std::vector for that. If you are not allowed to use it, then new float[capacity] is correct. The queue should be a class and the queue pointer should be a private member of that class. Only the member functions that you implement should (be able to) change the pointer. This way you can be sure that it is always either a null pointer (when you set it to that) or a pointer to a float array allocated with new float[...]. No outside code should be allowed to change the pointer. Then you don't need the check that you are asking for. Commented Feb 9, 2020 at 0:30
  • 2
    @Nate Yes, you need to always initialize all non-class types with a sensible value, preferably immediately when you declare them (btw. since C++11 this works inside classes for class members as well). Only class types can have a default constructor that initializes to a reasonable initial value. Commented Feb 9, 2020 at 0:38

1 Answer 1

2

No you cannot.

You can test whether a pointer has a null pointer value by comparing it against nullptr (or NULL in pre-C++11; NULL should never be used since C++11), but you can never tell whether a non-null pointer is pointing to a valid object, whether that object is part of an array or whether that object/array was allocated by new/new[].

It is the programmer's job to assure that the code can never reach a state where the above information is needed, but unavailable.

The easiest way of doing that is to never use raw new/delete. Instead only use std::vector and std::unique_ptr.


You also don't need to check for a null pointer before calling delete[]. delete[] can be called with a null pointer, in which case it simply doesn't do anything. You cannot call delete[] with a non-null pointer that doesn't have a value returned by new[] (and that hasn't been delete[]ed yet) tough. That would have undefined behavior.

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

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.