c++

Deleting a pointer a different places results in different behaviors (crash or not)

This question is a refinement of this one, which went in a different direction than expected. In my multithreaded application, the main thread creates parameters and stores them : typedef struct { int parameter1; double parameter2; float* parameter3; } jobParams; typedef struct { int ID; void* params; } jobData; s...

basic pointer question in c++ program

I looking for a clarification regarding the pointers. I have compiled the following code in bordland c++ 5.5.1 without any errors. But while i am trying to execute gives a core error. int main () { int x=10,y=20; int &a=x; int &b=y; int *c; int *d; *c=x; *d=y; return 0; } Basically I am trying to ...

Difference between code generated using a template function and a normal function

I have a vector containing large number of elements. Now I want to write a small function which counts the number of even or odd elements in the vector. Since performance is a major concern I don't want to put an if statement inside the loop. So I wrote two small functions like: long long countOdd(const std::vector<int>& v) { long l...

How do I port code that contains #pragma optimize( "a" ) from VC++7 to VC++9 ?

I'm moving my C++ codebase from Visual Studio 2k3 to Visual Studio 2k8. Code contains #pragma optimize( "a", on ) MSDN says that it means "assume no aliasing". Later versions of VS refuse to compile this and MSDN doesn't seem to say what to do with code containing this #pragma. What does "assume no aliasing" mean and how to I make a ...

When does the C++ default assignment operator become unaccessible?

If I define an own assignment operator, which has a different signature than the normally generated default assignment operator: struct B; struct A { void operator = (const B& b) { // assign something } }; does the default assignment operator, in this case operator = (A&) (or the like, correct me if wrong) become undefined/unacces...

What types of false positive memory leaks can be reported?

When you look for memory leaks using some kind of "tool" like, for example, WinDbg, GlowCode or even the integrated Visual C++ leak reporter, what types of false positives can be reported? In fact, GlowCode warns you against false positives. Depending on the type of scan that you run, more false positives can appear. With this question...

Marshalling BSTRs from C++ to C# with COM interop

I have an out-of-process COM server written in C++, which is called by some C# client code. A method on one of the server's interfaces returns a large BSTR to the client, and I suspect that this is causing a memory leak. The code works, but I am looking for help with marshalling-out BSTRs. Simplifying a bit, the IDL for the server metho...

Ignore OpenMP on Machine that doesn't have it

Hi, I have a C++ program using OpenMP, which will run on several machines that may have or not have OpenMP installed. How could I make my program know if a machine has no OpenMP and ignore those "#include ", OpenMP directives (like "#pragma omp parallel ...") and/or library functions (like "tid = omp_get_thread_num();") ? Thanks and r...

Hibernating/restarting a thread

I'm looking for a way to restart a thread, either from inside that thread's context or from outside the thread, possibly from within another process. (Any of these options will work.) I am aware of the difficulty of hibernating entire processes, and I'm pretty sure that those same difficulties attend to threads. However, I'm asking anywa...

Sort function does not work with function object created on stack?

#include<iostream> #include<vector> #include<algorithm> class Integer { public: int m; Integer(int a):m(a){}; }; class CompareParts { public: bool operator()(const Integer & p1,const Integer & p2) { return p1.m<p2.m; } }obj1; int main() { std::vector<Integer> vecInteger; vecI...

How should I compare a c++ metaprogram with an C code ? (runtime )

Hi there , I have ported a C program to a C++ Template Meta program .Now i want to compare the runtime . Since there is almost no runtime in the C++ program , how should i compare these 2 programs. Can i compare C runtime with C++ compile time ? or is it just not comparable ? ...

Can someone provide an example of seeking, reading, and writing a >4GB file using boost iostreams

I have read that boost iostreams supposedly supports 64 bit access to large files semi-portable way. Their FAQ mentions 64 bit offset functions, but there is no examples on how to use them. Has anyone used this library for handling large files? A simple example of opening two files, seeking to their middles, and copying one to the oth...

C++ .NET convert System::String to std::string

How do you convert System::String to std::string in C++ .NET? ...

Threads in C, C++, C++0x, pthread and boost

A question about threads in C/C++... C++0x syntax #include <thread> void dummy() {} int main(int, char*[]) { std::thread x(dummy); std::thread y(dummy); ... return 0; } How many threads are there? Two (x and y) or three (x, y and main)? Can I call this_thread::yield() in main? And what do I get from calling this_thread:...

How to prevent the linker from optimizing away startup code?

I have the following problem: My (C++-)project consists of several subprojects. In each, I have several files with code I want to run at startup. My solution so far is to use static variables which call the respective code on initialization like this: // Foo.cpp static TFooRegistry sFooRegistry; // does stuff in constructor. When b...

How to retrieve an object instance related to a Thread in C++?

Hello, In java I have the following generated code: public class B { public void exec(){ X x = (X) Thread.currentThread(); System.out.println(x.value); } } public class X extends Thread{ public int value; public X(int x){ value = x; } public void run(){ B b = new B(); b.exec(); } ...

Why might a static data member not get initialized?

I'm trying to register a bunch of classes with a factory at load time. My strategy is to harness static initialization to make sure that before main() begins, the factory is ready to go. This strategy seems to work when I link my library dynamically, but not when I link statically; when I link statically, only some of my static data me...

Map pointers to immutable objects with Hashtable in .NET

I have a Hashtable object which "names" or "map" various fields in a class with a string ref class Interrupt{ Interrupt(){ this->type = 0; this->size = 0; } int type; int size; } Interrupt^ interrupt = gcnew Interrupt(); Hashtable^ map = gcnew Hashtable(); map->Add("InterruptType", interrupt->type); map->Add("Interrup...

Looking for the most elegant code dispatcher

I think the problem is pretty common. You have some input string, and have to call a function depending on the content of the string. Something like a switch() for strings. Think of command line options. Currently I am using: using std::string; void Myclass::dispatch(string cmd, string args) { if (cmd == "foo") cmd_foo(arg...

VC++ LNK2001: unresolved external symbol only when compiling on 64bit

I have made a dll that compiles fine in 32bit mode, but when compiling in 64bit mode (both on a 32bit box cross compiling and on a native 64bit box) I get the above error. The symbol that it is complaining about are the following: "struct return_info_ * __cdecl patch_file(char *,char *,char *)" I am new to C++ but I think I have defin...