c++

Erasing a Char[]

Okay i am working on someone elses code. They do alot of this: char description[256]; description[0]=0; I know this would put a \0 in the first spot of the character array. But is this even a safe way to erase a string? Also visual studio keeps reporting memory leaks, and i've pretty much tied this done to the strings that are used. ...

AIX: Why does throwing an exception abort multithreaded code?

We get an abort when a C++ exception is thrown when running code compiled with -pthread. Platform: AIX 5.3 technical level 8 Compiler: gcc 3.4.6 Linker: AIX linker 1.65.2.4 Test Code: // exception.cpp #include <iostream> class MyException { public: MyException(){} virtual ~MyException(){}; }; void gTest() { t...

What is wrong with this tiny piece of mutex code?

// A Mutex allows threads mutually exclusive access to a resource. //----------------------------------------------------------------------- class Mutex { private: CRITICAL_SECTION m_mutex; public: Mutex() { InitializeCriticalSection(&m_mutex); } ~Mutex() { DeleteCriticalSection(&m_mutex); } void acquire() { Enter...

C++: Do static primitives become invalid at program exit?

Assume I have a function like this: MyClass &MyFunction(void) { static MyClass *ptr = 0; if (ptr == 0) ptr = new MyClass; return MyClass; } The question is at program exit time, will the ptr variable ever become invalid (i.e. the contents of that ptr are cleaned up by the exiting process)? I realize that this function leaks,...

Timers to measure latency

When measuring network latency (time ack received - time msg sent) in any protocol over TCP, what timer would you recommend to use and why? What resolution does it have? What are other advantages/disadvantages? Optional: how does it work? Optional: what timer would you NOT use and why? I'm looking mostly for Windows / C++ solutions, b...

Dead-lock issue created by 2 SQL connections, each using transactions, each working with a different table, with a foreign key constraint between the two tables

Environment: I'm working on a C++ application that uses SQL Native Client 9.0 to communicate with a SQL Server 2000 database. Scenario: 2 connections are opened to the DBMS Each connection is set to use transactions A query on Connection1 works with TableA A query on Connection2 works with TableB TableB has a foreign key constraint o...

Can I make Asynchronous ODBC Calls? Any reference materials?

Does ODBC support asynchronous calls? If it does, then can you tell me about any reference materials? My preferred language is C++. ...

C++ inserting a line into a file at a specific line number

I want to be able to read from an unsorted source text file (a record in each line), and insert the line/record into a destination text file by specifying the line number where it should be inserted. It will be determined where to insert the line/record into the destination file by comparing the incoming line from the source file to the...

Alternative to forward declarations when you don't want to #include

I usually, almost without thinking anymore, use forward declarations so that I won't have to include headers. Something along this example: //----------------------- // foo.h //----------------------- class foo { foo(); ~foo(); }; //----------------------- // bar.h //----------------------- class foo; // forward declaration cl...

Determine if two rectangles overlap each other?

Hi folks, I am trying to write a C++ program that takes the following inputs from the user to construct rectangles (between 2 and 5): height, width, x-pos, y-pos. All of these rectangles will exist parallel to the x and the y axis, that is all of their edges will have slopes of 0 or infinity. I've tried to implement what is mentioned i...

How do I get a list of files in a directory in C++?

How do you get a list of files within a directory so each can be processed? ...

Passing boost::any to results of boost::bind

I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type callFromAnyList(Bound b, list<any> p) { } Then, if I had some function: double myFunc(string s, int i) { return -3.0; } I could call it by doing something like this: list<any> p; p.push_back((string)"Hello"); p.push_back(7); doubl...

Cleaning up an STL list/vector of pointers

What is the shortest chunk of C++ you can come up with to safely clean up a vector or list of pointers? (assuming you have to call delete on the pointers?) list<Foo*> foo_list; I'd rather not use Boost or wrap my pointers with smart pointers. ...

Win32 Overlapped Readfile on COM Port returning ERROR_OPERATION_ABORTED

Ok, one for the SO hive mind... I have code which has - until today - run just fine on many systems and is deployed at many sites. It involves threads reading and writing data from a serial port. Trying to check out a new device, my code was swamped with 995 ERROR_OPERATION_ABORTED errors calling GetOverlappedResult after the ReadFil...

Forward declare an STL container?

Is it possible to forward declare an STL container in a header file? For example, take the following code: #include <vector> class Foo { private: std::vector<int> container_; ... }; I want to be able to do something like this: namespace std { template <typename T> class vector; } class Foo { private: std::vector<in...

g++ undefined reference to typeinfo

I just ran across the following error (and found the solution online, but it's not present in Stack Overflow): (.gnu.linkonce.[stuff]): undefined reference to [method] [object file]:(.gnu.linkonce.[stuff]): undefined reference to `typeinfo for [classname]' Why might one get one of these "undefined reference to typeinfo" lin...

C++ Problem : "Error external 'C::C()' referenced from C:\C++\CRP.OBJ" ....

Hi, I'm trying to get this: //C.h #ifndef C_H #define C_H #include "c.h" class C { public: C(); int function(int, int); }; #endif which is defined in this: //c.cpp #include "c.h" C::C() { } int C::function(int a, int b) { return a * b; } to work in this: //crp.cpp #include <iostream> #include "c.h" void main(vo...

What is the strtok_s equivalent in VC7?

The strtok_s function exists in vc8 but not in vc7. So what's a function (or code) that does the equivalent of strtok_s in vc7? ...

Automated Dead code detection in native C++ application on Windows?

Background I have an application written in native C++ over the course of several years that is around 60 KLOC. There are many many functions and classes that are dead (probably 10-15% like the similar Unix based question below asked). We recently began doing unit testing on all new code and applying it to modified code whenever possibl...

Can you have protected nested classes in C++?

I have a class that only really ever needed by classes in a certain class hierarchy. I wanted to know if it is possible to nest the class in the highest class's protected section and have all the other classes automatically inherit it? ...