c++

Confusing function lookup with templates in C++

Starting with the following (using gcc version 4.0.1): namespace name { template <typename T> void foo(const T& t) { bar(t); } template <typename T> void bar(const T& t) { baz(t); } void baz(int) { std::cout << "baz(int)\n"; } } If I add (in the global namespace) struct test {}; void bar(co...

Programmatically get amount of RAM installed on OS X

I'm working on a machine that has 8 gigs of memory installed and I'm trying to programmatically determine how much memory is installed in the machine. I've already attempted using sysctlbyname() to get the amount of memory installed, however it seems to be limited to returning a signed 32 bit integer. uint64_t total = 0; size_t size = ...

Efficient computation of the high order bits of a multiplication

Many CPUs have single assembly opcodes for returning the high order bits of a 32 bit integer multiplication. Normally multiplying two 32 bit integers produces a 64 bit result, but this is truncated to the low 32 bits if you store it in a 32 bit integer. For example, on PowerPC, the mulhw opcode returns the high 32 bits of the 64 bit res...

Better seeds than time(0)?

I understand that time(0) is commonly using for seeding random number generators and that it only becomes a problem when the program is being run more than once per second. I'm wondering what are some better seeds to consider when generating random numbers. I read about GetTickCount, timeGetTime, and QueryPerformanceCounter on Windows. W...

How to overload operators with Boost.Python

I am trying to overload operators of a C++ class using Boost.Python. According to this, I am doing it the right way... but I have a bunch of compiler errors. Here is a simple example I made trying to pinpoint the problem: #include "boost/python.hpp" using namespace boost::python; class number { public: number(int i) : m_Number(i...

Visual C++ Precompiled Headers errors

Update: What are the effects of including stdafx.h in my header files? I started on a C++ project in Linux/Eclipse CDT and imported it into Visual C++/Windows. In Visual C++, I started using precompiled headers to speed up compilation and defined stdafx.cpp and stdafx.h. Here's my stdafx.h #pragma once #include <string> #includ...

Programatically get Google search results

How can I get Google search results from inside a program? I need to get an array of search results for a specified string. ...

How to draw text on the desktop in Windows ?

How Would I go about placing text on the windows desktop? I've been told that GetDesktopWindow() is what I need but I need an example. ...

Custom text color for certain indexes in QTreeView

I would like to draw texts in one of the columns in a QTreeView widget using a custom color (depending on the data related to each row). I tried to overload the drawRow() protected method and change the style option parameter like this (a stripped-down example): virtual void drawRow(QPainter* p_painter, const QStyleOptionViewItem& optio...

Implementing std::list item read/write events.

I'm new to c++ but have set my mind on a specific task that needs me to enable adding a specific chunk of code to be execute whenever any list item is attempted to be changed or read. The resulting list should behave and look as much as as possible to std::list except for this small exception that would enable me to execute a specific t...

Qt4 DNS Server using QUdpSocket

I am writing a DNS server for an application I'm working on and I'm running into some difficulties. I want to bind to port 53 on localhost and listen for DNS lookup requests, then send back the response. Unfortunately, when I call QUdpSocket::bind() the resulting socket is not writeable. Is there a flag I need to pass to make it so I ...

C pointer array scope and function calls

I have this situation: { float foo[10]; for (int i = 0; i < 10; i++) { foo[i] = 1.0f; } object.function1(foo); // stores the float pointer to a const void* member of object } object.function2(); // uses the stored void pointer Are the contents of the float pointer unknown in the second function call? It seems ...

How to get the digits of a number without converting it to a string/ char array?

How do I get what the digits of a number are in C++ without converting it to strings or character arrays? ...

is there any database transaction mechanism in MFC/C++?

I want to make sure that if any error occurs during the database processing phase, program will know it need to roll back the whole process. any good ORM in MFC/C++ for doing this ? ...

Get original SQL query from prepared statement in SQLite

I'm using SQLite (3.6.4) from a C++ application (using the standard C api). My question is: once a query has been prepared, using sqlite3_prepare_v2(), and bound with parameters using sqlite3_bind_xyz() - is there any way to get a string containing the original SQL query? The reason is when something goes wrong, I'd like to print the qu...

c.erase(p) can't be compiled successfully, why?

I had tried to test the erase function in list, but I can't succeed! The error message of my compiler is: [Error]error: no matching function for call to `std::list<int,std::allocator<int> >::erase(std::_List_const_iterator<int>&)'. My code as follows: #include <iostream> #include <list> using namespace std; int main() { int ia[]...

How to save and load a C++ application state in a modular way

I have a distributed C++ application, which is composed of 4 processes spread on 2 machines. One of the applications serves as "control center" for the rest of the applications. I want to be able to save the current state to a file and load it again later. What exactly is a "state" is defined separately by each module in the system. Wh...

How to reconstruct a data-structure from injected process' memory space?

Dears, I've got this DLL I made. It's injected to another process. Inside the other process, I do a search from it's memory space with the following function: void MyDump(const void *m, unsigned int n) { const char *p = reinterpret_cast(m); for (unsigned int i = 0; i < n; ++i) { // Do something with p[...

[C++] How do I implement a "single instance"-like design?

Hi, I'm writing an application which will run as a daemon. UIs will connect to it over TCP. Now, there is a class called UiTcpInterface which will handle all communication between the UI and this daemon. Now, I'm faced with the problem of ensuring there is only one instance of UiTcpInterface. What would be the best way to do it? Curren...

sprintf_s problem

I have a funny problem using this function. I use it as follow: int nSeq = 1; char cBuf[8]; int j = sprintf_s(cBuf, sizeof(cBuf), "%08d", nSeq); And every time I get an exception. The exception is buffer to small. When I changed the second field in the function to sizeof(cBuf) + 1. Why do I need to add one if I only want to copy 8 by...