new-operator

JavaScript new keyword and objects scopes

泪湿孤枕 提交于 2019-11-30 23:50:50
Later today, I was scrolling through ejhon.com slides and I found out the following: Give this code function katana () { this.myvar = true; } katana (); console.info (myvar); Past the moment I compiled the code, I thought that myvar is attached to the katana function. Actually, it gets attached to the window objects, which pollutes the global namespace. I returned to my projects, which all uses the same approach.. a little bit differently function katana () { this.myvar = true; } var xyz = new katana(); console.info (myvar); I have a function object and instead of executing the function, I

Difference between Shadows (VB.NET) and New (C#)

冷暖自知 提交于 2019-11-30 23:28:38
问题 Simple question from a simple-minded: What are the differences between the Shadows keyword in VB.NET and the New keyword in C#? (regarding method signatures of course). 回答1: They are not identical. The Shadowing concept does not exist in C # Consider a vb.net base class with some overloads: Public Class BaseClass Public Function SomeMethod() As String Return String.Empty End Function Public Function SomeMethod(SomeParam As String) As String Return "Base from String" End Function Public

c++: what's the difference between new Object() and Object()

元气小坏坏 提交于 2019-11-30 20:10:01
问题 so in C++ you can instantiate objects using the new keyword or otherwise... Object o = new Object(); but you can also just do Object o = Object(); what exactly is the difference b/w the two and why would I use one over the other? 回答1: You can't do Object o = new Object(); The new operator returns a pointer to the type. It would have to be Object* o = new Object(); The Object instance will be on the heap . Object o = Object() will create an Object instance on the stack . My C++ is rusty, but I

Array of structs and new / delete

依然范特西╮ 提交于 2019-11-30 19:51:21
I have a struct like this: class Items { private: struct item { unsigned int a, b, c; }; item* items[MAX_ITEMS]; } Say I wanted to 'delete' an item, like so: items[5] = NULL; And I created a new item on that same spot later: items[5] = new item; Would I still need to call delete[] to clean this up? Or won't this be needed since bounds of array items[] are known before compiling? Is setting that pointer to NULL valid or should I be calling delete there? You need to call delete before setting it to NULL. (Setting it to NULL isn't required, it just helps reduce bugs if you accidentally try to

Maximum memory that can be allocated dynamically and at compile time in c++

折月煮酒 提交于 2019-11-30 19:30:14
I am playing around to understand how much memory can be allocated. Initially I thought that the maximum memory which can be allocated is equal to Physical memory (RAM). I checked my RAM on Ubuntu 12.04 by running the command as shown below: ~$ free -b total used free shared buffers cached Mem: 3170848768 2526740480 644108288 0 265547776 1360060416 -/+ buffers/cache: 901132288 2269716480 Swap: 2428497920 0 2428497920 As shown above,total physical memory is 3Gig (3170848768 bytes) out of which only 644108288 bytes is free, so I assumed I can at max allocate only this much memory. I tested it by

size_t parameter new operator

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 19:27:53
I have a point in my mind which I can't figure out about new operator overloading. Suppose that, I have a class MyClass yet MyClass.h MyClass.cpp and main.cpp files are like; //MyClass.h class MyClass { public: //Some member functions void* operator new (size_t size); void operator delete (void* ptr); //... }; //MyClass.cpp void* MyClass::operator new(size_t size) { return malloc(size); } void MyClass::operator delete(void* ptr) { free(ptr); } //main.cpp //Include files //... int main() { MyClass* cPtr = new MyClass(); delete cPtr } respectively. This program is running just fine. However, the

nothrow or exception?

时间秒杀一切 提交于 2019-11-30 18:54:13
I am a student and I have small knowledge on C++, which I try to expand. This is more of a philosophical question.. I am not trying to implement something. Since #include <new> //... T * t = new (std::nothrow) T(); if(t) { //... } //... Will hide the Exception, and since dealing with Exceptions is heavier compared to a simple if(t) , why isn't the normal new T() not considered less good practice, considering we will have to use try-catch() to check if a simple allocation succeeded (and if we don't, just watch the program die)?? What are the benefits (if any) of the normal new allocation

JavaScript new keyword and objects scopes

我的梦境 提交于 2019-11-30 18:32:10
问题 Later today, I was scrolling through ejhon.com slides and I found out the following: Give this code function katana () { this.myvar = true; } katana (); console.info (myvar); Past the moment I compiled the code, I thought that myvar is attached to the katana function. Actually, it gets attached to the window objects, which pollutes the global namespace. I returned to my projects, which all uses the same approach.. a little bit differently function katana () { this.myvar = true; } var xyz =

Will new operator return NULL? [duplicate]

一曲冷凌霜 提交于 2019-11-30 17:30:08
Possible Duplicate: Will new return NULL in any case? Say i have a class Car and i create an object Car *newcar = new Car(); if(newcar==NULL) //is it valid to check for NULL if new runs out of memory { } On a standards-conforming C++ implementation, no. The ordinary form of new will never return NULL ; if allocation fails, a std::bad_alloc exception will be thrown (the new (nothrow) form does not throw exceptions, and will return NULL if allocation fails). On some older C++ compilers (especially those that were released before the language was standardized) or in situations where exceptions

Finding size of dynamically allocated array

时光怂恿深爱的人放手 提交于 2019-11-30 16:38:28
问题 Why is it not possible to get the length of a buffer allocated in this fashion. AType * pArr = new AType[nVariable]; When the same array is deallocated delete [] pArr; the runtime must know how much to deallocate. Is there any means to access the length before deleting the array. If no, why no such API is provided that will fetch the length? 回答1: Is there any means to access the length before deleting the array? No. there is no way to determine that. The standard does not require the