Click here to Skip to main content
15,878,945 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
Hi All,


void *p = new [1024]; - normal new

void *p = operator new[1024]; - operator new


the above statement is true for new and operator new. If yes ,please explain where can i use and how can i apply in my project.






Regards,
Ranjith
Posted
Comments
pasztorpisti 11-Mar-14 19:27pm    
Allocating memory and arrays of primitive elements with new is a bad practice in C++. I consider new[] as an incomplete language feature anyway: you allocate an array and delete[] knows how many objects to destruct when you free an array (so C++ stored the array length somehwere - usually on a negative offset) but you can not query the length of the array yourself, you have to store it in a separate variable...

Use new only to create classes on the heap (and maybe if you are implementing low level classes like container templates). In other cases use container classes like std::vector to get the job done. Its much cleaner and also less error prone when it comes to overindexing and the related dreaded heap corruption problems. High level containers do the memory management (and check for programming mistakes with asserts) for you and you don't perform "manual" memory management, its a very-very bad practice.

1 solution

this will help you.......

Quote:
when you say new(1024), you are using C++ keyword new, which
represents the new operator. The job of new operator is :
1. to allocate the memory
2. to call the constructor.
The (1)st job is done using C++'s inbuilt 'operator new'. (Bad
overloading of terminologies!). The prototype of "operator new" is
void* operator new (size_t);

Thus, In the first example, the new operator expects the 'type' and
not the size, since it also needs to call the constructor on that
type. Hence the first line gives compilation error. (1024 is not a
type.)

On the second line, you are explicitly calling the 'operator new'. In
other words, what you are doing is only the first step (allocating the
memory). The 'operator new' requires size_t which you have provided
properly (1024), and hence the code compiles properly.



visit here...

difference-between-new-operator-new[^]
 
Share this answer
 
Comments
ranjithkumar81 11-Mar-14 6:48am    
SecondLine,which i mention

void *p = operator new[1024];

it gives the following compile time error.

1)void *' differs in levels of indirection from 'std::auto_ptr<_Ty>
2)initializing' : cannot convert from 'overloaded-function' to 'void *
etc...

so what is the proper declaration for operator new?.
[no name] 11-Mar-14 6:57am    
try this...

void* p = operator new (1024);
ranjithkumar81 11-Mar-14 7:19am    
still i am getting the same warning message.

compiler: Visual Studio 2010

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900