memory-management

Store pointer value

∥☆過路亽.° 提交于 2020-01-13 04:15:09
问题 As I know, when a pointer is passed into a function, it becomes merely a copy of the real pointer. Now, I want the real pointer to be changed without having to return a pointer from a function. For example: int *ptr; void allocateMemory(int *pointer) { pointer = malloc(sizeof(int)); } allocateMemory(ptr); Another thing, which is, how can I allocate memory to 2 or more dimensional arrays? Not by subscript, but by pointer arithmetic. Is this: int array[2][3]; array[2][1] = 10; the same as: int

The meaning of static in C++

假装没事ソ 提交于 2020-01-13 03:53:05
问题 I thought I was fairly good with C++, it turns out that I'm not. A previous question I asked: C++ const lvalue references had the following code in one of the answers: #include <iostream> using namespace std; int& GenX(bool reset) { static int* x = new int; *x = 100; if (reset) { delete x; x = new int; *x = 200; } return *x; } class YStore { public: YStore(int& x); int& getX() { return my_x; } private: int& my_x; }; YStore::YStore(int& x) : my_x(x) { } int main() { YStore Y(GenX(false)); cout

C++: Do static primitives become invalid at program exit?

為{幸葍}努か 提交于 2020-01-13 03:18:06
问题 Assume I have a function like this: MyClass &MyFunction(void) { static MyClass *ptr = 0; if (ptr == 0) ptr = new MyClass; return MyClass; } The question is at program exit time, will the ptr variable ever become invalid (i.e. the contents of that ptr are cleaned up by the exiting process)? I realize that this function leaks, but it is only an example for simplicity. The same question also applies to other primitives besides pointers as well. How about if I have a static integer, does the

What's the difference between a memory arena and a memory allocator?

…衆ロ難τιáo~ 提交于 2020-01-13 03:02:27
问题 This is more a semantic question than a coding question.... What's the difference between a memory arena and a memory allocator? I'm working in C++ and I'm seeing some memory management libs using concepts like "memory arena", "memory allocator" and sometimes both in the same lib. I know what an allocator is; I'm just not sure what a memory arena is if it's not just another word for allocator. 回答1: "Memory arena" typically means a large lump (or collection of lumps) of memory from which

Can a lower level cache have higher associativity and still hold inclusion?

独自空忆成欢 提交于 2020-01-12 23:15:31
问题 Can a lower level cache have higher associativity and still hold inclusion? Suppose we have 2-level of cache.(L1 being nearest to CPU and L2 being nearest to main memory) L1 cache is 2-way set associative with 4 sets and let's say L2 cache is direct mapped with 16 cache lines and assume that both caches have same block size. Then I think it will follow inclusion property even though L1(lower level) has higher associativity than L2 (upper level). As per my understanding, lower level cache can

Destructing Glib::RefPtr causes failed assertions in the GTK 3 core

╄→гoц情女王★ 提交于 2020-01-12 18:51:50
问题 The guys from Gtkmm are comparing Glib::RefPtr with std::auto_ptr<> : Glib::RefPtr is a smartpointer. Specifically, it is a reference-counting smartpointer. You might be familiar with std::auto_ptr<> , which is also a smartpointer, but Glib::RefPtr<> is much simpler, and more useful. But for some strange reason, I can't get my work done with the RefPtr . The same code is just fine with a auto_ptr . In the following code, SmartPtr is just a placeholder for one of these two smartpointers.

How to reduce memory usage of threaded python code?

我的未来我决定 提交于 2020-01-12 07:39:09
问题 I wrote about 50 classes that I use to connect and work with websites using mechanize and threading. They all work concurrently, but they don't depend on each other. So that means 1 class - 1 website - 1 thread. It's not particularly elegant solution, especially for managing the code, since lot of the code repeats in each class (but not nearly enough to make it into one class to pass arguments, as some sites may require additional processing of retrieved data in middle of methods - like

Autorelease pools in Objective-C - release main AutoreleasePool?

↘锁芯ラ 提交于 2020-01-12 07:35:09
问题 By my understanding, when an object is sent an autorelease message, if no autorelease pools exist other than the one in main.m , the object gets placed in the one in main.m . Assuming this is correct, I have a couple of questions: 1) Do all autoreleased objects stay in that pool until the termination of the app? 2) If 1 is true, does creating an autoreleased object without a local autorelease pool (therefore placing that object in the main.m pool) keep that object in memory until termination

Will this C++ code cause a memory leak (casting array new)

一世执手 提交于 2020-01-12 07:17:22
问题 I have been working on some legacy C++ code that uses variable length structures (TAPI), where the structure size will depend on variable length strings. The structures are allocated by casting array new thus: STRUCT* pStruct = (STRUCT*)new BYTE [sizeof(STRUCT) + nPaddingSize]; Later on however the memory is freed using a delete call: delete pStruct; Will this mix of array new [] and non-array delete cause a memory leak or would it depend on the compiler? Would I be better off changing this

return large data by reference or as return in function?

时间秒杀一切 提交于 2020-01-12 04:45:07
问题 On the job today I had a argue with a collage about passing large data between scopes. The myth was that reference uses less memory/CPU usage when passing between 2 scopes. We build a proof of concept who was right... so: function by_return($dummy=null) { $dummy = str_repeat("1",100 * 1024 * 1024); return $dummy; } function by_reference(&$dummy) { $dummy = null; $dummy = str_repeat("1",100 * 1024 * 1024); } echo memory_get_usage()."/".memory_get_peak_usage()."\n"; //1 always returns: