c++

Benefit of slist over vector?

What I need is just a dynamically growing array. I don't need random access, and I always insert to the end and read it from the beginning to the end. slist seems to be the first choice, because it provides just enough of what I need. However, I can't tell what benefit I get by using slist instead of vector. Besides, several materials I...

Problem with getting error description after SSL_CTX_new returned NULL

I am very new to SSL , Actually I would say I know nothing about it. I am using the method "SSL_CTX_new" to create an SSL_CTX object. The method returns null.The documentation says I can check the error stack in order to get the cause for this. So I have the function "int SSL_get_error(SSL *s,int ret_code)" which (as I understand) I ...

C++0x initializer list example

I would like to see how this example of existing code would be able to take advantage of the C++0x initializer list feature. Example0: #include <vector> #include <string> struct Ask { std::string prompt; Ask(std::string a_prompt):prompt(a_prompt){} }; struct AskString : public Ask{ int min; int max; AskString(std::s...

Is there any API for getting start time of a APACHE WebServer?

I am writing a small application to get the various diagnostic parameter of Apache Webserver like time of the start of the server, Worker mode or Prefork mode, server version and many more. I have found few API for getting info about these parameter. But I colud not find nay API for the getting start time of the WebServer. Is there any s...

How to define class-specific << operator in C++

Given a class such as: class Person { private: char *name; public: Person() { name = new char[20]; } ~Person() { delete [] name; } } I want to print to print the name from an instance of this, using a statement like the following: cout << myPerson << endl; What do I need to do to define ...

memory management problem in mexFunction

I'm reading from a binary data file which is written by invoking the following lines in Matlab m-file: disp(sprintf('template = %d', fwrite(fid, template_1d, 'uint8'))); AFAIK, uint8 is the same size as the types BYTE, unsigned char, and unsigned short. Hence I have written the following code in a file-reading method in a C++ class ins...

Program crashes when run outside IDE

I'm currently working on a C++ program in Windows XP that processes large sets of data. Our largest input file causes the program to terminate unexpectedly with no sort of error message. Interestingly, when the program is run from our IDE (Code::Blocks), the file is processed without any such issues. As the data is being processed, it...

Style question about existing piece of code (C/C++)

I just hope the following doesn't seem to you like redundant jabber :) Anyway, there is that: for (p = fmt; *p; p++) { if (*p != '%') { putchar(*p); continue; } switch (*++p) { /* Some cases here */ ... } } And I wondered why the writer (Kernighan / Ritchie) used the continue in the if ...

In C++, is is possible to throw an exception that will not be caught by std::exception?

Question 1: Is is possible to throw an exception that will not be caught by std::exception? try { } catch(std::exception & e) { } catch(...) { //Is this block needed? } Question 2: Is it better to have: catch(std::exception & e) Or catch(std::exception e) Or catch(const std::exception &e)//<--- this is the method I usuall...

c++ implementing cancel across thread pools

I have several thread pools and I want my application to handle a cancel operation. To do this I implemented a shared operation controller object which I poll at various spots in each thread pool worker function that is called. Is this a good model, or is there a better way to do it? I just worry about having all of these operationC...

is there any function in C++ that calculates a fingerprint or hash of a string that's guaranteed to be at least 64 bits wide?

is there any function in C++ that calculates a fingerprint or hash of a string that's guaranteed to be at least 64 bits wide? I'd like to replace my unordered_map<string, int> with unordered_map<long long, int>. Given the answers that I'm getting (thanks Stack Overflow community...) the technique that I'm describing is not well-known. ...

Getting template metaprogramming compile-time constants at runtime

Background Consider the following: template <unsigned N> struct Fibonacci { enum { value = Fibonacci<N-1>::value + Fibonacci<N-2>::value }; }; template <> struct Fibonacci<1> { enum { value = 1 }; }; template <> struct Fibonacci<0> { enum { value = 0 }; }; This is a common example ...

c++ back_inserter for a set?

I guess this is a simple question. I need to do something like this: std::set<int> s1, s2; s1 = getAnExcitingSet(); transform(s1.begin(), s1.end(), std::back_inserter(s2), ExcitingUnaryFunctor()); Of course, back_inserter doesn't work since there's no push_back. std::inserter also needs an iterator? I haven't used inserter so I'm not ...

lists find algorithm

I'm trying to translate the usage of find function in Matlab to C++. From what I can see from the C++ find function, I can't seem to find in the description anywhere of an easy method of finding the index in the list in which certain conditions is true rather than just comparing for equality between the item that is being searched for an...

Is there a function in C++ that creates a .bin file, or is this code missing something?

I have a code that looks like this: int main () { fstream file; file.open("test.bin", ios::out | ios::binary); if(!file.is_open()) { return -1; } int n = 3; file.write(reinterpret_cast<char*>(&n), sizeof(n)); file.close(); return 0; } when I run it alone, it exits with -1, so obviously it failed to open "test.b...

Why doesn't this program read (or write?) correctly from a .bin file? (C++)

I created this program: #include <iostream> #include <fstream> using namespace std; int main () { fstream file; file.open("test.bin", ios::in | ios::out | ios::binary); if(!file.is_open()) { return -1; } int n = 5; int x; file.write(reinterpret_cast<char*>(&n), sizeof(n)); file.read(reinterpret_cast<char*>(&x), ...

Wait for certain website to be accessed

My objective is to have an event that is triggered when a website is accessed. Now, maybe through the window title, or the text in the window. Or maybe even reading a URL. As of now I am aware of FindWindow (class,title); However all attempts to put this line of code into a loop and it's exit condition being when the window appears hav...

Isn't C++'s inline totally optional?

I have a class that had an inline member, but I later decided that I wanted to remove the implementation from the headers so I moved the members body of the functions out to a cpp file. At first I just left the inlined signature in the header file (sloppy me) and the program failed to link correctly. Then I fixed my header and it all wor...

Tokenizing a SIC Assembler source

I've pretty much finished coding a SIC assembler for my systems programming class but I'm stumped on the tokenizing part. For example, take this line of source code: The format (free format) is: {LABEL} OPCODE {OPERAND{,X}} {COMMENT} The curls indicate that the field is optional. Also, each field must be separated by at least one sp...

Template class expression parameter overloading

Hey, I'm trying to figure out if it's possible to "overload" a template class deffinition with expression parameters. Kind of like the following snippet of code. template<class T> class Test { public: T testvar; Test() { testvar = 5; cout << "Testvar: " << testvar << endl; } }; template<class T> class Test<T,...