new-operator

operator new with empty exception-specification calling constructor when allocation returns 0

让人想犯罪 __ 提交于 2019-12-04 11:15:42
I have the following declaration: void * operator new (size_t s, PersistentMemory * m) throw() {return m->allocatePersistentMemory(s);} I'm testing memory exhaustion on start-up, which results in m->allocatePersistentMemory(s); returning 0. New then calls the constructor with a null pointer for this However, based on 3.7.3.1 paragraph 3 of C++ 2003 standard: An allocation function that fails to allocate storage can invoke the currently installed new_handler (18.4.2.2), if any. [Note: A program-supplied allocation function can obtain the address of the currently installed new_handler using the

When is the appropriate time to use the 'new' keyword?

删除回忆录丶 提交于 2019-12-04 10:48:47
问题 When is it necessary to use the new keyword in Java. I know you are supposed to use it when you create an instance of an object like this: TextView textView = new TextView(this); Sometimes in code I notice that new isn't used and I get confused.. In this line of code: AssetManager assetManager = getAssets(); Why isn't an instance of the AssetManager created like this: AssetManager assetManager = new AssetManager(); then it is set equal to getAssests()? When should new be used? Thanks! 回答1:

Why are new()/delete() slower than malloc()/free()?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 10:32:30
问题 Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please kindly point out specifications of standard C++ implementation of new() and delete() if you have them thanks! 回答1: Look at this piece of C code: struct data* pd = malloc(sizeof(struct data)); init_data(pd); The new operator in C++ is essentially doing what the above piece of code does. That's why it is slower than malloc() . Likewise with delete . It's doing the equivalent of this: deinit_data(pd);

Dynamic allocation with DOUBLE POINTERS

我与影子孤独终老i 提交于 2019-12-04 10:31:30
I have a base class Toy and derived classes Toy_remote_car amd Toy_battery_car. I am doing this: Toy** ptr; ptr=new Toy*; ptr[0]=new Toy_remote_car[1]; ptr[1]=new Toy_battery_car[1];/*this is completely wrong according to my teacher because i never created ptr[1]. Instead this is a misuse of memory according to him.*/ The above code(ptr=new Toy*) is creating a single pointer of type Toy(ptr[0]) which contains the object of derived class Toy_remote_car. Now i want to write such a code: ->the number of Toy type pointers should not be predefined. ->instead i would call an add_toy function which

Appending file results in overwrite (Java)

僤鯓⒐⒋嵵緔 提交于 2019-12-04 10:10:29
So I am creating a CSV file and everytime an action occurs I would like to write the data to the file. The problem I am having is that it will overwrite the data when entering the second time. How do I add the data to the end of the file? public boolean save_to_csv(){ //check if directory exists, if not create the folder File folder = new File(Environment.getExternalStorageDirectory() + "/HKA_CAL"); //Environment.getExternalStorageDirectory() get the location of external storage boolean success = true; if(!folder.exists()) { success = folder.mkdir(); } if (success) { //success is true if

To “new” or not to “new”

∥☆過路亽.° 提交于 2019-12-04 07:48:25
Is there a rule of thumb to follow when to use the new keyword and when not to when declaring objects? List<MyCustomClass> listCustClass = GetList(); OR List<MyCustomClass> listCustClass = new List<MyCustomClass>(); listCustClass = GetList(); In your scenario it seems that the actual creation of the object is being performed inside your GetList() method. So your first sample would be the correct usage. When created, your List<MyCustomClass> is stored in the heap, and your listCustClass is simply a reference to that new object. When you set listCustClass to GetList() the reference pointer of

How to add to an existing hash in Ruby

故事扮演 提交于 2019-12-04 07:27:57
问题 In regards to adding an key => value pair to an existing populated hash in Ruby, I'm in the process of working through Apress' Beginning Ruby and have just finished the hashes chapter. I am trying to find the simplest way to achieve the same results with hashes as this does with arrays: x = [1, 2, 3, 4] x << 5 p x 回答1: If you have a hash, you can add items to it by referencing them by key: hash = { } hash[:a] = 'a' hash[:a] # => 'a' Here, like [ ] creates an empty array, { } will create a

Why would you want to hide a method using `new`? [duplicate]

无人久伴 提交于 2019-12-04 05:13:41
This question already has answers here : Closed 8 years ago . Possible Duplicate: C# - new keyword in method signature Let's say I have 3 classes: GrandDad, Dad, Son. Son inherits from Dad, which inherits from GrandDad. Each class implements foo. // GrandDad class: public virtual void foo() // Dad class: new public virtual void foo() // Son class: public override void foo() I don't understand the reason as to why Dad would use the new keyword. As I understand, using new hides a method. Why would you want to do this? I read the MSDN explanation of new, but the discussion was only mechanical,

Is it ok to return None from __new__?

北城余情 提交于 2019-12-04 05:10:57
In general, is it reasonable to return None from a __new__ method if the user of the class knows that sometimes the constructor will evaluate to None? The documentation doesn't imply it's illegal, and I don't see any immediate problems (since __init__ is not going to be called, None not being an instance of the custom class in question!). But I'm worried about whether it might have other unforeseen issues whether it's a good programming practice to have constructors return None Specific example: class MyNumber(int): def __new__(cls, value): # value is a string (usually) parsed from a file if

VIM: set filetype=txt for every new file [No Name]

為{幸葍}努か 提交于 2019-12-04 05:10:26
I tried all possible things to let vim set filetype to 'txt' to all new files I create (in a new tab) but it doesn't work. This is p.e. what I've read on the web a few times: au BufRead,BufNewFile *.txt setlocal ft=txt (to put in _vimrc) However it doesn't work. Can anyone help me? Curt Nelson The following line, added to your .vimrc , will set the filetype to text if it is not already set. autocmd BufEnter * if &filetype == "" | setlocal ft=text | endif All files are considered plain text unless you have file-type detection turned on or explicitly set the file-type. However, Vim lets you set