new-operator

How to dynamically allocate arrays in C++

牧云@^-^@ 提交于 2019-11-30 16:19:32
问题 I know how to dynamically allocate space for an array in C. It can be done as follows: L = (int*)malloc(mid*sizeof(int)); and the memory can be released by: free(L); How do I achieve the equivalent in C++? Specifically, how do I use the new and delete[] keywords? Especially in the context of creating/destroying a linked list node, or creating and destroying an array whose size is given by a variable during compile time? 回答1: L = new int[mid]; delete[] L; for arrays (which is what you want) or

How much memory should you be able to allocate?

China☆狼群 提交于 2019-11-30 14:04:39
问题 Background: I am writing a C++ program working with large amounts of geodata, and wish to load large chunks to process at a single go. I am constrained to working with an app compiled for 32 bit machines. The machine I am testing on is running a 64 bit OS (Windows 7) and has 6 gig of ram. Using MS VS 2008. I have the following code: byte* pTempBuffer2[3]; try { //size_t nBufSize = nBandBytes*m_nBandCount; pTempBuffer2[0] = new byte[nBandBytes]; pTempBuffer2[1] = new byte[nBandBytes];

Parameter “size” of member operator new[] increases if class has destructor/delete[]

让人想犯罪 __ 提交于 2019-11-30 13:33:41
4 classes in the following codes: A, B, C and D. They all have a member operator new[] . Besides, B has a constructor; C has a destructor; D has a member operator delete[] . The Parameter size of member operator new[] and the sizeof of the 4 classes are output: new[] A 40 new[] B 40 new[] C 48 new[] D 48 sizeof(A) 4 sizeof(B) 4 sizeof(C) 4 sizeof(D) 4 What's the reason for the differences of size ? Codes(ugly I know): #include <iostream> using namespace std; class A { int i; public: static void* operator new[](std::size_t size) throw(std::bad_alloc) { cout << "new[] A " << size << endl; return

How can I construct an object using an array of values for parameters, rather than listing them out, in JavaScript?

扶醉桌前 提交于 2019-11-30 13:04:53
问题 Is this possible? I am creating a single base factory function to drive factories of different types (but have some similarities) and I want to be able to pass arguments as an array to the base factory which then possibly creates an instance of a new object populating the arguments of the constructor of the relevant class via an array. In JavaScript it's possible to use an array to call a function with multiple arguments by using the apply method: namespace.myFunc = function(arg1, arg2) { /

Difference between object a = new Dog() vs Dog a = new Dog()

こ雲淡風輕ζ 提交于 2019-11-30 12:57:58
问题 object a = new Dog(); vs Dog a = new Dog(); In both cases a.GetType() gives Dog . Both invoke same constructor (with same hierarchy). Then can you please tell me the difference between these two statements? 回答1: Both create a Dog object. Only the second allows you to directly invoke Dog methods or to otherwise treat it like a dog, such as if you need to pass the object to a method as a parameter of type Dog (or something in the Dog hierarchy that is more specific than simply object ). object

Does ::operator new(size_t) use malloc()?

允我心安 提交于 2019-11-30 12:56:42
Does ::operator new(size_t) call malloc() internally, or does it use system calls / OS-specific library calls directly? What does the C++ standard say? In this answer it says that: malloc() is guaranteed to return an address aligned for any standard type. ::operator new(n) is only guaranteed to return an address aligned for any standard type no larger than n , and if T isn't a character type then new T[n] is only required to return an address aligned for T . And that suggests that new() cannot be required to call malloc() . Note: There's an SO question about everything operator new does other

Performance cost of 'new' in C#?

删除回忆录丶 提交于 2019-11-30 12:40:46
问题 In C# what is the performance cost of using the new keyword? I ask specifically in relation to games development, I know in C++ it is a definite no-no to be newing things every update cycle. Does the same apply to C#? I'm using XNA and developing for mobile. Just asking as an optimization question really. 回答1: There are three parts to the cost of new : Allocating the memory (may not be required if it's a value type) Running the constructor (depending on what you're doing) Garbage collection

Is it considered good style to dereference `new` pointer?

偶尔善良 提交于 2019-11-30 11:34:12
To avoid keep having to use -> and instead work directly with the object, is it acceptable practice to do: obj x = *(new obj(...)); ... delete &obj; This is not just poor practice, but: Leaking memory (most likely, unless you are using some pattern that is not visible from the code you provided), since obj will store a copy of the original object created by the new expression, and the pointer to that object returned by new is lost; Most importantly, undefined behavior , since you are passing to delete a pointer to an object that was not allocated with new . Per paragraph 5.3.5/2 of the C++11

Why is ::operator new[] necessary when ::operator new is enough?

时光毁灭记忆、已成空白 提交于 2019-11-30 11:30:59
问题 As we know, the C++ standard defines two forms of global allocation functions: void* operator new(size_t); void* operator new[](size_t); And also, the draft C++ standard (18.6.1.2 n3797) says: 227) It is not the direct responsibility of operator new or operator delete to note the repetition count or element size of the array. Those operations are performed elsewhere in the array new and delete expressions. The array new expression, may, however, increase the size argument to operator new to

More about Virtual / new…plus interfaces!

﹥>﹥吖頭↗ 提交于 2019-11-30 10:14:26
Yesterday I posted a question about the new/virtual/override keywords, and i learned a lot from your answers. But still i remain with some doubts. In between all the "boxes", i lost touch with what's really happening to the type's method tables. For instance: interface I1 { void Draw(); } interface I2 { void Draw(); } class A : I1, I2 { public void Minstance() { Console.WriteLine("A::MInstance"); } public virtual void Draw() { Console.WriteLine("A::Draw"); } void I2.Draw() { Console.WriteLine("A::I2.Draw"); } } class B : A, I1, I2 { public new virtual void Draw() { Console.WriteLine("B::Draw")