c++

How do I make sure there is only 1 mutex?

Hi guys. I am running some thread safe code here. I am using a mutex to protect the section of code that needs to be run only by only 1 thread at a time. The problem I have is using this code sometimes I end up with 2 Mutex objects. This is a static function by the way. How do I make sure only 1 mutex object gets created?? /*static*/ My...

How to initial static member in c++ using function?

I am using c++; in .h: static CRITICAL_SECTION g_CS; in .cpp: CRITICAL_SECTION CQCommon::g_CS; but I want to use QGUID temp; EnterCriticalSection(&g_CS); temp = g_GUID++; LeaveCriticalSection(&g_CS); return temp; in one static function. How can I invoke InitializeCriticalSection(PCRITICAL_SECTION pcs); C...

What is the relation ship between CRITICAL_SECTION and CCriticalSection

What is the relation ship between CRITICAL_SECTION and CCriticalSection. is CCriticalSection a wrapper of CRITICAL_SECTION? BTW: I think the following code is meanless because the cs is not Global, it initial every times before lock() so it can't lock the XXX , is it ? int func { CCriticalSection cs; cs.Lock(); XXX cs.Unlock(); } Ma...

TokyoCabinet: Segmentation fault at hdb->close()

Hi, I'm stuck at a Segmentation fault after inserting about 8 million records in a TC Hash Database. After everything is inserted I close the DB but I caught a Segmentation Fault at this part of the code (tchdb.c): static void tchdbsetflag(TCHDB *hdb, int flag, bool sign){ assert(hdb); char *fp = (char *)hdb->map + HDBFLAGSOFF; if...

The most abusive C++ you've ever seen used?

I'm curious to know, what is the most abrasive, caustic, abusive but perfectly legal and correct C++ you've ever seen ship out the door? This question isn't meant to be a C++ bashing session, but a humorous introspection into the world of C++ we allow ourselves (or others) to get away with. ...

Initialize a pointer to a class with NULL values

I am tring to intialize an array of pointers to a NODE struct that I made struct Node{ int data; Node* next; }; the private member of my other class is declared as Node** buckets; It is currently initialised as buckets = new Node*[SIZE] Is there anyway to initialize the array so that its members point to NULL or some ot...

C global static - shared among threads?

In C, declaring a variable static in the global scope makes it a global variable. Is this global variable shared among threads or is it allocated per thread? Update: If they are shared among threads, what is an easy way to make globals in a preexisting library unique to a thread/non-shared? Update2: Basically, I need to use a preexisti...

Passing Array of objects Composed of other objects as reference to Functions In C++?

Hi all, I have a function which calls itself 10 times a second. I am using QTimer for repeat calls. void DisplayClass::UpdateGuiWithData() { //miscellaneous code which is validated SingletonObjectAsThread::instance()->UpdateFromGuiToExternalHardware(ClassOjbectArray,var1,var2); QTimer::singleShot(100,this,SLOT(UpdateGuiWithData())...

Converting integer identifiers to pointers

I have ID values of the type unsigned int. I need to map an Id to a pointer in constant time. Key Distribution: ID will have a value in the range of 0 to uint_max. Most of keys will be clustered into a single group, but there will be outliers. Implementation: I thought about using the C++ ext hash_map stuff, but I've heard thei...

What to do in class specific version of placement new ?

Class-specific version of placement new can be provided even though you can't replace the global one. What scenarios exist where a class should provide its own placement new operator? Even if my class don't implement placement new the following code works (assuming for abc no operator new is overloaded). char arr[100]; abc *pt = n...

c++ problem with enum and struct

why doesn't this compile: enum E { a, b} typedef struct { int i; E e; } S; int main(){return 0;} I get different errors on different system. ...

Sample code to call Invalidate method of Rect object in Active X control in Visual C++?

Can anyone tell code about Invalidate method of Rect Object in Active X control in VC++? ...

How to get a pointer to the beginning of a file in C++

Hi Is it possible in C++ to somehow get a pointer to the beginning of an opened file so that it ( the pointer ) can be passed to the unix write function together with the size of the file? Just to be clear: I want to pass the whole file to a write-like function - how do I do this? ...

How do I prefix the length of message in TCP/IP

I'm sending messages over TCP/IP, I need to prefix message length in a char array and then send it. How do I do it? Also can you please provide an example of how to extract it at the another end. And if possible, please explain. I'm using C++ and Winsock. EDIT: string writeBuffer = "Hello"; unsigned __int32 length = htonl(writeBuffe...

Parsing Buffer C - Advance buffer past whitespace, store word

It's been awhile since I worked with the standard C library's string parsing code (sprintf, atoi, etc). My specific, current need is as follows: advance_buf( const char*& buf, const char* removed_chars, int size ); It should advance the buf pointer to beyond the group of whitespace and copy the removed characters into removed_chars ....

Constant reference to temporary object

Let's say there is a function like void SendToTheWorld(const Foo& f); and I need to preprocess Foo object before sending X PreprocessFoo(const Foo& f) { if (something) { // create new object (very expensive). return Foo(); } // return original object (cannot be copied) return f; } Usage Foo foo; SendToT...

How can I run an executable from RAM using C++?

How can I run an executable from RAM using C++? The executable is in RAM, and I know the address, how do I call into the program from mine? ...

How to get the address of the std::vector buffer start most elegantly?

I want to use std::vector for dynamically allocating memory. The scenario is: int neededLength = computeLength(); // some logic here // this will allocate the buffer std::vector<TCHAR> buffer( neededLength ); // call a function that accepts TCHAR* and the number of elements callFunction( &(buffer[0]), buffer.size() ); The code ...

warning: returning reference to temporary

I have a function like this const string &SomeClass::Foo(int Value) { if (Value < 0 or Value > 10) return ""; else return SomeClass::StaticMember[i]; } I get warning: returning reference to temporary. Why is that? I thought the both values the function returns (reference to const char* "" and reference to a sta...

Remote debugging error with eclipse CDT

Hi, I am using Galileo CDT on mac os x 10.5.7. I want to remotelly debug a c++ application on a linux machine. I found this guide: http://www.embedded-linux.co.uk/tutorial/eclipse-rse But when it comes to the step "Install Remote System Explorer", when I go to "available software", I only get error messages like "No repository found ...