memory-management

Understanding the memory content of a union

这一生的挚爱 提交于 2019-12-23 07:54:04
问题 Suppose I define a union like this: #include <stdio.h> int main() { union u { int i; float f; }; union u tst; tst.f = 23.45; printf("%d\n", tst.i); return 0; } Can somebody tell me what the memory where tst is stored will look like? I am trying to understand the output 1102813594 that this program produces. 回答1: It depends on the implementation (compiler, OS, etc.) but you can use the debugger to actually see the memory contents if you want. For example, in my MSVC 2008: 0x00415748 9a 99 bb

std::unique_ptr::reset and constructor exceptions

拥有回忆 提交于 2019-12-23 07:48:45
问题 If initialise a unique_ptr like this: std::unique_ptr<Foo> i; i.reset( new Foo() ); but an exception is thrown from Foo::Foo() , the question is: what happens with the memory allocated? how does unique_ptr avoid it being leaked? is this something handled inside new operator? The destructor will certainly be called when the scope exits. Since the reset call is not invoked until new Foo() returns, it seems that this must be handled by new , by freeing the allocated memory when the exception

std::unique_ptr::reset and constructor exceptions

限于喜欢 提交于 2019-12-23 07:48:19
问题 If initialise a unique_ptr like this: std::unique_ptr<Foo> i; i.reset( new Foo() ); but an exception is thrown from Foo::Foo() , the question is: what happens with the memory allocated? how does unique_ptr avoid it being leaked? is this something handled inside new operator? The destructor will certainly be called when the scope exits. Since the reset call is not invoked until new Foo() returns, it seems that this must be handled by new , by freeing the allocated memory when the exception

NSInvocation and memory issues [duplicate]

。_饼干妹妹 提交于 2019-12-23 07:47:49
问题 This question already has answers here : NSInvocation getReturnValue: called inside forwardInvocation: makes the returned object call dealloc: (3 answers) Closed 6 years ago . So I come from the Java world where we are blissfully ignorant of memory management issues. For the most part, ARC has saved my butt, but here is something that has got me stumped. Basically I am using NSInvocations for some stuff, and I ran into some nasty memory issues before I made the following code modifications.

Does JavaScript populate empty array items?

跟風遠走 提交于 2019-12-23 07:40:10
问题 I am coding a lot of annual data in JavaScript, and I was considering adding it to arrays, using the year as the array index and putting the data into the array. However, Firebug seems to be indicating that JavaScript handles this by populating two thousand odd entries in the array with "undefined." With hundreds of such arrays kicking around in active memory, I'm worried the overhead of hundreds of thousands of useless array items could start to slow the program down. Will it? 回答1: When you

In WPF: Children.Remove or Children.Clear doesn't free objects

旧巷老猫 提交于 2019-12-23 07:05:57
问题 Update : I tried this on another, more cleanly installed, machine. I could not reproduce this on that machine. If I find out what offending (VSStudio) component causes this, I will let you know. I create some UIElements from code behind and was anticipating the garbage collection to clear up stuff. However, the objects are not free-ed at the time I expected it. I was expecting them to be freeed at RemoveAt(0), but they are only freed at the end of the program. How can I make the objects be

Is extending a base class with non-virtual destructor dangerous?

£可爱£侵袭症+ 提交于 2019-12-23 07:04:54
问题 In the following code: class A { }; class B : public A { }; class C : public A { int x; }; int main (int argc, char** argv) { A* b = new B(); A* c = new C(); //in both cases, only ~A() is called, not ~B() or ~C() delete b; //is this ok? delete c; //does this line leak memory? return 0; } When calling delete on a class with a non-virtual destructor with member functions (like class C), can the memory allocator tell what the proper size of the object is? If not, is memory leaked? Secondly, if

Memory Management with NSDecimalNumber: How to avoid memory problems in tight loops?

只愿长相守 提交于 2019-12-23 06:57:27
问题 I have a tight loop that iterates about 500 times. In every iteration, it will create a few NSDecimalNumber objects to do some arithmetics. Example - this code snippet is in the for loop. the -decimalNumberByAdding: method creates a new NSDecimalNumber instance and autoreleases it. resultDecimalNumber = [resultDecimalNumber decimalNumberByAdding:anotherDecimalNumber]; So let me get that right: If the loop is huge, I collect thousands of NSDecimalNumber objects which wait for the whole loop to

PostgreSQL installation error — Cannot allocate memory

£可爱£侵袭症+ 提交于 2019-12-23 06:57:27
问题 I'm trying to switch over from sqlite3 to PostgreSQL for development in Rails so that I don't have any heroku problems. I was following the advice given on heroku and a linked-to Railscast, but I ran into the following error after brew installing postgresql. creating template1 database in /usr/local/var/postgres/base/1 ... FATAL: could not create shared memory segment: Cannot allocate memory DETAIL: Failed system call was shmget(key=1, size=2072576, 03600). HINT: This error usually means that

Deallocated Heap Not Being Reclaimed?

老子叫甜甜 提交于 2019-12-23 05:32:32
问题 I have C++ process that ingests large blocks of data and stores them in memory. The storage array contains roughly 10 GB of data partitioned into 4MB blocks. As new data arrives it creates a new block and then deletes an old block if it is full. This process cycles through the full circular buffer once every 10 - 60 seconds. We are running on an x86_64 RH5 and RH6 and compiling with the Intel 14 compiler. We are seeing a problem where the overall process memory usage grows over time until the