Is make_shared really more efficient than new?

As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4. Well that appears to be your problem. The C++11 standard states the following requirements for make_shared<T> (and allocate_shared<T>), in section 20.7.2.2.6: Requires: The expression ::new (pv) T(std::forward(args)…), where pv has type void* and points to storage suitable to hold … Read more

Why does libc++’s implementation of std::string take up 3x memory as libstdc++?

Here is a short program to help you explore both kinds of memory usage of std::string: stack and heap. #include <string> #include <new> #include <cstdio> #include <cstdlib> std::size_t allocated = 0; void* operator new (size_t sz) { void* p = std::malloc(sz); allocated += sz; return p; } void operator delete(void* p) noexcept { return std::free(p); … Read more

Is snprintf() ALWAYS null terminating?

As the other answers establish: It should: snprintf … Writes the results to a character string buffer. (…) will be terminated with a null character, unless buf_size is zero. So all you have to take care is that you don’t pass an zero-size buffer to it, because (obviously) it cannot write a zero to “nowhere”. … Read more

Why is there no strtoi in stdlib.h?

strtol() converts a string to an integer, a long integer but an integer nevertheless. There is atoi() but it should be avoided in most cases due to the fact that it lacks a mechanism for error reporting from invalid input.

How to compile/link Boost with clang++/libc++?

I didn’t know how to do this either. But after poking around in here, the getting started, and trial and error: $ ./bootstrap –with-toolset=clang $ ./b2 clean $ ./b2 toolset=clang cxxflags=”-stdlib=libc++” linkflags=”-stdlib=libc++” You’ll get lots of warnings. And the signals library will fail to build due to LWG 2059. But otherwise I think it works.

Compiling without libc

If you compile your code with -nostdlib, you won’t be able to call any C library functions (of course), but you also don’t get the regular C bootstrap code. In particular, the real entry point of a program on Linux is not main(), but rather a function called _start(). The standard libraries normally provide a … Read more

Is malloc/free a syscall or a library routine provided by libc?

Very often, malloc and free are using lower-level virtual memory allocation services and allocating several pages (or even megabytes) at once, using system calls like mmap and munmap (and perhaps sbrk). Often malloc prefers to reuse previously freed memory space when relevant. Most malloc implementations use various and different strategies for “large” and “small” allocations, … Read more

Linking against an old version of libc to provide greater application coverage

Work out which symbols in your executable are creating the dependency on the undesired version of glibc. $ objdump -p myprog … Version References: required from libc.so.6: 0x09691972 0x00 05 GLIBC_2.3 0x09691a75 0x00 03 GLIBC_2.2.5 $ objdump -T myprog | fgrep GLIBC_2.3 0000000000000000 DF *UND* 0000000000000000 GLIBC_2.3 realpath Look within the depended-upon library to see … Read more