standard-library

How does stringstream work internally?

不羁岁月 提交于 2019-11-30 20:56:47
I'm asking in context of performance. Is stringstream simply a string/vector, so writing to it may result in its whole content being copied to a bigger chunk of memory, or is it done in a more tricky way (say, a list of strings or whatever)? It's up to the standard library vendor how to implement stringstream (or any library feature for that matter). You can look at the sstream header shipped with your compiler to see how it's implemented there. That much on the theoretical side... As far as practical experience and measurements show, ostringstream is often slow compared to other methods for

How does printf work internally? [duplicate]

给你一囗甜甜゛ 提交于 2019-11-30 17:53:55
This question already has an answer here: how printf works internally.? [duplicate] 1 answer I am curious as to how printf works internally within Linux. I don't understand how it writes data to STDOUT . After a bit of searching for the internals, I downloaded glibc and took a look at the source code: __printf (const char *format, ...) { va_list arg; int done; va_start (arg, format); done = vfprintf (stdout, format, arg); va_end (arg); return done; } After finding this, I went deeper into the vfprintf function - but the file is about 2500 lines of unfamiliar C code. I'm looking for an

Force import module from Python standard library instead of PYTHONPATH default

China☆狼群 提交于 2019-11-30 17:46:16
I have a custom module in one of the directories in my PYTHONPATH with the same name as one of the standard library modules, so that when I import module_name , that module gets loaded. If I want to use the original standard library module, is there any way to force Python to import from the standard library rather than from the PYTHONPATH directory, short of renaming the custom module and changing every reference to point to the new name? The ideal solution would be to rename your module to something not in the standard library. You can also switch absolute imports on if you're on Python 2.5+

How does memchr() work under the hood?

跟風遠走 提交于 2019-11-30 13:56:08
问题 Background: I'm trying to create a pure D language implementation of functionality that's roughly equivalent to C's memchr but uses arrays and indices instead of pointers. The reason is so that std.string will work with compile time function evaluation. For those of you unfamiliar w/ D, functions can be evaluated at compile time if certain restrictions are met. One restriction is that they can't use pointers. Another is that they can't call C functions or use inline assembly language. Having

Where to find algorithms for standard math functions?

随声附和 提交于 2019-11-30 11:51:26
问题 I'm looking to submit a patch to the D programming language standard library that will allow much of std.math to be evaluated at compile time using the compile-time function evaluation facilities of the language. Compile-time function evaluation has several limitations, the most important ones being: You can't use assembly language. You can't call C code or code for which the source is otherwise unavailable. Several std.math functions violate these and compile-time versions need to be written

What is the result of `strtod(“3ex”, &end)` supposed to be? What about `sscanf`?

元气小坏坏 提交于 2019-11-30 09:03:29
In my experiments this expression double d = strtod("3ex", &end); initializes d with 3.0 and places end pointer at 'e' character in the input string. This is exactly as I would expect it to behave. The 'e' character might look as a beginning of the exponent part, but since the actual exponent value (required by 6.4.4.2) is missing, that 'e' should be treated as a completely independent character. However, when I do double d; char c; sscanf("3ex", "%lf%c", &d, &c); I notice that sscanf consumes both '3' and 'e' for the %lf format specifier. Variable d receives 3.0 value. Variable c ends up with

How does memchr() work under the hood?

折月煮酒 提交于 2019-11-30 08:55:32
Background: I'm trying to create a pure D language implementation of functionality that's roughly equivalent to C's memchr but uses arrays and indices instead of pointers. The reason is so that std.string will work with compile time function evaluation. For those of you unfamiliar w/ D, functions can be evaluated at compile time if certain restrictions are met. One restriction is that they can't use pointers. Another is that they can't call C functions or use inline assembly language. Having the string library work at compile time is useful for some compile time code gen hacks. Question: How

Why do iterators need to be default-constructible

梦想的初衷 提交于 2019-11-30 07:54:00
Iterators of the categories forward , bidirectional , and random access need to be default-constructible. Why is this, and why do input and output operators not have to be default-constructible? Forward iterators and stronger are required to refer to some external sequence (see [forward.iterators]/6 which says "If a and b are both dereferenceable, then a == b if and only if *a and *b are bound to the same object.") This means they are generally just a lightweight handle onto something else (e.g. a pointer to an element or a node in a container) and so there is little reason not to require that

Where are the functions in the C standard library defined?

余生长醉 提交于 2019-11-30 07:53:54
问题 I'm not interested in the source code, I want to know how the C compiler (GCC) actually finds the functions. As in, when the preprocessor sees that I've included stdio.h , where does it look to find the files that define the function bodies? Edit I should probably also say I'm using Ubuntu 12.04, but if there's a general answer, that would work too. 回答1: gcc comes with (binary) object files ( not C source files) which contain implementations of all the standard C functions. When you use gcc

How can pointers be totally ordered?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 06:39:37
Pointers in C++ may in general only be compared for equality. By contrast, less-than comparison is only allowed for two pointers that point to subobjects of the same complete object (e.g. array elements). So given T * p, * q , it is illegal in general to evaluate p < q . The standard library contains functor class templates std::less<T> etc. which wrap the built-in operator < . However, the standard has this to say about pointer types (20.8.5/8): For templates greater , less , greater_equal , and less_equal , the specializations for any pointer type yield a total order, even if the built-in