c++

Cleanest way to combine two shorts to an int.

I have two 16-bit shorts (s1 and s2), and I'm trying to combine them into a single 32-bit integer (i1). According to the spec I'm dealing with, s1 is the most significant word, and s2 is the least significant word, and the combined word appears to be signed. (i.e. the top bit of s1 is the sign.) What is the cleanest way to combine s1 an...

ReceiveOneOrMore returns wrong length in Symbian

Hi I am doing some socket stuff on Symbian, which works fine so far. However, I am facing a weird problem when trying to read out the data that has been sent. Assume the code looks as follows: TSockXfrLength len; iSocket.RecvOneOrMore( buff, 0, iStatus, len ); User::WaitForRequest(iStatus); if (iStatus == KErrNone) { print...

Queue that uses a Stack

I am having trouble understanding a question. The question asks first to write a C++ class to represent a stack of integers. Done. Here are my prototypes: class Stack{ private: int top; int item[100]; public: Stack() {top = -1;} ~Stack(); void push(int x) {item[++top] = x;} int pop() {return item[top--];} int...

Transparent window containing opaque text and buttons

I'm creating a non-intrusive popup window to notify the user when processing a time-consuming operation. At the moment I'm setting its transparency by calling SetLayeredWindowAttributes which gives me a reasonable result: However I'd like the text and close button to appear opaque (it doesn't quite look right with white text) while k...

Can I convert the floating part of a double into an integer (without using string conversions)?

Example: If I have 7.2828, I just want to get the 2828 as an integer. ...

What is the correct way to instantiate an object with an allocator?

I have implemented a custom allocator (to be used by STL containers within my memory debugging utility, without them using my overridden new operator). Within the memory debugger I use an instance of the same allocator class to allocate the objects I need to keep track of 'normal' memory allocations. It's all working fine, but I'm not su...

C++ IP Address human-readable form

In C/C++, you can use the regular gethostbyname() call to turn a dotted-IP address string ("127.0.0.1" in the case of localhost) into a structure suitable for standard socket calls. Now how do you translate it back? I know I can do some bit-shifting to get exactly which bit sets I want and just print those out, but is there any "standa...

behavior of bool with non-boolean operators

What I really want is a ||= operator. old_value = old_value || possible_new_value; old_value ||= possible_new_value; The second line is a compiler error (c++ doesn't have a ||= operator). So what are my other options? old_value += possible_new_value; old_value |= possible_new_value; While I'm on the subject how does bool behave wi...

Array of Pointers

How can I get an array of pointers pointing to objects (classes) ? I need to dynamically allocate space for them and the length of array isn't determined until run-time. Can any one explain and tell me how to define it? and possibly explain them how it works, would be really nice :) ...

Header dependencies in qmake using MSVC Express

Hi, I'm using QtCreator on windows using MSVC compiler (the compiler from Visual c++ express edition) and qt 4.5.2 open source. When I modify a header on the project and press build all, nothing is actually built, only If I modify a .cpp file the modified cpp is compiled. That causes that every time that I have to change some header f...

Compile and optimize for different target architectures

Summary: I want to take advantage of compiler optimizations and processor instruction sets, but still have a portable application (running on different processors). Normally I could indeed compile 5 times and let the user choose the right one to run. My question is: how can I can automate this, so that the processor is detected at runt...

How to get the end of a C++ string stream?

I'm adding functions to my (simple) log class to make it usable like a stream. Currently, after some modifications, I got this (in my cpp): // blah blah blah... // note: here String is a defined as: typedef std::string String; void Log::logMessage( const String& message ) { logText(); // to be sure we flush the current text if...

getsockopt sensible for SOCK_STREAM + BTHPROTO_RFCOMM ?

I'm connecting to a bluetooth socket like so, SOCKET s = socket(AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM); SOCKADDR_BTH bthSockAddr; bthSockAddr.btAddr = addr; bthSockAddr.addressFamily = AF_BTH; bthSockAddr.port = BT_PORT_ANY; bthSockAddr.serviceClassId = SerialPortServiceClass_UUID; if ( 0==connect( sOut, (sockaddr*)&bthSockAddr,sizeof...

Getting a compile error : 0x2 trying to open file <vfdmsg>.

I'm trying to build http://chitchat.at.infoseek.co.jp/vmware/vfd.html (VS 2008, Windows Server 2008 x64) however I'm getting the following error messages: Error 1 error : 0x2 trying to open file <vfdmsg>. mc lib Error 2 error PRJ0019: A tool returned an error code from "Compiling Message - L:\src\lib\vfdmsg.mc" lib lib Error 3 error : 0...

How to implement scoped iostream formatting?

I'd like to scope-limit the effect of I/O stream formatting in C++, so that I can do something like this: std::cout << std::hex << ... if (some_condition) { scoped_iofmt localized(std::cout); std::cout << std::oct << ... } // outside the block, we're now back to hex so that base, precision, fill, etc. are restored to their previo...

What is the difference between NULL, '\0' and 0

In C, there appear to be differences between various values of zero -- NULL, NUL and 0. I know that the ASCII character '0' evaluates to 48 or 0x30. The NULL pointer is usually defined as: #define NULL 0l In addition, there is the NUL character '\0' which seems to evaluate to 0 as well. Are there times when these three values can n...

Boost best practices?

I've used Boost graph library a fair amount but not much of the rest of it. I frequently see recommendations here to use parts of Boost (say, Boost's various smart pointers). Obviously Boost is good and good to use. It is also large or diverse. Does anyone know of a FAQ or decent best practices doc to help a knowledgeable C++ programmer ...

Function template specialization with reference to pointer.

I have a template function: template<typename T> void foo(const T& value) { bar(value); x = -1; } I want to specialize it for a set of types: template<> void foo<char>(const char& value) { bar(value); x = 0; } template<> void foo<unsigned char>(const unsigned char& value) { bar(value); x = 1; } It works ok. When I compile this: te...

How to create binary/hex dump of another process's memory?

I am having trouble finding a reasonable way to dump another process's memory to a file. After extensive searching, I've been able to find a nice article at CodeProject that has *most* of the functionality I want: Performing a hex dump of another process's memory. This does a good job of addressing permission issues and sets a good fou...

C++: Callback typedefs with __stdcall in MSVC

This typedef: typedef DWORD WINAPI (* CM_Open_DevNode_Key)(DWORD, DWORD, DWORD, DWORD, PHKEY, DWORD); compiles fine in BorlandCpp, however, when I compile it in msvc I have to remove WINAPI (which is just an alias for __stdcall): typedef DWORD (* CM_Open_DevNode_Key)(DWORD, DWORD, DWORD, DWORD, PHKEY, DWORD); Why is this happening?...