new-operator

Why do I need to call new? [duplicate]

孤街浪徒 提交于 2019-12-21 20:03:39
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: When to use “new” and when not to, in C++? When should I use the new keyword in C++? It seems like I could program something without ever using the word new , and I would never have to worry about deleting anything either, so why should I ever call it? From what I understand, it's because I would run out of stack memory. Is this correct? I guess my main question is, when should I call new ? 回答1: It's a matter

Allocating large blocks of memory with new

…衆ロ難τιáo~ 提交于 2019-12-21 12:29:03
问题 I have the need to allocate large blocks of memory with new. I am stuck with using new because I am writing a mock for the producer side of a two part application. The actual producer code is allocating these large blocks and my code has responsibility to delete them (after processing them). Is there a way I can ensure my application is capable of allocating such a large amount of memory from the heap? Can I set the heap to a larger size? My case is 64 blocks of 288000 bytes. Sometimes I am

Reallocating memory via “new” in C++

ぐ巨炮叔叔 提交于 2019-12-21 09:17:48
问题 Quick question regarding memory management in C++ If I do the following operation: pointer = new char [strlen(someinput_input)+1]; And then perform it again, with perhaps a different result being returned from strlen(someinput_input) . Does this result in memory being left allocated from the previous " new " statement? As in, is each new statement receiving another block of HEAP memory from the OS, or is it simply reallocating? Assuming I do a final delete pointer[]; will that deallocate any

C++: is push_back(new Object()) a memory leak?

别来无恙 提交于 2019-12-20 18:09:10
问题 Is the following C++ code a memory leak? list.push_back(new String("hi")); As I understand it, push_back from any std collection/container always makes a copy. So if the new string is copied, nothing can ever delete the new'd string right? since there is no reference to it after the push_back... Am I correct or wrong here? Thanks. Jbu edit: I think I am wrong, since new will return a pointer...we'll always have the pointer to be able to delete the new String 回答1: No, the vector stores

what's more efficient? to empty an object or create a new one?

感情迁移 提交于 2019-12-20 12:28:40
问题 how expensive is 'new'? I mean, should I aim at reusing the same object or if the object is 'out of scope' it's the same as emptying it? example, say a method creates a list: List<Integer> list = new ArrayList<Integer>(); at the end of the method the list is no longer in use - does it mean that there's no memory allocated to it anymore or does it mean that there's a null pointer to it (since it was 'created'). Alternately, I can send a 'list' to the method and empty it at the end of the

What is exactly happening when instantiating with 'new'?

喜夏-厌秋 提交于 2019-12-20 11:49:10
问题 Let's consider the following code: class a { public $var1; function disp(){ echo $this->var1; } } $obj1 = new a; echo '<br/>After instantiation into $obj1:<br/>'; xdebug_debug_zval('obj1'); $obj1->var1 = "Hello "; echo '<br/><br/>After assigning "Hello" to $obj->var1:<br/>'; $obj1->disp(); echo "<br/><br/>"; xdebug_debug_zval('obj1'); The output: After instantiation into $obj1: obj1: (refcount=1, is_ref=0)=class a { public $var1 = (refcount=2, is_ref=0)=NULL } After assigning "Hello" to $obj-

Override new operator in C++ while crtdbg.h is causing conflicts

只谈情不闲聊 提交于 2019-12-20 09:46:00
问题 While trying out some memory tracking and preparation for my own memory manager, I tried to override the new operator. The article on flipcode was my main guideline in this process ( http://www.flipcode.com/archives/How_To_Find_Memory_Leaks.shtml ). After implementing the techniques described in that article, I am left with the problem that somewhere in the STL the "crtdbg.h" is being included either directly or indirectly through some of the header-files that are being included (Using Visual

Is it possible to completely disable the default C++ new operator?

折月煮酒 提交于 2019-12-20 09:26:35
问题 Because our app has hard performance and memory constraints, our coding standards forbid the use of the default heap — ie, no malloc , no default new . Every memory allocation has to choose one of a few specific allocators; something like // declared globally void* operator new( size_t size, CustomAllocHeap* heap, const char* perpetrator_name ) { return heap->Allocate( size, perpetrator_name ); } // imagine a bunch of CustomAllocHeap's declared globally or statically, thus Vector* v = new(

What is the difference between type and type.__new__ in python?

冷暖自知 提交于 2019-12-20 09:18:04
问题 I was writing a metaclass and accidentally did it like this: class MetaCls(type): def __new__(cls, name, bases, dict): return type(name, bases, dict) ...instead of like this: class MetaCls(type): def __new__(cls, name, bases, dict): return type.__new__(cls, name, bases, dict) What exactly is the difference between these two metaclasses? And more specifically, what caused the first one to not work properly (some classes weren't called into by the metaclass)? 回答1: In the first example you're

new Keyword is used for creating object without assigning to an object reference

萝らか妹 提交于 2019-12-20 07:48:28
问题 Currently i am referring Thread class in java .so i came across a program in which object is created without referring to to the object reference.can anyone explain the concept here is the code // Create a second thread. class NewThread implements Runnable { Thread t; NewThread() { // Create a new, second thread t = new Thread(this, "Demo Thread"); System.out.println("Child thread: " + t); t.start(); // Start the thread } // This is the entry point for the second thread. public void run() {