Since I can’t return a local variable, what’s the best way to return a string from a C or C++ function?

You’re absolutely right. Your c array in the second example is being allocated on the stack, and thus the memory will get reused immediately following. In particular, if you had code like printf(“%s\n”,greet()); you’d get weird results, because the call to printf would have reused some of the space of your array. The solution is … Read more

Go 1.3 Garbage collector not releasing server memory back to system

First, note that Go, itself, doesn’t always shrink its own memory space: https://groups.google.com/forum/#!topic/Golang-Nuts/vfmd6zaRQVs The heap is freed, you can check this using runtime.ReadMemStats(), but the processes virtual address space does not shrink — ie, your program will not return memory to the operating system. On Unix based platforms we use a system call to tell … Read more

How-to write a password-safe class?

Yes, first define a custom allocator: template <class T> class SecureAllocator : public std::allocator<T> { public: template<class U> struct rebind { typedef SecureAllocator<U> other; }; SecureAllocator() throw() {} SecureAllocator(const SecureAllocator&) throw() {} template <class U> SecureAllocator(const SecureAllocator<U>&) throw() {} void deallocate(pointer p, size_type n) { std::fill_n((volatile char*)p, n*sizeof(T), 0); std::allocator<T>::deallocate(p, n); } }; This allocator … Read more

Memory usage of current process in C

The getrusage library function returns a structure containing a whole lot of data about the current process, including these: long ru_ixrss; /* integral shared memory size */ long ru_idrss; /* integral unshared data size */ long ru_isrss; /* integral unshared stack size */ However, the most up-to-date linux documentation says about these 3 fields (unmaintained) … Read more

The maximum amount of memory any single process on Windows can address

Mark Russinovich published a multipart series on windows memory resources really covers this very well. You can find it here: http://blogs.technet.com/b/markrussinovich/archive/2008/07/21/3092070.aspx He covers the reasons why the limits are what they are, as well as tests. The code for the tests are floating around in the tubes somewhere. If you want to know about memory … Read more