Topic: What are the key differences between operator new and operator new[]?
Author: xmllmx <xmllmx@gmail.com>
Date: Wed, 3 Feb 2010 11:52:35 CST Raw View
Dear all,
As we know, the C++ standard defines at least four special global
functions as follows:
1) void* operator new(size_t);
2) void* operator new[](size_t);
3) void operator delete(void*);
4) void operator delete[](void*);
In Visual C++, 2) and 4) simply forward their respective call to 1)
and 3). Obviously, 1) and 3) are equivalents respectively to malloc
and free in C. However, when and why should we call 2) and 4)?
Though 2) and 4) are not harmful, but I think them rather ugly.
Because I can not find any necessity of them.
I hope someone can give me a convincing explanation? Thanks in
advance!
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: codingspree <ferdinand.urban@gmail.com>
Date: Thu, 4 Feb 2010 11:54:35 CST Raw View
On Feb 3, 6:52 pm, xmllmx <xml...@gmail.com> wrote:
> Dear all,
>
> As we know, the C++ standard defines at least four special global
> functions as follows:
>
> 1) void* operator new(size_t);
>
> 2) void* operator new[](size_t);
>
> 3) void operator delete(void*);
>
> 4) void operator delete[](void*);
>
> In Visual C++, 2) and 4) simply forward their respective call to 1)
> and 3). Obviously, 1) and 3) are equivalents respectively to malloc
> and free in C. However, when and why should we call 2) and 4)?
>
> Though 2) and 4) are not harmful, but I think them rather ugly.
> Because I can not find any necessity of them.
>
> I hope someone can give me a convincing explanation? Thanks in
> advance!
>
> --
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use mailto:std-...@netlab.cs.rpi.edu]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]
difference between new and new[] is that the new is used to allocate
an object but the new[] is used to allocate array of objects. If you
used new[] you have to used delete[] not just delete.
Hope it helps little bit.
F
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@btinternet.com>
Date: Thu, 4 Feb 2010 18:43:31 CST Raw View
codingspree wrote:
> On Feb 3, 6:52 pm, xmllmx <xml...@gmail.com> wrote:
>
>> Dear all,
>>
>> As we know, the C++ standard defines at least four special global
>> functions as follows:
>>
>> 1) void* operator new(size_t);
>>
>> 2) void* operator new[](size_t);
>>
>> 3) void operator delete(void*);
>>
>> 4) void operator delete[](void*);
>>
>> In Visual C++, 2) and 4) simply forward their respective call to 1)
>> and 3). Obviously, 1) and 3) are equivalents respectively to malloc
>> and free in C. However, when and why should we call 2) and 4)?
>>
>> Though 2) and 4) are not harmful, but I think them rather ugly.
>> Because I can not find any necessity of them.
>>
>> I hope someone can give me a convincing explanation? Thanks in
>> advance!
>>
>> --
>> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
>> [ your news-reader. If that fails, use mailto:std-...@netlab.cs.rpi.edu]
>> [ --- Please see the FAQ before posting. --- ]
>> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]
>>
>
> difference between new and new[] is that the new is used to allocate
> an object but the new[] is used to allocate array of objects. If you
> used new[] you have to used delete[] not just delete.
>
> I suspect that you do not understand the question. He is not asking about
the new versus the new[] expression which is what your answer is about but
the difference between the two memory acquisition operators. The reason for
having two is that it is possible that you may need a different strategy for
acquiring storage for a single object as opposed to storage for an array of
objects.
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]