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

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.

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.

stdlib and colored output in C

All modern terminal emulators use ANSI escape codes to show colours and other things. Don’t bother with libraries, the code is really simple. More info is here. Example in C: #include <stdio.h> #define ANSI_COLOR_RED “\x1b[31m” #define ANSI_COLOR_GREEN “\x1b[32m” #define ANSI_COLOR_YELLOW “\x1b[33m” #define ANSI_COLOR_BLUE “\x1b[34m” #define ANSI_COLOR_MAGENTA “\x1b[35m” #define ANSI_COLOR_CYAN “\x1b[36m” #define ANSI_COLOR_RESET “\x1b[0m” int main … 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

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

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

tech