[Please do not mail me a copy of your followup]
goodm...@gmail.com spake the secret code
<
c381631c-9f6a-4ecd...@googlegroups.com> thusly:
>class MyClass
>{
>public:
> MyClass(int questionsCount)
> {
>[...]
> answers = new int[questionsCount]; // allocating space
>[...]
> }
> ~MultipleChoiceTest()
> {
> delete [] answers; // <------- (1)
> delete answers; (2)
> }
>[...]
>};
>
>In the class above in the destructor I am not sure if (1) has to be
>used or (2) ..... My best guess would be (2) as i only have one
>pointer not an array of pointers ... Am I right ??
Match scalar delete with scalar new:
int *one = new int;
delete one;
Match array delete with array new:
int *bunches = new int[10];
delete[] bunches
There are source code analysis tools that will catch mismatched
array/scalar new/delete.
From this code, it looks like you are just learning the language.
From this perspective you need to understand the low-level new and
delete operators and how they are used.
From a practical day-to-day perspective, you don't really need to
handle raw pointers much. Instead you use an appropriate "resource
container" for dynamically allocated memory. Need a dynamically
resizable array? Use std::vector. Need an object allocated on the
heap? Use std::unique_ptr or std::shared_ptr for unique or shared
ownership of the allocated object. Resource container classes manage
the raw pointers for you, so you don't need to worry about how you
should call new or how you should call delete.
--
"The Direct3D Graphics Pipeline" free book <
http://tinyurl.com/d3d-pipeline>
The Terminals Wiki <
http://terminals-wiki.org>
The Computer Graphics Museum <
http://computergraphicsmuseum.org>
Legalize Adulthood! (my blog) <
http://legalizeadulthood.wordpress.com>