I have some multi threaded code that makes one thread per core except I'm having troubles working out how to get the core count under windows.
For linux i have:
//from http://www.cplusplus.com/forum/unices/6544/
FILE * fp;
char res[128] = {0};
fp = popen("/bin/cat /proc/cpuinfo |grep -c '^processor'","r");
fread(res, 1, sizeof(res)-1, ...
It seems this method for creating a Global Object is not working! Here is my code:
#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
#pragma hdrstop
class Tester;
void input();
class Tester
{
static int number = 5;
public:
Tester(){};
~Tester(){};
void setNumber(int newN...
What are the Advantages/Drawbacks of these two ways of iterating through a container / which one do you prefer and why:
for (MyClass::iterator i = m.begin(), e = m.end() ; i != e ; i++)
{
// ...
}
or
for (MyClass::iterator i = m.begin() ; i != m.end() ; i++)
{
// ...
}
Subsidiary question: i++ or ++i? Why?
...
I am having trouble starting my service on my pc. My code is based on this article http://www.gamedev.net/reference/articles/article1899.asp
When i call installService from my int main(int argc, char *argv[]), it is registered successfully (i can see it in msconfig and services.msc). However it has not started. I manually start the serv...
I just stumbled on Protothreads. They seems superior to native threads since context switch are explicit.
My question is. Makes this multi-threaded programming an easy task again?
(I think so. But have I missed something?)
...
I'm using C++ with the OpenCV library, which is a library image-processing although that's not relevant for this question. Currently I have a design decision to make.
OpenCV, being a C library, has its data structures (such as CvMat) declared as structs. To create them, you use functions like cvCreateMat, and to release them, you use f...
I have a class
template <unsigned int N>
class StaticVector {
// stuff
};
How can I declare and define in this class a static factory method returning a StaticVector<3> object, sth like
StaticVector<3> create3dVec(double x1, double x2, double x2);
?
...
Suppose I need to have a class which wraps a priority queue of other objects (meaning the queue is a member of the class), and additionally gives it some extra functions.
I am not quite sure what the best way is to define that vector and, mainly, how to instantiate it.
Currently I have something like this in the header file:
// in Som...
/* user-defined exception class derived from a standard class for exceptions*/
class MyProblem : public std::exception {
public:
...
MyProblem(...) { //special constructor
}
virtual const char* what() const throw() {
//what() function
...
}
};
...
void f() {
...
//create an exception object and throw ...
Yesterday I read some code of a colleague and came across this:
class a_class
{
public:
a_class() {...}
int some_method(int some_param) {...}
int value_1;
int value_2;
float value_3;
std::vector<some_other_class*> even_more_values;
/* and so on */
}
a_class a_instances[10];
void some_function()
{
...
When declaring a template, I am used to having this kind of code:
template <class T>
But in this question, they used:
template <unsigned int N>
I checked that it compiles. But what does it mean? Is it a non-type parameter? And if so, how can we have a template without any type parameter?
...
I have a function in an external library that I cannot change with the following signature:
void registerResizeCallback(void (*)(int, int))
I want to pass in a member function as the callback, as my callback needs to modify instance variables.
Obviously this isn't possible with a simple:
registerResizeCallback(&Window::Resize);
so...
To specialize a class template, one has to redefine all of the member functions in the underlying base template (i.e. the unspecialized class template) even if they are expected to remain mostly unchanged. What are some of the accepted methods and "best practices" to avoid this code duplication?
Thanks.
...
What is the most optimal way to get a string or char* pointer into an istream.
I want to do the following
std::string a = "abc..";
//I know this can be done, not sure if this is most efficient
// and not sure about char* pointers
std::istringstream istr (a);
...
foo (istr);
void foo(std::istream& is) {
}
...
Both operations create an empty file and return the filename but mkstemp leaves the file open in exclusive mode and gives you the handle. Is there a safety benefit to the C-function? Does this imply that there is a safety hole in the command-line version?
As an aside, it is interesting that there are several related functions in the C...
Okay, mkstemp is the preferred way to create a temp file in POSIX.
But it opens the file and returns an int, which is a file descriptor. From that I can only create a FILE*, but not an std::ofstream, which I would prefer in C++. (Apparently, on AIX and some other systems, you can create an std::ofstream from a file descriptor, but my c...
So I'm reading the "3D Math Primer For Graphics And Game Development" book, coming from pretty much a non-math background I'm finally starting to grasp vector/matrix math - which is a relief.
But, yes there's always a but, I'm having trouble understand the translation of an object from one coordinate space to another. In the book the a...
I'd love to start writing Android apps. That's apparently all in Java. Programming jobs on Craigslist are at least 100 Java to 1 C++. I want to learn Java.
Unfortunately, the CS program I'm considering teaches C++ rather than Java, so C++ is what I'm learning. (I'm sure learning C++ will teach me to code well, but so would Java, and...
Been doing mostly Java and smattering of .NET for last five years and haven't written any significant C or C++ during that time. So have been away from that scene for a while.
If I want to write a C or C++ program today that does some multi-threading and is source code portable across Windows, Mac OS X, and Linux/Unix - is PThread a goo...
What is the purpose of anonymous { } blocks in C style languages (C, C++, C#)
Example -
void function()
{
{
int i = 0;
i = i + 1;
}
{
int k = 0;
k = k + 1;
}
}
Edit - Thanks for all of the excellent answers!
...