c++

c++ / gcc : segmentation fault at exit of main after catching a signal

The following code triggers a segmentation fault at exit. It seems to happen only if data is allocated on the stack between the 'sigaction' call and the loop : #include <signal.h> #include <unistd.h> bool end = false; void handler(int) { end = true; } int main() { struct sigaction sigs; sigs.sa_handler = handler; sigaction(SIGI...

Python: convert swig object...

I am using SWIG to create a Python wrapper around a piece of C++ code which returns an object like this: TH1F*GetHist(); (The class TH1F has not been written by me.) Now under Python this results in an <Swig Object of type 'TH1F *' at 0x12da820> My problem is that I would like to use methods of this object which SWIG does not know ...

Protocol buffer polymorphism

I have a C++ program that sends out various events, e.g. StatusEvent and DetectionEvent with different proto message definitions to a message service (currently Active MQ, via activemq-cpp APU). I want to write a message listener that receives these messages, parses them and writes them to cout, for debugging purposes. The listener has s...

Can a STL map be used with keys of varying sizes

Can a STL map be used for keys of varying sizes? I don't have code for this. I'm still trying to figure out if this can be done and hence my question. (I'm the type that can spend too much time on an impossible problem. I'm hoping to learn from your wisdom). I am working on a look up table that essentially has two keys. A numeric type ...

Factory pattern and class templates in C++

I have a hierarchy of class templates. At the top of the hierarchy is an abstract base class (interface). I won't know which concrete implementation to instantiate until runtime, so it seems like the perfect situation to use the factory pattern. However, virtual member function templates are not allowed in C++. How can I achieve a le...

C++ Project Structure

I'm now involved with a C++ project, and I've never developed in C++ before. The question I have is the structure of projects that are multi-platform. One developer is working in VC++ and another in XCode. XCode doesn't have literal directories, they're logical (groups) directories. Should big C++ projects have actual folders, and if...

overloaded cast operator or single argument constructor

If a class has a single argument constructor my understanding is it is implicitly convertible by the constructor to the type of the argument in appropriate contexts. Defining a conversion operator also makes a class convertible to another type. Questions Does the conversion operator ever get called implicitly? If both a single argume...

C++ doubles: dividing by 100 causes very small error

I'm having a problem with the specific set of values in this code snippet. double inputs[] = {0, -546543, 99015, 6750, 825, 2725, 70475, 50950, 42200, 6750, 26925, 16125, 134350, 10075, 79378}; double result = 0; for (int i = 0; i < 15; i++) { result += inputs[i]/100; } I expected the final value of result to be 0. And if I t...

Named namespace in implementation file to group const string literals - Good/Bad?

I've grouped several message strings into a named (non anonymous) namespace in the .cpp file for a class handling output as seen in the code below: namespace Messages { static const std::string AppTitle = "The Widgetizer - Serving all your Widget needs"; static const std::string SuccessMsg = "Great success! Widgets for all! "; sta...

Reading bits in a memory

If I have a pointer to the start of a memory region, and I need to read the value packed in bits 30, 31, and 32 of that region, how can I read that value? ...

CString variable name prefix

Easy question for you vets out there: What is the accepted (assuming there is one...) prefix for a CString variable name? For clarification, I've seen the following for other data types: int iIndex; //integer int* pIndex; //pointer bool fFlag; //bool flag And there are countless others. Please feel free to let me know if this ...

Are tokens after #endif legal?

I currently do the following and the compiler (MSVC2008 / as well as 2010) doesn't complain about it but I'm not sure if it's a bad idea or not: #ifndef FOO_H_ #define FOO_H_ // note, FOO_H_ is not a comment: #endif FOO_H_ I used to always write it as #endif // FOO_H_ but I caught myself not doing that today and thought it was strang...

Is a WPF application managed code only?

I want to use WPF in an app. I want to write it in C++. Does the application have to be managed? I know that I can mix managed with unmanaged. I'm wondering if I can have the whole application be unmanaged. ...

Programmatically search + replace in a .doc

If I'm given a .doc file with special tags in it such as [first_name], how do I go about replacing all occurrences of it with something like "Clark"? A simple binary replacement only works if the replacement string is the exact same length. Haskell, C, and C++ answers would be best, but any compiled language would do. I'd also prefer to...

"operator char*" issue.

Below code expected to print "kevin" But, it's printing garbage value. I have checked in debugger. The pointer returned by "operator char*" call is invalid. Any idea? class Wrapper { private: char* _data; public: Wrapper(const char* input) { int length = strlen(input) + 1; _data = new char[length]; ...

Using a static initializer function

In this contrived example, I have a static initialization function that is run at construction time. I'd like to know if this a legal way to initialize a_, b_, and c_. Or, is it too early in the construction process to use them this way? DWORD SomeInitializationFunction( HANDLE*, DWORD*, DWORD* ); class MyClass { public: MyClass()...

Weird output for bitwise NOT

I am trying to take one's complement of 0 to get 1 but i get 4294967295. Here is what i have done: unsigned int x = 0; unsigned int y= ~x; cout << y; My output is 4294967295 but I expect 1, why is this so?. By the way, i am doing this in C++. ...

Detect drive letter of SD card hardware

Is there a way to programatically detect the driver letter of an SD card(s) on Windows? Does the method support internal and external SD card hardware? Thank you for your time. ...

Getting image contents of obstructed window

Is it possible to get image contents of an obstructed window without bringing it to the front? Also, is it possible to send mouse clicks to a specific locations of such window using Windows API? Would the realization be different on Windows XP and Windows 7? What functions I would need and where can I read more about that. Thanks. ...

GLUTesselator for realtime tesselation?

I'm trying to make a vector drawing application using OpenGL which will allow the user to see the result in real time. The way I have it set up is with an edge flag callback so the glu tesselator only outputs triangles which I then pass to a VBO. I'v tried t make all my algorithms as fast as possible and this is not where my issue is. Ac...