c++

Derived Functor with any return type and any parameters

I have a class that uses functors as units of work. It accepts a reference to a functor in its Run() method. To allow this class to operate on any functor, all these functors must derive from my base functor class which looks like this: class baseFunctor{ public: virtual void operator()()=0; virtual baseFunctor Clone()=0; }; ...

Rebind a socket to a different interface

Is there an existing Linux/POSIX C/C++ library or example code for how to rebind a socket from one physical interface to another? For example, I have ping transmitting on a socket that is associated with a physical connection A and I want to rebind that socket to physical connection B and have the ping packets continue being sent and re...

Compelling examples of custom C++ STL allocators?

What are some really good reasons to ditch the standard STL allocators for a custom solution? Have you run across any situations where it was absolutely necessary for correctness, performance, scalability, etc? Any really clever examples? Custom allocators have always been a feature of the STL that I haven't had much need for. I was jus...

How to make NUnit assertion failures show line numbers for C++?

When I run NUnit tests against my C++ code and an assertion fails, I don't get line numbers for where the failure occurs. Sample Method: [Test] void testMethod() { Assert::Fail("test comment"); } Sample output: [nunit2] Failures: [nunit2] 1) namespace.SomeTest.testMethod: test comment [nunit2] at namespace.SomeTe...

What data type does memory see when I use void?

When I create a method of type int the compiler reserves X number of bits in memory. So how does the see a void type? How many bits/bytes does a void type take up? ...

Why does my program consume 100% CPU under nVidia NView?

I was recently working on a windows program that would sometimes become unresponsive when scrolling through a large list of items in a production environment. Of course it works fine on my desktop. The production Environment is: Windows XP based Workstation with 2 monitors nVidia Video Drivers with nView enabled Of note is a Dr watso...

How do I store arrays in an STL list?

Using C++ and the STL, does anybody know how to store integer arrays as nodes in an STL list or vector? I have an unknown number of pairs of numbers that I need to store, and coming from other languages my first thought was to use some sort of list- or vector-like data structure... but I'm running into some trouble. I am 100% sure that I...

SQLite - pre allocating database size...

Is there a way to pre allocate my SQLite database to a certain size? Currently I'm adding and deleting a number of records and would like to avoid this over head at create time. ...

off-by-one error with string functions (C/C++) and security potentials

So this code has the off-by-one error: void foo (const char * str) { char buffer[64]; strncpy(buffer, str, sizeof(buffer)); buffer[sizeof(buffer)] = '\0'; printf("whoa: %s", buffer); } What can malicious attackers do if she figured out how the function foo() works? Basically, to what kind of security potential pr...

Virtual Default Destructors in C++

Hello all :) I've got a large set of inherited classes (criteria) which inherit from a base class (criterion). Here's criterion's code class criterion { public: virtual unsigned __int32 getPriorityClass() const = 0; virtual BOOL include(fileData &file) const = 0; virtual void reorderTree() = 0; virtual unsigned int dire...

What is the STL?

I'm not a C++ programmer and have difficulty understanding the explanations given on websites. I don't understand containers or iterators and don't have plans to learn C++ in the near future. So in layman's terms: What is the STL and what can it do for me? How does it compare to something like the Python Standard library or glibc? ...

Default value for bool in C++

I'm redesigning a class constructor in C++ and need it to catch an unspecified bool. I have used default values for all of the other parameters, but from my understanding bool can only be initialized to true or false. Since both of those cases have meaning in the class, how should I handle checking for change from a default value? ...

Explicitly calling a constructor in C++

Hi I know we can explicitly call the constructor of a class in C++ using scope resolution operator i.e. className::className() I was wondering where exactly would I need to make such a call. cheers ...

C/C++ Windows API sending text to clipboard

This code is supposed to send a string to the clipboard. However I got it to work once. Now it does not come up correctly when I CTRL+V. But when I use this snippet to identify the clipboard text it shows what it should be. #include <windows.h> #include <iostream> BOOL SetClipboardText(LPCTSTR pszText) { BOOL ok = FALSE; if(O...

Given filename, how can I get the Adler32 using Crypto++

Given a "string filename", how can I get the Adler32 checksum using the C++ Crypto++ library. I am a little confused about using their FileSource and Sink system. Below I have the skeleton of the code that does MD5, but I can't seem to find any examples or tutorials on the Adler32 usage. string filename = "/tmp/data.txt" string file_ad...

Unmanaged lib in managed executable causing managed exceptions

Hi everyone, I'm having a problem with mixing managed and unmanaged code. I have created two projects under a single solution in Visual Studio 2008 under Vista x64 SP1. One of them does not have CLR support and is a static library. My second project is compiled as an executable with CLR enabled. It depends on the first static library, an...

Calculating e^x without using any functions

We are supposed to calculate e^x using this kind of formula: e^x = 1 + (x ^ 1 / 1!) + (x ^ 2 / 2!) ...... I have this code so far: while (result >= 1.0E-20 ) { power = power * input; factorial = factorial * counter; result = power / factorial; eValue += result; counter++; iterations++; } My problem now is tha...

Why doesn't Java have a copy constructor?

Why doesn't Java support a copy constructor like in C++? ...

Using SubclassDlgItem to change control types

I have a C++ MFC app with a dialog where I want to change the type of the control dynamically based on the selection in a combo box. The dialog resource starts off with a plain old edit control which I then call SubclassDlgItem on to change to a custom control type. So far so good. Now, when the user changes the selection in a differe...

Pattern to specialize templates based on inheritance possible?

So, one problem pattern that I keep coming across and don't have a good solution for is how to provide template specializations that are based in what type a template parameter is derived from. For example, suppose I have: template<typename T> struct implementPersist; template<typename T> void persist( T& object ) { implementPersist...