Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Rattrap

Member Since 03 Nov 2004
Offline Last Active Yesterday, 01:48 PM
*****

#5044270 Eight, Nine, Ten...

Posted by Rattrap on 18 March 2013 - 11:07 AM

an explanation for the more noobish of us? what's wrong with that definition? >_<;

 

0x10 in 16 in hex.




#5022157 VS2010 problem with multiple declaration in for loop

Posted by Rattrap on 16 January 2013 - 07:04 AM

Can't bring myself to do it.

 

Why?

 

This code is basically self documenting and much easier to read.




#5021504 which cpu to model in VM interpreter?

Posted by Rattrap on 14 January 2013 - 12:44 PM

Go for MMIX.

 

It's a fictional processor Donald Knuth uses in The Art of Computer Programming books.  64-bit and RISC.




#5002652 Background resource loading - waiting for some item to finish

Posted by Rattrap on 20 November 2012 - 07:00 AM

1) If you have a C++11 compiler, you could use std::future to block until the resource signals it is ready

2) std::future has timeout capability as well.

[edit]
If you don't have a C++11 compiler, you could use boost::future as a replacement.


#4881431 Shared pointers in event driven game engine

Posted by Rattrap on 07 November 2011 - 09:52 AM

In my Game Actor class constructor I pass a boost::shared_ptr<IEventListener*>(this) to the Event Manager so I guess in essence I do what you say (using a raw pointer twice)


You might look into enable_shared_from_this.  It might help provide some of the functionality you are looking for.  The multiple inheritance might cause some problems still.


#4880788 MSVC std::vector and aligned data

Posted by Rattrap on 05 November 2011 - 08:56 AM

After reading this thread back in October, I had submitted a bug report to Microsoft about vector resize not taking a const reference.  Some research had shown that it had been previously requested to be changed to a const reference, but it had been rejected to be fixed.

As you can see by the first link, this wll be fixed in VC11.

Hi,

Thanks for reporting this bug. We've fixed it, and the fix will be available in VC11.

Amusingly, when this was originally reported in July 2006 (months before I joined the VC team), this was indeed By Design. C++03 23.2.4.2 [lib.vector.capacity]/6 specified:

void resize(size_type sz, T c = T());

Now, C++11 23.3.6.3 [vector.capacity]/9, 11 specifies:

void resize(size_type sz);
void resize(size_type sz, const T& c);

This change was introduced in March 2008's Working Paper N2588.

If you have any further questions, feel free to E-mail me at stl@microsoft.com .

Stephan T. Lavavej
Visual C++ Libraries Developer




#4861045 [ Visual Studio ( Express ) ] C++ Intellisense?

Posted by Rattrap on 13 September 2011 - 05:50 AM

I'm not sure if this works with Express, but it does in Pro.  Look under the Project menu or Right click on the project name under the Solution Explorer. Choose Rescan Solution. This will cause Intellisence to go over everything. This usually is enough to kick start it if it stops working.



Since they added this to VC 2010, this has managed to fix any problems I've been having.  I haven't needed to go back and delete the ncb's like in 2005 or 2008.


#4852748 [DX9, C++] shared_ptr and IDirect3DTexture9 (or any other COM interface)

Posted by Rattrap on 23 August 2011 - 06:04 AM

void AddTexture(std::string textureName, std::string filename)
{
	.....
	Textures[textureName] = texture; // a raw pointer here; [b]should I "wrap" it into an intrusive_ptr[/b]?
}


Remember what I said about calling the constructor so it doesn't make an additional call to AddRef, you will want to explicitly create the intrusive_ptr to ensure this doesn't happen.


void AddTexture(std::string textureName, std::string filename)
{
	....
	Textures[textureName] = d3dtexture_t(texture, false);
}

The use of false as the second parameter on the constructor keeps the ref counting from going to 2, when we really only have 1 reference.


#4838130 [C++] A few Boost Thread questions

Posted by Rattrap on 20 July 2011 - 02:50 PM

First, replace those lock and unlock calls with one of the RAII locking structures provided by boost::threads (see example below, I made a typedef for declaring the mutex and a lock that uses a typedef exposed by boost::mutex)

Next, take a look at std::queue.  This will be a much better structure for storing the jobs, instead of the static sized array you've got.

Next, instead of storing as a function pointer, use boost or tr1 function to store the functions to process.  They have a little more "weight" to them behind the scenes, but should be a little safer to use.

Using the queue, it will be much easier to test if there is a job availble to run, else you can call sleep() or yield() to let the thread not completely eat the cpu when it has nothing to do.

typedef	boost::mutex		mutex_t;
typedef	mutex_t::scoped_lock 	lock_t;

std::queue<std::tr1::function<void ()> > Jobs;

__declspec(dllexport) Indigo::Worker::Worker()
{
	active = true;
	Thread = boost::thread(&Worker::Work, this);
}

__declspec(dllexport) Indigo::Worker::~Worker()
{
	lock_t Lock(mutex);
	active = false;
	//	Ensure there are no remaining jobs in the queue (this is probably overkill)
	while(Jobs.empty() == false)
	{
		Jobs.pop();
	}
}
	
__declspec(dllexport) void Indigo::Worker::Work()
{
	//	Checking active will probably need to be atomic
    lock_t Lock(mutex);
    while (active)
	{
		if(Jobs.empty() == false)
		{
			//	Execute the first job in the queue
			Jobs.front();
			//	Remove the job from the queue
			Jobs.pop();
		}
	else
	{
		boost::thread::yield();
	}
	}
}
	
__declspec(dllexport) void Indigo::Worker::Add(boost::function<void ()> Func)
{
	lock_t Lock(mutex);
	Jobs.push(Func);
}

[edit]
Fixed formatting in code and added yield condition.


#4837645 [C++] A few Boost Thread questions

Posted by Rattrap on 19 July 2011 - 03:53 PM


		Mutex_A.lock(); //don't continue until A is finished
		Mutex_B.lock(); //don't continue until B is finished
		Mutex_C.lock(); //don't continue until C is finished

		//TODO: Finish with Update A, B & C resources

		Mutex_A.unlock(); //free A for next loop
		Mutex_B.unlock(); //free B for next loop
		Mutex_C.unlock(); //free C for next loop
};


My first advice would be to not use .lock / .unlock and use one of the RAII based lock-guards that boost provides

As far as frequent creation and destruction, that can get expensive.  If you can, you might look at creating a couple of worker threads that you keep running all of the time.  These threads should each have a queue of functions to execute (like a functor).  If there is nothing in queue, just have the thread sleep then do another check.

[edit]
See this thread for a more detailed example of what I was describing.


#4837384 [VS 2010 C++] Strange behavior including boost

Posted by Rattrap on 19 July 2011 - 08:05 AM

do you think they could be causing the problem of the program doing nothing?
if so what would I be looking for that indicates CLI?
I'm unfamiliar with the language, but taking a quick google it seems similar to C# (with that being the coding language I'm most familiar with its plausible I used similar code in C++ by mistake)


It's basically a modified version of C++ that uses the .Net Common Language Runtime.  Check your project files (Right click on the Project and choose Properties).  Check under General -> Common Language Runtime Support and see if it says Common Language Runtime Support /clr (see picture below)

VS2010CLR.png


If it is there, try changing it to No Common Language Runtime Support.


#4837371 [VS 2010 C++] Strange behavior including boost

Posted by Rattrap on 19 July 2011 - 07:42 AM

The correct syntax for #include is #include <path/to/file.h> not <path\to\file.h>.


VC 2010 Pro (not sure about express) actually has the option to auto-complete includes either with either slash (defaults to \ )

VS2010Slash.png





#4837352 [VS 2010 C++] Strange behavior including boost

Posted by Rattrap on 19 July 2011 - 06:55 AM

I'm not using C++/CLI... at least not to my knowledge
Intellisence still works and it was to my understanding it didn't work with projects that included / used C++/CLI (I've come across VS 2010 commenting on it in the bottom right corner when looking at certain code examples)


I just tried creating a test C++/CLI project in VC2010 and included <boost/thread.hpp> and got the exact same error you did.

1>------ Build started: Project: CLRTest, Configuration: Debug Win32 ------
1>Build started 7/19/2011 8:51:27 AM.
1>InitializeBuildStatus:
1>  Creating "E:\Programming\CLRTest\_Obj\Win32\Debug\CLRTest\CLRTest.unsuccessfulbuild" because "AlwaysCreate" was specified.
1>ClCompile:
1>  stdafx.cpp
1>  AssemblyInfo.cpp
1>  CLRTest.cpp
1>  Generating Code...
1>e:\programming\boost\boost\thread\win32\basic_timed_mutex.hpp(160): warning C4793: 'boost::detail::basic_timed_mutex::unlock' : function compiled as native :
1>   Found an intrinsic not supported in managed code
1>e:\programming\boost\boost\thread\win32\thread_primitives.hpp(314): warning C4793: 'boost::detail::win32::interlocked_bit_test_and_set' : function compiled as native :
1>   Found an intrinsic not supported in managed code

1>  .NETFramework,Version=v4.0.AssemblyAttributes.cpp
1>C:\Users\Rob Yull\AppData\Local\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.cpp : fatal error C1083: Cannot open compiler generated file: 'E:\Programming\CLRTest\_Obj\Win32\Debug\CLRTest\C:\Users\Rob Yull\AppData\Local\Temp\.NETFramework,Version=v4.0.AssemblyAttributes.obj': Invalid argument
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:04.30
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


It looks like your IndigoFramework project is C++/CLI.

You are correct about Intellisense and C++/CLI, but I'm guessing only one of the projects may be incorrectly setup this way.


#4836937 why is C++ not commonly used in low level code?

Posted by Rattrap on 18 July 2011 - 12:31 PM

Change those std::endl to '\n' and you see a big difference.  You are flushing the buffer after every call using std::endl;
Also, to make the code a little closer to the C version, reserve some memory for the string.




#include <iostream>
#include <string>

int main(int argc, char *argv[]) {
    std::string name;
    name.reserve(32);
    for(int i=0;i<8192;i++) {
        std::cout << "Argument count: " << argc << '\n';
        for(int j=0;j<argc;j++) {
            std::cout << "Name: " << argv[j] << '\n';
        }
    }
    std::cin >> name;
    std::cout << "Hello " << name << "!" << std::endl;
    return 0;
}


C++
real 0m0.042s
user 0m0.030s
sys 0m0.010s

C
real 0m0.029s
user 0m0.020s
sys 0m0.007s

[edit]
Added link about std::endl.


#4835219 When use what Error-Handling

Posted by Rattrap on 14 July 2011 - 06:09 AM

Exceptions all boil down to two extremes: "handle the problem immediately and locally, and gosh darnit now I have to remember to wrap every call to this function" or the other extreme, "stop everything, and move on."  In the latter case it's perfectly fine to use a return code.  Return codes, in the places where you want them, are not actually strenuous.


I find this statement funny.  So basically instead of having to wrap a function call in a try/catch block, you want to wrap it in a if/else or switch block?  Possible performance issues aside (to be determined by profiling), you are basically just trading one test for another (hopefully, since I frequently see examples where no test is performed).

Exceptions are less scary in memory-safe languages and so their use is less discouraged.


If you are using RAII (as you should be in C++), it can be a very memory and resource safe language.  And this applies to whether your are using exceptions or return codes.




PARTNERS