c++

Initializing a char array in C. Which way is better?

The following are the two ways of initializing a char array: char charArray1[] = "foo"; char charArray2[] = {'f','o','o','\0'}; If both are equivalent, one would expect everyone to use the first option above (since it requires fewer key strokes). But I've seen code where the author takes the pain to always use the second method. My...

what median_unsafe in C++ mean

Hi, I run into a C++ code: vector<double> PITs; if(PITs.size() > 0) PIT = util::median_unsafe(PITs); What is mean by util::median_unsafe, why I cant find it in C++ reference? thanks ...

Change local time by C++ program

Now in my country the local time is: 3:43 PM Can I write program in C++ to change the time on a Windows 7 system to another time? For example set it to 5:00 PM (increase) or decrease it? ...

Nested templates and constructor

Hi everybody! Edit: Note that my final purpose here is not having the class working, is just learning more about templates :-) Suppose you have a template class which implements a vector: template <typename T> class Vector { public: Vector(size_t dim) { dimension = dim; elements = new T[dim]; ...

Is a C++ program really slower than a similar C program?

Assume that i have written a program in C++ without using RTTI and run-time polymorphism (no virtual function, no virtual inheritance) and classes don't have private/protected members, also the C++ specific header files are not used (i.e. C header files are used: cstring, cstdio, ... instead of string, iostream, ...). Then i want to wri...

Cocktail Sort code segfaults - not sure why.

Hi there. I wrote a Cocktail Sort algorithm, and am testing it by generating random vectors of size 500 up to 10,000 - running it 10 times per vector. After about the 2000-3000 length vector mark, the code segfaults. I expect it isn't the test code as the same test is used for multiple sorting algorithms and it runs fine for every other ...

Difficulty getting GDB to load debugging symbols

I use GDB to debug C/C++ programs very often, and I'm reasonably knowledgeable with how it works and what it can do. However, every so often I run into problems where mysteriously I can't seem to get GDB to properly load symbols from a core file. Currently, I have a binary executable in a shared NFS directory. The executable was def...

Ensure all key events get sent to main window?

Is there a way to ensure that all WM_KEYDOWN events find their way into my main window regardless of who has focus? this is mainly for global things such as Delete, and hotkeys such as CTRL A and CTRL S. The problem is if another control has focus, all of these stop working. Is there maybe a better way of doing this than putting them in ...

Erasing multiple objects from a std::vector?

Here is my issue, lets say I have a std::vector with ints in it. let's say it has 50,90,40,90,80,60,80. I know I need to remove the second, fifth and third elements. I don't necessarily always know the order of elements to remove, nor how many. The issue is by erasing an element, this changes the index of the other elements. Therefore,...

making a SingletonMixin class in c++

Hi guys: I have four classes, let's call S1, S2, S3 and S4. These class are singletons; each one have a getInstance and a finalize method - and an instance private variable-. Now, to avoid repeting the finalize and getInstance methods I'm trying to make a SingletonMixin class, something like: template<class T> class SingletonMixin { ...

error: pointer being freed was not allocated

I am trying to overload the assignment operator to do a deep copy of a polygon object, the program compiles but I am getting an error toward the end that I want to clear up. Below is the relevant code, if you think I need to add more please just post a comment. Assume the proper #include's and that the << operator is overloaded for prope...

Modern CPU Inner Loop Indirection Optimizations

From http://www.boost.org/community/implementation_variations.html "... coding differences such as changing a class from virtual to non-virtual members or removing a level of indirection are unlikely to make any measurable difference unless deep in an inner loop. And even in an inner loop, modern CPUs often execute such competing code s...

Drawing text with C++ directx

I'm creating an application overlay via Direct3D hooking, however I can't draw any text. I've started with this sample. The library itself seems to replace all D3D calls with own functions (and in the end it calls the original ones). I've tried all variations of DrawText without any result visible result. On the other hand, I can draw pr...

Pointer to the start of an object (C++)

Hello, I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance. I found this article which describes a way to do it (see the section called "Dynamic Casts") using typeid and a dynami...

How to compile for 32bit with Eclipse

I'm currently writing a little program in c++ on my 64bit Ubuntu Pc. By default eclipse compiles the program for a 64bit architecture. Since I want to use my little program on my server which is still 32bit, I need to be able to compile my program for 32bit. How could can I do that in eclipse? I've been fiddling for a while with the sett...

Integer vs floating division -> Who is responsible for providing the result?

I've been programming for a while in C++, but suddenly had a doubt and wanted to clarify with the Stackoverflow community. When an integer is divided by another integer, we all know the result is an integer and like wise, a float divided by float is also a float. But who is responsible for providing this result? Is it the compiler or D...

Mergesort - std::bad_alloc thrown when trying to assign vector.

Good afternoon ladies and gents. So, it is not my day for errors. Implementing Mergesort (not in-place) in C++, and I'm having real trouble with the code, with no idea why. The second-to-last line of the mergeSort() function assigns the result of the merge() to a vector of ints, result. This line (the actual allocation, not the function)...

How to send shortcut to another application in c++ in MS Windows?

My question is how to send shortcut from c++ to another application. Let's say for example that the notepad is open, and I want send to them shortcut like CTRL+P, or more complexive shortcut like CTRL+SHIFT+HOME, or ALT+F4, I find many good tutorials that explained how to send one key like 'A' or 'ALT', but I did not find how to send it ...

need advice for type of TCP server to cater for this type of application.

The requirement of the TCP server: receive from each client and send result back to same client (the server only do this) require to cater for 100 clients speed is an important factor, ie: even at 100 client connections, it should not be laggy. For now I have been using C# async method, but I find that I always encounter laggy at...

Overriding a templated class function

I'm trying to create some kind of callback for a class template. The code is like this: template <typename t> class Foo { void add(T *t) { prinf('do some template stuff'); on_added(t); } void on_added(T *t) { } } struct aaa {} class Bar : Foo<aaa> { void on_added(aaa *object) { printf("on added ...