c++

Help with semi-complex C++ assignment

This is a really easy question I'm sure but I'd appreciate the help. :) Here's my variable in the .h file: map<int, map<int, map<int, CString>*>*> batch; Here's me trying to assign a value: ((*((*(batch[atoi(transnum)]))[1]))[atoi(*docnum)]) = page; I added some extra parentheses while trying to figure this out in order to make su...

sizeof on a class inheriting from a base class with a virtual function

For the following code fragment. /*This program demonstartes how a virtual table pointer * adds to a size of a class*/ class A{ }; class X{ public: void doNothing(){} private: char a; }; class Z:public X { public: void doNothing(){} private: char z; }; class Y{ public: ...

Determine total number of bytes read by a process.

I am currently working on a project where one of our goals is to reduce the total amount of data read from disk. Is there a way to determine the total number of bytes run by a process? I am working with on a C++ application built with Visual Studio 2005, running on Windows XP. Ideally, I would like some sort of monitor that can print re...

linked list with no duplicates

I have the following code (correct for my simple tests) for a linked list for no duplicates, but I think it is a bit ugly. Could anyone recommend a cleaner way to handle the duplicate code? The current piece in question is: if( (val == cur->val) || (cur->next && (val == cur->next->val)) ) But I think that a better solution might exi...

Writing to default-constructed fstream: Undefined Behavior?

Consider the following: std::basic_fstream<char> testfile; testfile.write(reinterpret_cast<const char*>(&someInt), sizeof(int)); testfile.close(); This runs with no complaint when built with VC 8.0, but crashes when built with VC 10.0 beta. I have some legacy code that actually relies on the VC 8 behavior, where we inherit from basic...

Performance of comparisons in C++ ( foo >= 0 vs. foo != 0 )

Hi there, I've been working on a piece of code recently where performance is very important, and essentially I have the following situation: int len = some_very_big_number; int counter = some_rather_small_number; for( int i = len; i >= 0; --i ){ while( counter > 0 && costly other stuff here ){ /* do stuff */ --coun...

Initializing arrays in C++

I'm trying to initialize the last element in the array int grades[10]; to grade 7 but it doesn't seem to work I'm using C++ btw ...

Why doesn't this C++ template code compile?

Does anyone know why this will not compile? I've tried both VS 2008 and GCC 4.something and both spit out errors. It doesn't matter whether or not I'm referencing "ThisFunctionDoesNotCompile()". I can workaround this by just passing 'InternalType' as a second template parameter to Base, but I'm still curious why this comes up as an er...

global variable not printing correctly in visual studio

I am playing around with some recursion, and trying to count the number of additions in a recursive fib function (code below). The problem is, g_add never gets printed as anything but zero (0). In the debugger it is set correctly, but it wont print correctly. In fact, the code below is modified a bit, to sanity check that everything else...

NS_ERROR_XPC_GS_RETURNED_FAILURE error code

I'm developing extension for Firefox which calls XPCOM component writen in C++ and I get this error: [Exception... "Component returned failure code: 0x80570016 (NS_ERROR_XPC_GS_RETURNED_FAILURE) [nsIJSCID.getService]" nsresult: "0x80570016 (NS_ERROR_XPC_GS_RETURNED_FAILURE)" location: "JS frame :: chrome://testtest/content/mytest.js ...

Should the "this" pointer and smart pointers be mixed?

How should I avoid using the "this" pointer in conjunction with smart pointers? Are there any design patterns/general suggestions on working around this? I'm assuming combining the two is a no-no since either: you're passing around a native pointer to a smart pointer-managed object which defeats the point of using the smart pointers ...

How should smart pointers get down casted?

Do smart pointers handle down casting, and if not what is a safe way of working around this limitation? An example of what I'm trying to do is having two STL vectors (for example) containing smart pointers. The first contains smart pointers to a base class while the second contains smart pointers to a derived class. The smart pointers...

this compiles without a warning in VC9 at warning level 4. Why would one NOT consider this a compiler defect?

I saw some posted code with an out of range error on SO that made me wonder. I would expect a compiler to generate a warning (at the highest level at least) for this code #pragma warning(push,4) int main(){ int x[2]; x[2]=0; return 0; } #pragma warning(pop) but it does not. The EDG compiler nicely says: "sourceFil...

keep cmd open while running a file

I'm learning C++ and I'm using Visual C++ Express and while running this #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } the cmd window closes so fast, I can't see Hello World is there anyway to prevent this? ...

Print to console without flooding in C++

My apologies for an inaccurate title, but I'm not sure what this is called exactly. How would one print to the console a single, updating line? For example, if I wanted to print a percent completion status every cycle but not flood the console with steams of text, how would I accomplish this? (What is this called? -- for future Googlin...

Problem with HTML Agility Pack and Visual Studio C++

I am in need of a very simple HTML parser which can extract text, table from well-formed HTML documents in the .NET environment. I found several references to HTMLAgilityPack. My problem is that I am using the Visual C++ environment in the .NET framework. Can anyone help me with instructions on how do I add a "reference" to the C# genera...

Can't link libpqxx in MinGW

Using MSYS, I compiled libpq (from compiling postgres). I then compiled libpqxx. Now, I want to create a client that will use libpqxx. libpq seemed to work fine. And, I can compile code with libpqxx. However, linking the libpq client application fails. Here's my code: #include <pqxx/pqxx> #include <iostream> using namespace std; ...

pthread thread state

Is there a mechanism that I can use to tell if a pthread thread is currently running, or has exited? Is there a method for pthread_join() that is able to timeout after a specific period of time if the thread has not yet exited? ...

Winsocks Send and Receive

I'm using the WSAEventSelect I/O model in Windows Sockets and now I want to know that how may I know that my send and receive operations have sent and received all of the data? After I know that, how should I design a way so that it sends the data fully? Any examples would be really appreciated. Here is my code (not mine actually samp...

making an exe low priority

Hi, how do i run a .exe as low priority? i know i can go to task manager and change the priority setting there manually, but is there a way that i can launch the .exe from a .bat file with a command to make the .exe run at a given priority (in this case low)? the .exe is a program that i've written in c++, can i set the priority in c++ ...