c++

Terminate all (grand)children when terminating a child process

I will jump right in, to be brief and descriptive: C++, Windows API I am creating child processes using CreateProcess to run external (command-line) applications. I have built in a time-out, and if the child process has not returned normal execution by that time, I wish to force termination on that child process. Ideally, I would lik...

static array allocation issue!

I want to statically allocate the array. Look at the following code, this code is not correct but it will give you an idea what I want to do class array { const int arraysize; int array[arraysize];//i want to statically allocate the array as we can do it by non type parameters of templates public: array(); }; array::array():array...

How do you return a 2d array in C++?

How do I return a 2d array in C++? For example, I have the following method in java: public static int[][] getFreeCellList(int[][] grid) { // Determine the number of free cells int numberOfFreeCells = 0; for (int i=0; i<9; i++) for (int j=0; j<9; j++) if (grid[i][j] == 0) numberOfFreeCells++; /...

Basic C++ Idioms / Techniques

Note: marked as community wiki. In recent days, I've realized how little I know about C++. Besides: using the STL implementing RAII implementing ref-counted smart pointers writing my own policy-based template classes overloading operators << for fun What other techniques are must-know for a good C++ programmer? Thanks! ...

Windows game: UTF-8, UTF-16, DirectX and Lua

I'm developing a game for windows for learning purposes (I'm learning DirectX). I would like it to have UTF support. Reading this question I learned that windows uses wchar_t, which is UTF-16. I want my game to have Lua scripting support, and Lua doesn't really like Unicode much.. It simply treats strings as a "stream of bytes"; this wo...

Can C++ policy classes be used to specify existence / non-existence of constructors?

Suppose I have: struct Magic { Magic(Foo* foo); Magic(Bar* bar); }; Is there a way to make Magic a template, and define template classes s.t. typedef Magic<FooPolicy, ...> MagicFoo; typedef Magic<BarPolicy, ...> MagicBar; typedef Magic<..., ...> MagicNone; typedef Magic<FooPolicy, BarPolicy> MagicAll; s.t. MagicFoo & MagicAll h...

Finding the "Nth node from the end" of a linked list.

This seems to be returning the correct answer, but I'm not sure if this is really the best way to go about things. It seems like I'm visiting the first n nodes too many times. Any suggestions? Note that I have to do this with a singly linked list. Node *findNodeFromLast( Node *head, int n ) { Node *currentNode; Node *behindCu...

How to find the time taken to send data across UDP

I have a simple UDP client server written in C++ on Ubuntu 9.10 where the client sends a set to the server. How can I check how much time s it taking to sent it. I need to find the time from start of transfer to end. Supposing my server and client are on the same machine then can I somehow save the system time and find the difference or...

How to make the Win32 APP Background Transparent?

How to make the Win32 APP Background Transparent? in C++ i want to make the background of the program to look like the desktop picture. ...

How can I make a banner in QT, like the news banner of CNN/FOX?

Hi, as the title says, I want to add my main window a banner in the bottom of the window that shows text that runs from left to right, just like you can see in your TV when watching fox/CNN... I'm using QT 4.5.2 Thanks a lot :) ...

What does this dynamic allocation do?

Today, I found out that you can write such code in C++ and compile it: int* ptr = new int(5, 6); What is the purpose of this? I know of course the dynamic new int(5) thing, but here i'm lost. Any clues? ...

How to get data out of the STL's const_iterator?

I have something that runs like this: T baseline; list<T>::const_iterator it = mylist.begin(); while (it != mylist.end()) { if (it == baseline) /* <----- This is what I want to make happen */ // do stuff } My problem is that I have no idea how to extract the data from the iterator. I feel like this is a stupid thing to be con...

Another algorithm problem...

I am given N numbers and for them apply M rules about their order. The rules are represented in a pairs of indexes and every pair (A, B) is telling that the number with index A (A-th number) must be AFTER the B-th number - it doesn't have to be next to him. Ex: N = 4 1 2 3 4 M = 2 3 2 3 1 Output: 1234, 4213, 4123, 2134,...

reference counting with cycles in C++ smart pointer

Hi In shared_ptr smart pointer, reference counting is used. However, reference counting has a problem, that it can't break cycles of reference. I have four questions with this issue. 1) Could anybody offer me one snippet in which the cycles of reference happened? 2) If it can't break cycles of reference, how does RCSP guarantee succ...

Confused on C++ casting

I have been reading a lot about C++ casting and I am starting to get confused because I have always used C style casting. I have read that C style casting should be avoided in C++ and that reinterpret_cast is very very dangerous and should not be used whenever there is an alternative. On the contrary to not using reinterpret_cast, I hav...

Boost Regex Find host/domain name

hello. I'm very new to c++ and boost. I'm trying to get the host name of a given url: this is what I have now: int main() { string url = "http://www.amazon.com/gp/product/blabla"; //Regular Expression from Javascript. boost::regex ex("/^((\w+):\/\/\/?)?((\w+):?(\w+)?@)?([^\/\?:]+):?(\d+)?(\/?[^\?#;\|]+)?([;\|])?([^\?#]+)?\??([^#]...

C++ - Two Basic Questions.

1) I checked out the C++ recommended reading list. I'm probably still a beginner. So, would I still start with a beginner book, and go from there? And, do you recommend reading one book for each level, or all of the books in one level before moving up? Obviously, I can only start on one book. Which one should I begin with? 2) What kind...

Calling Lua function without executing script

I am embedding Lua into a C/C++ application. Is there any way to call a Lua function from C/C++ without executing the entire script first? I've tried doing this: //call lua script from C/C++ program luaL_loadfile(L,"hello.lua"); //call lua function from C/C++ program lua_getglobal(L,"bar"); lua_call(L,0,0); But it gives me this: ...

why implicit conversion is harmful in C++

Hi, I understand that the keyword explicit can be used to prevent implicit conversion. For example Foo { public: explicit Foo(int i) {} } My question is, under what condition, implicit conversion should be prohibited? Why implicit conversion is harmful? ...

C++ templated class implementation of the multiton pattern

I implemented the multiton pattern using a templated class in C++. #ifndef MULTITON_H #define MULTITON_H #include <map> template <typename Key, typename T> class Multiton { public: static void destroy() { for (typename std::map<Key, T*>::iterator it = instances.begin(); it != instances.end(); ++it) { de...