Usually when you want to sort a std::vector or another container thats holds objects of a custom datatype, you just write a compare function that takes two such objects, does something to find out if one is less than the other, and returns true if it is. Then you pass the address of this function to std::sort and voila.
Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts
C++ - Discovering Memory Leaks
If you are working on a serious project, you will propably use a sophisticated memory manager which will also discover memory leaks (along with their exact position in the code), but if you just want to do a quick test in a small program, there is an easy way to let the debugger check for memory leaks:
- Include crtdbg.h in your project (this file is included in the Microsoft Platform SDK)
- At the beginning of main() or WinMain () put this code:
- Include crtdbg.h in your project (this file is included in the Microsoft Platform SDK)
- At the beginning of main() or WinMain () put this code:
int flag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
flag |= _CRTDBG_LEAK_CHECK_DF;
_CrtSetDbgFlag(flag);This works in Visual C++ ( i don't know about other compilers though ) and, if you run the programm in debug mode, it will report memory leaks in the debug output window ("Immediate Window" in the latest Visual Studio version).C++ - Bitmap Tutorial: Loading and Saving Bitmaps
1. Introduction
The windows bitmap file format (.bmp) is the most widely used image file format on windows (next to .jpg), and there are many occasions a program or game has to be able to load or save bitmaps ( raytracers and other non-realtime renderers should be able to save their output in .bmp format, games might have to load them as textures etc. ).
Unfortunately .bmp files are not as straightforward as for example .png image files and provide quite a problem for newbies since it's not that easy to figure out how to use them when without a library or API.
Two notes before we start:
1) In this tutorial we're dealing with 24 bit bmps only. But it shouldn't be hard to change the code to support other formats.
2) For clarity's sake i'm only showing the code important to the task at hand, with only minimal error checking. If you want to use this code in a real program, you should add some exception handling.
The windows bitmap file format (.bmp) is the most widely used image file format on windows (next to .jpg), and there are many occasions a program or game has to be able to load or save bitmaps ( raytracers and other non-realtime renderers should be able to save their output in .bmp format, games might have to load them as textures etc. ).
Unfortunately .bmp files are not as straightforward as for example .png image files and provide quite a problem for newbies since it's not that easy to figure out how to use them when without a library or API.
Two notes before we start:
1) In this tutorial we're dealing with 24 bit bmps only. But it shouldn't be hard to change the code to support other formats.
2) For clarity's sake i'm only showing the code important to the task at hand, with only minimal error checking. If you want to use this code in a real program, you should add some exception handling.
C# - Non-blocking Per-Pixel Painting in Windows Forms
WinForms gives us the PictureBox control, into which we can easily load images of different formats, have them scaled for us, etc.
But, unfortunately, PictureBox gives us no obvious way to paint it ourself via some SetPixel method.
In this article i will thus show you how to implement your own SetPixel method. As you will know, when you directly execute a long-running function, the user interface will be unresponsive (you won't be able to move the window, or click anything on it) until the function has finished running. When painting something, your paint method might be very long running (for example if you do raytracing), so we will do our painting in a background thread. This will only add a few lines of code, but keep the application running smooth.
Also, since there are different ways to achieve our goals, i will split this article up into two parts, showing you different methods of how to achieve the same effect.
For the rest of this article, i assume that we have a Windows Forms project, and that the form contains a PictureBox named pictureBox1, and a Button (named button1), which will start our test-rendering.
But, unfortunately, PictureBox gives us no obvious way to paint it ourself via some SetPixel method.
In this article i will thus show you how to implement your own SetPixel method. As you will know, when you directly execute a long-running function, the user interface will be unresponsive (you won't be able to move the window, or click anything on it) until the function has finished running. When painting something, your paint method might be very long running (for example if you do raytracing), so we will do our painting in a background thread. This will only add a few lines of code, but keep the application running smooth.
Also, since there are different ways to achieve our goals, i will split this article up into two parts, showing you different methods of how to achieve the same effect.
For the rest of this article, i assume that we have a Windows Forms project, and that the form contains a PictureBox named pictureBox1, and a Button (named button1), which will start our test-rendering.
C# - How to Reverse a Unicode String
Perhaps due to the lack of a built-in String.Reverse method in the .NET Framework, it's very common for implementations of such a method to be posted.
Unfortunately, most of these implementations do not handle characters outside Unicode's Basic Multilingual Plane correctly. These supplementary characters have code points between U+10000 and U+10FFFF and so cannot be represented with one 16-bit char. In UTF-16 (which is how .NET strings are encoded), these Unicode characters are represented as two C# chars, a high surrogatefollowed by a low surrogate. When the string is reversed, the order of these two chars has to be preserved.
Here's our method that reverses a string while handling surrogate code units correctly:
C# - How to Reverse a String
Unfortunately, C#'s string class is missing a Reverse() function, so here are three ways to reverse strings:
1. Manual Reversal
The most obvious way is to reverse a string by manually walking through it character for character and building a new string:
1. Manual Reversal
The most obvious way is to reverse a string by manually walking through it character for character and building a new string:
string input = "hello world"; string output = ""; for (int i = input.Length - 1; i >= 0; i--) { output += input[i]; }Note that if you take this approach in a serious program, you should use StringBuilder instead of string, since in the above code, a new temporary string is created each time you add a single character to the output string.
C# - Booleans Without Short-Circuiting
C# implements the boolean AND and OR operations like C/C++ with the operators && and ||.
The 'single character' versions of these (& and |) are used in both languages for bitwise AND and OR operations.
But in C# the single character versions can be used for boolean logic too, but with a twist.
The standard boolean operators (&& and ||) try to make your code as efficient as possible, by employing short-circuiting, to skip evaluation where possible.
If we have a boolean AND expression with two arguments, we know that the expression as a whole is false if one (or both) of the arguments is false. Thus when the first argument is false, the end result is known, and the second argument is never evaluated.
With OR it's the other way around: if one argument is true, the whole expression is true. Thus if the first argument evaluates to true, the second argument isn't evaluated.
Consider this sample class:
The 'single character' versions of these (& and |) are used in both languages for bitwise AND and OR operations.
But in C# the single character versions can be used for boolean logic too, but with a twist.
The standard boolean operators (&& and ||) try to make your code as efficient as possible, by employing short-circuiting, to skip evaluation where possible.
If we have a boolean AND expression with two arguments, we know that the expression as a whole is false if one (or both) of the arguments is false. Thus when the first argument is false, the end result is known, and the second argument is never evaluated.
With OR it's the other way around: if one argument is true, the whole expression is true. Thus if the first argument evaluates to true, the second argument isn't evaluated.
Consider this sample class:
C# - Efficiently Looping Over An Array
This tip can improve the speed of programs that spend a lot of time looping over arrays.
Propably you know from experience, that when you try to access an invalid array element (say, index 11 in an array of size 10) you will get an IndexOutOfRangeException. To be able to generate this exception and prohibit the dangerous access of memory beyond your array storage, the runtime performs an array bounds check everytime you access an array, which checks that the index you supply is lower then the array size.
For example, take a look at this code:
Propably you know from experience, that when you try to access an invalid array element (say, index 11 in an array of size 10) you will get an IndexOutOfRangeException. To be able to generate this exception and prohibit the dangerous access of memory beyond your array storage, the runtime performs an array bounds check everytime you access an array, which checks that the index you supply is lower then the array size.
For example, take a look at this code:
Is there is a way to call assembly instructions from perl on Linux?!!!
I wonder if there is a way to call assembly instructions from Perl on Linux
like calling assembly from c or c++.
like calling assembly from c or c++.
I put this question on yahoo answers and I got two "NO" answers.
I know that the first question will jump to your mind is "Why the hell you want to do this?!!".
OK, here is the answer. Suppose you succeeded to get a non root Linux shell and unfortunately
there is no c or c++ compiler installed but luckily Perl is available. I think it would be very helpful if
you could write assembly in Perl !!!
I found this code which is very close of what we'r talking about.OK, here is the answer. Suppose you succeeded to get a non root Linux shell and unfortunately
there is no c or c++ compiler installed but luckily Perl is available. I think it would be very helpful if
you could write assembly in Perl !!!
This code work well on windows and of course doesn't work at all on Linux.
This is because it depends on win32 API.
Subscribe to:
Posts (Atom)