new-operator

C# The 'new' keyword on existing objects

江枫思渺然 提交于 2019-12-12 07:11:03
问题 I was wondering as to what happens to an object (in C#), once its reference becomes reassigned. Example: Car c = new Car("Red Car"); c = new Car("Blue Car"); Since the reference was reused, does the garbage collector dispose / handle the 'Red Car' after it's lost its reference? Or does a separate method need to be implemented to dispose of the 'red car'? I'm primarily wondering because there's a relatively large object that I'm going to recycle, and need to know if there is anything that

Is there any reason to choose __new__ over __init__ when defining a metaclass?

送分小仙女□ 提交于 2019-12-12 07:08:57
问题 I've always set up metaclasses something like this: class SomeMetaClass(type): def __new__(cls, name, bases, dict): #do stuff here But I just came across a metaclass that was defined like this: class SomeMetaClass(type): def __init__(self, name, bases, dict): #do stuff here Is there any reason to prefer one over the other? Update : Bear in mind that I'm asking about using __new__ and __init__ in a metaclass. I already understand the difference between them in another class. But in a metaclass

Why should I avoid using malloc in c++? [duplicate]

夙愿已清 提交于 2019-12-12 05:37:18
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: What is the difference between new/delete and malloc/free? In what cases do I use malloc vs new? Why should I avoid using malloc in c++? 回答1: Because malloc does not call the constructor of newly allocated objects. Consider: class Foo { public: Foo() { /* some non-trivial construction process */ } void Bar() { /* does something on Foo's instance variables */ } }; // Creates an array big enough to hold 42 Foo

problems with operator new when allocating arrays

爱⌒轻易说出口 提交于 2019-12-12 03:41:33
问题 I'm having prblems with my C++/openGL program. at some point of code, like these(it's a constructor): MyObject(MyMesh * m, MyTexture* t, float *c=NULL, float *sr=NULL, int sh=100){ texture=t; mesh=m; subObjects=NULL; texCoords=NULL; if (texture!=NULL){ texCoords=new float[mesh->numSurfacePoints*2]; the new throws an std::bad_alloc exception. it's the same at another place. is it possible, that i ran out of memory? i dont think so, so if you could help me, i would be glad! bye! 回答1: You should

How best to store VERY large 2D list of floats in c++? Error-handling?

坚强是说给别人听的谎言 提交于 2019-12-12 03:13:10
问题 I'm migrating some code from c to c++. I've changed some malloc calls for structure memory allocation to new calls. Basically before the code I'm porting was malloc'ing arrays that contain multiple sets of frame coords, each a couple hundred thousand floats in length -- so the total array length could be in the tens of millions of coordinates. What kind of structure/container should I use? And what kind of protections do I need to catch memory-related errors? Edit 1 I've retitled/rephrased

How Do I Use a Variable in new[]'s Value Initialization

拜拜、爱过 提交于 2019-12-12 01:54:13
问题 So when new ing an array of char s I can value initialize: const char* foo = new char[4]{'J', 'o', 'n', '\0'}; What I want to know is how to use a variable in that initializer_list : const string initializer{"jon"}; const char* foo = new char[4]{initializer.c_str()}; // This doesn't work, can I make it work? 回答1: You can use a variable, but you can't use a string and expect the compiler to magically split it up. const char* ip = initializer.c_str(); const char* foo = new char[4]{ip[0], ip[1],

Can't obtain accurate information of available memory in the heap

半腔热情 提交于 2019-12-12 01:35:18
问题 all I use the following function to obtain information about available memory and largest contiguous block in the heap. int GetLargestContiguousMemory() { MEMORY_BASIC_INFORMATION mbi; DWORD start = 0; bool recording = false; DWORD freestart = 0, largestFreestart = 0; __int64 free = 0, largestFree = 0; while (true) { // SIZE_T s = VirtualQueryEx(hproc, reinterpret_cast<lpvoid>(start), &mbi, sizeof(mbi)); SIZE_T s = VirtualQueryEx(GetCurrentProcess(), (void*)(start), &mbi, sizeof(mbi)); if (s

new-expression and delete-expression on const reference and const pointer

怎甘沉沦 提交于 2019-12-11 22:19:50
问题 C++ Much literature says const references cannot be used to modify their referents and const pointers cannot be used to modify their pointees. Then, why can they be delete d? const int& cirDynamic = *( new int(5) ); // ^ 'const int& cirDynamic = *( &( *( new int(5) ) ) );' gives same output below cout << cirDynamic << endl; // 5 delete &cirDynamic; cout << cirDynamic << endl; // garbage value I know the trailing const in T* const only prevents the pointer from being reseated, but below I use

What does the use of new require you to also call delete?

一曲冷凌霜 提交于 2019-12-11 19:39:13
问题 I am here stuck with a question in my C++ book with the following: "What does the use of new require you to also call delete?" Maybe you have an answer for that? 回答1: Because that is the way C++ is designed & that is the intended behavior. The intention was to provide a memory allocation which you demand and own till you reliquish it explicitly. new gives you a dynamic memory allocation(on heap) which will continue to exist and you own it untill you explicitly deallocate it by calling delete

Javascript banner link open in new window

笑着哭i 提交于 2019-12-11 18:32:45
问题 I have a page with a JavaScript code: http://pastehtml.com/view/1d997i8.html please view source code When you open the page a random banner will be displayed. When I click on the banner, i want to open the link on the banner in a new window. Right now it opens on the same window. Any help is appreciated. Thank you 回答1: < a href="somehwere" target="_blank">yay a new window< /a > You need to set the "target" attribute in the link 回答2: You could use jQuery and include the following in the script