new-operator

declaring objects in C++ vs java

吃可爱长大的小学妹 提交于 2019-12-23 20:01:34
问题 I have been using c++ for a while now and I am learning java, declaring objects in java is confusing me, In java we write myclass myobject = new myclass(); myobject.mymethod(); Is it same as this code in c++ ? myclass *myobject = new myclass(); myobject->mymethod(); i.e is the memory getting allocated on heap ? If it is on heap why we never free the memory. I believe the new keyword is the same. If so, how do we allocate memory on stack? 回答1: Is it same as this code in c++ ? Yes. It's the

Replacing libstdc++.dylib (4.0) global new and delete operators on OSX

老子叫甜甜 提交于 2019-12-23 17:50:38
问题 I'm trying hard to replace the global new and delete operators with XCode 3.2, GCC 4.2, libstdc++ 4.0, dynamic version. I took the protypes directly from the header "new" and implemented them. They are pasted below. The project is a .plugin so a dynamic lib. This plug-in MUST delegate allocation to the main application's own alloc/free routines, which are in an old C SDK. All my own call to new/delete along with std::list and std::map allocations are correctly replaced, BUT not when std:

Replacing libstdc++.dylib (4.0) global new and delete operators on OSX

ぃ、小莉子 提交于 2019-12-23 17:43:34
问题 I'm trying hard to replace the global new and delete operators with XCode 3.2, GCC 4.2, libstdc++ 4.0, dynamic version. I took the protypes directly from the header "new" and implemented them. They are pasted below. The project is a .plugin so a dynamic lib. This plug-in MUST delegate allocation to the main application's own alloc/free routines, which are in an old C SDK. All my own call to new/delete along with std::list and std::map allocations are correctly replaced, BUT not when std:

How does C# manage memory allocated by a new operator inside a loop?

戏子无情 提交于 2019-12-23 15:44:56
问题 For example: for (i=0;i<10;i++) { myclass = new myclass(); // do stuff with myclass } Questions: How will all the memory that has been allocated by doing 10 allocations in this case be retrieved? What will my memory footprint be at the end of execution? With delete functionality in C++, one had more control over this but in this case, for the second iteration, myclass would simply take a new allocation and move on? 回答1: Since there are no more references to each new object after the next

is the following new overload leaking memory?

末鹿安然 提交于 2019-12-23 15:17:31
问题 I have encountered the following code: class a { public: void * operator new(size_t l, int nb); double values; }; void *a::operator new (size_t l,int n) { return new char[l+ (n>1 ? n - 1 : 0)*sizeof(double)]; } From what I get it is then used to have an array like structure that start at "values": double* Val = &(p->a->values) + fColumnNumber; My question is : is there a memory leak? I am very new to overloading new operator, but I'm pretty sure that the memory allocated is not deallocated

How to use delete with a variable pointed to by two pointers?

走远了吗. 提交于 2019-12-23 12:54:37
问题 Let say I have a hypothetical pointer declared with new like so: int* hypothetical_pointer = new int; and create another hypothetical pointer, with the same value: int* another_hypothetical_pointer = hypothetical_pointer; If I were to go about deleting these, which were declared with new, would I have to delete both pointers, or only the one explicitly declared with new? Or could I delete either pointer? 回答1: delete destroys the dynamically allocated object pointed to by the pointer. It doesn

How large is the attributes can a class object hold? how to determine the stack/heap limit?

人走茶凉 提交于 2019-12-23 12:14:31
问题 I have a class which requiring a large amount of memory. class BigClass { public: BigClass() { bf1[96000000-1] = 1; } double bf1[96000000]; }; I can only initiate the class by "new" a object in heap memory. BigClass *c = new BigClass(); assert( c->bf1[96000000-1] == 1 ); delete c; If I initiate it without "new". I will get a segmentation fault in runtime. BigClass c; // SIGSEGV! How can I determine the memory limit? or should I better always use "new"? 回答1: The stack have a fixed size that is

Why does c++ have its separate syntax for new & delete?

梦想的初衷 提交于 2019-12-23 12:13:52
问题 Why can't it just be regular function calls? New is essentially: malloc(sizeof(Foo)); Foo::Foo(); While delete is Foo:~Foo(); free(...); So why does new/delete end up having it's own syntax rather than being regular functions? 回答1: Here's a stab at it: The new operator calls the operator new() function. Similarly, the delete operator calls the operator delete() function (and similarly for the array versions). So why is this? Because the user is allowed to override operator new() but not the

Is memory address 0x0 usable?

霸气de小男生 提交于 2019-12-23 10:30:09
问题 I was wondering... what if when you do a new, the address where the reservation starts is 0x0? I guess it is not possible, but why? is the new operator prepared for that? is that part of the first byte not usable? it is always reserved when the OS starts? Thanks! 回答1: The null pointer is not necessarily address 0x0 , so potentially an architecture could choose another address to represent the null pointer and you could get 0x0 from new as a valid address. (I don't think anyone does that, btw,

Is “new” still not recommended in JavaScript?

时光怂恿深爱的人放手 提交于 2019-12-23 09:02:38
问题 So I see plenty of JavaScript code out there that uses "new" when making constructors. After reading part of JavaScript the Good Parts it seems that using "new" isn't the cat's pajamas. That was 4 years ago though... Is it still not recommended? What is the current standard? 回答1: Since when is new not recommended? D. Crockford has a valid point and a strong opinion but new is part of the language and it's very much being used in many projects. new is part of the prototype inheritance model,