c++

C/C++ bitfields versus bitwise operators to single out bits, which is faster, better, more portable?

I need to pack some bits in a byte in this fashion: struct { char bit0: 1; char bit1: 1; } a; if( a.bit1 ) /* etc */ Or: if( a & 0x2 ) /* etc */ From the source code clarity it's pretty obvious to me that bitfields are neater. But which option is faster? I know the speed difference won't be too much if any, but as I can use any of th...

std::vector insert() reallocation

I was looking through the std::vector code and I found something I didn't quite get. When capacity < size() + 1 it needs to reallocate the buffer so it can insert the new element. What it does (as far as I've been able to extract from the code) is: allocate the new buffer copy the prefix of the old buffer (0 - index of insertion) const...

GDB doesn't work with -D_FILE_OFFSET_BITS=64

I'm compiling a i386 C++ app on Snow Leopard. When I compile with -D_FILE_OFFSET_BITS=64 I can't use the binary with gdb. I get the following error message: warning: Could not find object file "/var/folders/kw/kwmH332LGwCIh3GrRREgCk+++TI/-Tmp-//ccZfMAM5.o" - no debug information available for "test.cpp". ...

Can anyone explain this algorithm for calculating large factorials?

i came across the following program for calculating large factorials(numbers as big as 100).. can anyone explain me the basic idea used in this algorithm?? I need to know just the mathematics implemented in calculating the factorial. #include <cmath> #include <iostream> #include <cstdlib> using namespace std; int main() { unsig...

Is there a readable implementation of the STL?

I'm on Linux; looking at the STL headers; they're really really complicated. Is there, somewhere, a smaller version of STL that has the core features of the STL, but is actually readable? Thanks! ...

SFINAE + sizeof = detect if expression compiles

I just found out how to check if operator<< is provided for a type. template<class T> T& lvalue_of_type(); template<class T> T rvalue_of_type(); template<class T> struct is_printable { template<class U> static char test(char(*)[sizeof( lvalue_of_type<std::ostream>() << rvalue_of_type<U>() )]); template<class U> sta...

what kind of datatype should i use for this 600851475143 in c++?

i'm using c++, even if i declare long int, there is error like...... long int num = 600851475143; warning: integer constant is too large for ‘long’ type which datatype should be used in this case? ...

gcc - significance of -pthread flag when compiling

In various multi threaded C and C++ projects I've seen the -pthread flag applied to both the compiling and linking stage while others don't use it at all and just pass -lpthread to the linking stage. Is there any danger not compiling and linking with the -pthread flag - i.e. what does -pthread actully do ? I'm primarly interrested in li...

C++ encoding macro

Is there a macro that tells you what encoding C++ is using for its wchar_t type? I am currently stuck to GNU and clang. I am guessing UTF32 because my wchar_t has a size of 4 bytes. Although it could be UTF-16, it also uses 4 bytes for some code-points. But then there is still the problem of UCS-4 or UTF-32LE or UTF-32BE. Any help/expe...

C++ strcpy non-constant expression as array bound

Hi I turned back to C++ after a long time in C#, PHP and other stuff and I found something strange: temp.name = new char[strlen(name) + strlen(r.name) + 1]; this compiles temp.name = (char *)malloc(sizeof(char[strlen(name) + strlen(r.name) + 1])); this doesn't (temp.name is a char *) The compiler error is error C2540: n...

Create Square Window C++

Hi everyone, Stuck on a little fiddly problem. I'm creating a GUI in C++ using XP and VS C++ using the command CreateWindow(). My question is, how do I make the inside paintable region a perfect square. When passing in the size of the window to create, some of this is deducted for the menu bar at the top, border all around etc. Are ther...

How can I generate random samples from bivariate normal and student T distibutions in C++?

Hi, what is the best approach to generate random samples from bivariate normal and student T distributions? In both cases sigma is one, mean 0 - so the only parameter I am really interested in is correlation (and degrees of freedom for student t). I need to have the solution in C++, so I can't unfortunately use already implemented funct...

Typecasting, ints and chars in c++

I'm trying to write a function to detect separators as defined by an assignment and I know it is not good programming style to #define EXCLAMATION_POINT 33, but to instead do #define EXCLAMATION_POINT '!' This is my code: #include <iostream> #include <ostream> using namespace std; #define PERIOD '.' #define QUESTION_MARK '?' #define...

Ok to provide constructor + trivial operators for behaviorless aggregates?

This is a follow-up question to 2043381. Consider the following: struct DataBundle { std::string name; int age; DataBundle() : age(0) {} DataBundle(const std::string& name, int age) : name(name), age(age) {} void swap(DataBundle& rhs) {name.swap(rhs.name); std::swap(age, rhs.age);} DataBundle& operator=...

Can main function call itself in C++?

Hi, Can anybody tell what's the problem of the code below? int main () { return main() ; } I tested, it compiles correctly. It's running forever. Anymore trick behind the scene? ...

How to determine whether a class has a particular templated member function?

I was wondering if it's possible to extend the SFINAE approach to detecting whether a class has a certain member function (as discussed here: "Is there a Technique in C++ to know if a class has a member function of a given signature?" http://stackoverflow.com/questions/87372/is-there-a-technique-in-c-to-know-if-a-class-has-a-member-func...

Segmentation fault

Hi, This is probably a silly question, but I can't see what I am doing wrong here. I have the class: #include <sys/time.h> #include <gsl/gsl_cdf.h> #include <gsl/gsl_randist.h> #include <cmath> #include "randomnumbergenerator.h" class RandomNumberGenerator { gsl_rng * rn; public: RandomNumberGenerator(); ~RandomNumberGener...

Edit values in config. file

Currently, I'm using ReadFile() and WriteFile() APIs to write & read from file. are there any API functions to replace/edit text if the data is large enough to be written again? I only heard about SetFilePointer() but i'm not quite sure how to use it to replace the text from a file. For example, select a string/char from file say, value...

What does (1U << X) mean?

Hi there! I found the following piece of code: enum { IsDynamic = (1U << 0), // ... IsSharable = (1U << 1), // ... IsStrong = (1U << 2) // ... } What does the (1U << X) mean? Why is this used here ...

C++ compilation problem

Pretty much forgot how to code C++ at all. Anyway, here is the problem. I am trying to load a class defined in a .cpp file I wrote myself. In the main function: ... ... #include "loader.h" ... ... model load_model("TechnologyEnterpriseFacility_Day_Gregor/ TechnologyEnterpriseFacility_Gregor.model"); the header file...