Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ - what is the aim of operator new?


  • Please log in to reply
2 replies to this topic

#1 Cybbermouse

Cybbermouse

  • Members
  • 72 posts
  • OFFLINE
  •  
  • Gender:Female
  • Local time:11:33 AM

Posted 01 November 2014 - 03:50 AM

I heard that if I want to dispose unlimited number of memory cells I use the operator new. For example : I want to have a table[x] and x is unknown then I wanna reserve some place (because I don't know x will be 10 or 4503 or 15). I do it with the operator new, *index and alternatively with delete (if I want to delete *index). 

I understand it. 

But in my book is :

"double *index
index = new double [15]"

So : if I know that table has got 15 elements why I use the operator new?
Maybe there is more than one application of new?



BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:01:33 PM

Posted 01 November 2014 - 08:10 PM

There are two forms of operator new:
  • Traditional operator new; allocates memory for an object or array of objects and calls any necessary constructor(s) to initialize the memory in question.
  • Placement new; calls the constructor for an object to initialize it in a given memory block; this is how, for example std::vector is able to have non-constructed objects in its underlying array.
In the example in your book, they're using a fixed value, 15, to demonstrate how this looks in code; but that's not always known in advance. Consider:

double* give_me_x_doubles(std::size_t numberOfDoubles)
{
    return new double[numberOfDoubles];
}
Here the size of the resulting array is not known until runtime.

That said, in most real code you'll want to use std::vector or std::unique_ptr<T[]> to ensure that you can't have memory leaks by forgetting to call operator delete[].

Billy3
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 Cybbermouse

Cybbermouse
  • Topic Starter

  • Members
  • 72 posts
  • OFFLINE
  •  
  • Gender:Female
  • Local time:11:33 AM

Posted 04 November 2014 - 09:45 AM

Thanks very much for Your explanation.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users