Tag: c++

Entries for tag "c++", ordered from most recent. Entry count: 121.

Pages: 1 2 3 ... 16 >

22:40
Sat
08
Aug 2015

Adventures with Porting Code to Visual Studio 2015 and No DirectX SDK

I just installed new Visual Studio 2015 Community Edition and ported my personal project to it. At the same time, I uninstalled old DirectX SDK Jun 2010 and started using new DirectX headers bundled with Windows SDK, which is installed together with Visual Studio. These two transitions are not connected - I could already switch to new DX headers years ago, but I decided to do it now. While transition to VS 2015 was smooth, abandoning DirectX SDK caused some problems, because new DirectX doesn't contain D3DX library. Here is a dump of the problems and solutions that I encountered during this effort:

1. I uninstalled DirectX SDK Jun 2010, Visual Studio 2013 Community Edition and all other components that seemed related to it, like Microsoft SQL. I left all "Microsoft Visual C++ XX Redistributable" though, because these are required by many applications and intended to be installed on target machine, not necessarily as a part of development environment.

Next, I downloaded and installed new Visual Studio 2015 Community Edition. During this long process, I was thinking what should I expect from the new IDE... Whether Microsoft did a good job this time? On one hand, it is suprising that C++ is now an optional installation component, so it seems like native code is in shadow comparing to all these new and trendy cloud/web/mobile/managed technologies. On the other hand, table: C++11/14/17 Features In VS 2015 RTM shows that the new C++ compiler caught up with many features of new C++11/14/17 language, which gives hope that authors still treat native code development seriously.

2. The looks of new IDE is so similar to the previous version it is hard to notice any differences. After launching it, I had to first recompile static libraries that my project depends on. That was zlib 1.2.8 and my CommonLib. Converting project to new version, as well as the build itself went smoothly, without any problems - which is unusual with C/C++ libraries :) Just as in previous version, headers and libs of standard C library, standard C++ library (STL) and WinAPI are already bundled with the Visual Studio, so there is no need to install or configure anything additional before you can use them.

Read full entry > | Comments (0) | Tags: visual studio directx c++ | Author: Adam Sawicki | Share

12:59
Wed
08
Jul 2015

How to check size of structure during development in Visual C++?

Today I wanted to know what is sizeof(SomeStructure) during my coding in C++ in Visual Studio, without compiling and running my program. I needed that to put it into an assertion. Sure I could run the program and then break into debugger and evaluate the sizeof(SomeStructure) e.g. in Watches window, but the project is big and takes long time to build.

It turns out there is no such feature in Visual Studio to check size of structure, but it can be easily hacked using IntelliSense. In just few seconds, from this Google query, through its first result - this StackOverflow page, I have found following solution:

1. Put this line somewhere into your code:

template<size_t S> class Sizer { }; Sizer<sizeof(MY_STRUCTURE)> foo;

2. Replace "MY_STRUCTURE" with the name of your structure, other type or a variable.

3. Hover mouse cursor over "foo" and observe evaluated expression, for example: "Sizer<1296U> foo". 1296 is the size of your structure, in bytes - same as operator sizeof would return in runtime, when your program is compiled in currently selected configuration. "U" is for "unsigned".

By the way, maybe it would be a good feature request to add printing size of a type to the information available in design-time in Visual Studio, e.g. to Properties window, where there already are information about the type like IsTemplate, IsValue etc.?

Comments (2) | Tags: c++ visual studio | Author: Adam Sawicki | Share

07:29
Wed
20
May 2015

Installing Visual C++ Redistributable Package from Command Line

You may think that unless you explicitly use some external library (like FMOD), your program will not require any additional libraries to work, but when coding in C++ using Visual Studio, this is not the case. The functions of standard C/C++ library are implemented in a package of DLL-s called Microsoft Visual C++ Redistributable Package. Each version of Visual Studio has their own set. For example, version for Visual Studio 2013 (Release configuration) consists of files: msvcr120.dll, msvcp120.dll.

You can make your application not requiring this library by setting your project options in Configuration Properties > C/C++ > Code Generation > Runtime Library to "Multi-threaded [Debug]" without the "DLL" part, which makes it statically linked. Alternatively, you can distribute these DLL files (although I'm not sure if this is legal) or the whole library installer together with your application. The library is small and free, available to download from Microsoft website:

The question is: can you launch the installer of these packages with some special parameter so the user doesn't have to go through all the setup wizard, confirming each step? The answer is yes, but as Microsoft likes to change everything very often :) the exact command line is different depending on version. Here is the whole set:

Visual Studio 2005:

Visual Studio 2005, x86 (32-bit version):
vcredist_x86.exe /q:a /c:"VCREDI~1.EXE /q:a /c:""msiexec /i vcredist.msi /qn""
Visual Studio 2005, x64 (64-bit version):
vcredist_x64.exe /q:a /c:"VCREDI~2.EXE /q:a /c:""msiexec /i vcredist.msi /qn"" "
Visual Studio 2005 SP1, x86:
vcredist_x86.exe /q:a /c:"VCREDI~3.EXE /q:a /c:""msiexec /i vcredist.msi /qn"" "
Visual Studio 2005 SP1, x64:
vcredist_x64.exe /q:a /c:"VCREDI~2.EXE /q:a /c:""msiexec /i vcredist.msi /qn"" "

If you would like to install it in unattended mode (which will show a small progress bar but not require any user interaction), you can change the "/qn" switch above to "/qb". Unattended mode + disabled "Cancel" button is "/qb!".

Visual Studio 2008: Just pass one of these parameters:

/q - quiet mode, no user interface.
/qb - unattended mode, shows progress bar but no user interaction required.
/qb! - unattended mode with "Cancel" button disabled.

Visual Studio 2010 and 2012:

/q /norestart - quiet mode
/passive /norestart - passive (unattended) mode

Visual Studio 2013 and 2015:

/install /quiet /norestart - quiet mode
/install /passive /norestart - passive (unattended) mode

To quickly install all of these libraries on the machines where lots of different applications are launched that may require them, I gathered all the libraries in one directory and I have written following BAT script:

"2005 SP1\vcredist_x86.exe" /q:a /c:"VCREDI~3.EXE /q:a /c:""msiexec /i vcredist.msi /qb"" "
"2005 SP1\vcredist_x64.exe" /q:a /c:"VCREDI~2.EXE /q:a /c:""msiexec /i vcredist.msi /qb"" "

"2008 SP1\vcredist_x86.exe" /qb
"2008 SP1\vcredist_x64.exe" /qb

"2010 SP1\vcredist_x86.exe" /passive /norestart
"2010 SP1\vcredist_x64.exe" /passive /norestart

"2012 Update 4\vcredist_x86.exe" /passive /norestart
"2012 Update 4\vcredist_x64.exe" /passive /norestart

"2013\vcredist_x86.exe" /install /passive /norestart
"2013\vcredist_x64.exe" /install /passive /norestart "2015\vc_redist.x86.exe" /install /passive /norestart "2015\vc_redist.x64.exe" /install /passive /norestart

Comments (0) | Tags: visual studio c++ | Author: Adam Sawicki | Share

23:57
Wed
14
Jan 2015

Review: Deleaker - A tool that finds resource leaks

Deleaker is a tool for programmers that finds resource leaks in C++ programs. It's commercial, with free trial and unconditional 30 day money back. Here is my review of this tool. I've tested version 3.0.27.0.

Deleaker is installed as a plugin for Visual Studio, any version from 2005 to 2013. It also works with Visual Studio Community 2013, as this new free version also supports plugins. There is also standalone Deleaker application (see below).

The purpose of this tool is to augment debugging of native C++ programs with the ability to list all resources that are allocated at the moment (heap memory, virtual memory, OLE memory, GDI objects, USER objects, handles) and so to detect resource leaks. Here is how it works:

The interface is very simple - it can be learned in just few minutes. You can build your program and start debugging it by hitting F5, Deleaker is enabled automatically. Now just open dedicated panel (menu Deleaker > Deleaker Window) and there press "Take snapshot" button. You don't even have to pause execution, but of course the button works as well when your program is paused at a breakpoint. After few seconds, the panel is populated with a list of currently allocated resources, with the place from which it was allocated shown in first column.

After selecting one, bottom panel displays full call stack. Clicking in this call stacks navigates to the place in the source code where the allocation is specified. Finally, after program exit, the list is filled with resources that were not freed - these are actual leaks!

You can filter the list by module (EXE or DLL file that made the call) and by resource type (memory, GDI objects etc.). There is also a column with size of the resource and "Hit Count" - number of resources that were allocated by that particular place in the code (e.g. inside a loop) and stay allocated at the moment.

"Show full stack" button is a nice feature. Clicking it displays full call stack, while by default, the stack is stripped from entries that don't come from your code, but from system libraries. For example, above my function with the actual allocation instruction, there is MSVCR120D.dll!operator new, then there is MSVCR120D.dll!malloc etc... until ntdll.dll!RtlAllocateHeap. It's good that the program can ignore such call stack entries. It also entirely ignores allocations made by system modules outside of your code.

Unfortunately it does this only by identifying module that the function comes from and not it's name, so it cannot ignore templates, like these from STL containers. Maybe ignoring functions by name specified as wildcard or regular expression would help, e.g. "std::*" or "std\:\:.+" - just like Visual Studio debugger can step over specified functions, as I described in How to Make Visual Studio Debugger not Step Into STL.

You can press "Take snapshot" multiple times and save the snapshots for later view. (They are just numbered, you cannot give them names.) By the way, Deleaker captures F5 key, so even when during debugging session, if the focus is in Deleaker panel, this button doesn't resume your program, but instead refreshes the list of allocations (takes new snapshot). You can also select two snapshots and compare them. Then you see only resources that were allocated in the right snapshot and not in the left, which can indicate a leak that happened during some time of the program execution.

Besides heap memory allocations, the tool can also detect other types of resources, like GDI objects. Unfortunately not all interesting types of resources are covered. For example, an opened file of type FILE* f = fopen(...) is shown as normal memory allocation and opened file of type HANDLE f = CreateFile(...) is not shown at all, but I guess it must be due to some system internals.

I didn't find a single leak in my main home project, so I created a dedicated, simple program to test if it can really find leaks. I also checked that it works with programs compiled in Release configuration as well.

Aside from being a Visual Studio plugin, Deleaker can also work as standalone Windows application.

Overall, I like the program. If its price is not a problem for you or your company, I think it can be very useful in improving quality of developed software. I especially like the fact that it's so easy to learn and use.

Comments (4) | Tags: c++ tools visual studio | Author: Adam Sawicki | Share

14:49
Sun
28
Sep 2014

Building 64-bit Executables in Visual Studio Express

I admit that for years I was convinced only the paid version of Visual Studio can compile 64-bit EXE files. It's just because I never really needed to create one in my personal projects. Now I have to do it (what for? - I will show in one of my next posts) and as it turns out, it's perfectly possible and easy to do also in Visual Studio Express. You just have to make some configuration because the "x64" platform is not shown by default. So to compile 64-bit executable in your Visual Studio 2013 Express:

You can now choose and build any configuration (Debug or Release) for any platform (Win32 or x64). For x64 platform, intermediate and output files will be written to subdirectories: x64\Debug\ and x64\Release\. Notice that for Win32, it's just Debug\ and Release\.

Comments (3) | Tags: c++ visual studio | Author: Adam Sawicki | Share

14:55
Sat
02
Aug 2014

CPrintStream 1.1

2.5 years ago I wrote a piece of C++ code called CPrintStream - see CPrintStream - Polymorphic Printf for description. Now I updated it to version 1.1. Files to download:

PrintStream.hpp
PrintStream.cpp

What's new:

OutputDebugString is a WinAPI function for passing debug messages. If debugging in Visual Studio, these messages can be viewed in Output panel. If not, they can be captured using separate, simple and free program - DebugView.

Comments (13) | Tags: c++ | Author: Adam Sawicki | Share

22:48
Wed
28
May 2014

Offensive Programming :)

Generally, defensive programming is a very good idea. But many times I've seen this following C++ code explained as an example of defensive programming:

if(pointer != NULL) {
   pointer->SomeMethod();
   // Some other things with pointer.
}

If the pointer here is expected to always point to a valid object and not NULL, otherwise it's a programmer's bug somewhere else in the code, then I believe such practice is very, very bad. I think there should be an assertion instead, like this:

assert(pointer != NULL);
pointer->SomeMethod();
// Some other things with pointer.

First of all, assertion is compiled only in debug configuration, while condition would be additional code executed in all builds, unnecessarily impacting performance. Second and more important argument is that when there is a bug and the pointer is NULL, we should know about it as soon as possible and be able to debug this particular place. That's easy when we put an assert. That's also easy when we dereference NULL pointer, because then the application would crash, which is also possible to catch in the debugger. On the other hand, when there is a condition around code using this pointer and the pointer is NULL, the code inside is just not executed, which may somehow corrupt or just propagate corrupted state of the program, hide the bug or defer its appearance for later time and to different place of the code, making it harder to find. Recommending to crash the app could sound radical, so let's call this "offensive programming" :)

Comments (12) | Tags: c++ | Author: Adam Sawicki | Share

18:31
Tue
06
May 2014

Boost Geometry LOL

Many programmers I follow on Twitter made fun recently of this page: Boost Geometry - Design Rationale. I agree with them - it's ridiculous how sophisticated a simple geometric distance function can become if you want to make it generic using C++ metaprogramming. There are many developers who like such tricks, but I'm not one of them.

Of course I know and appreciate that C++ is so powerful you can write such generic code and that all the complexity is solved in compile time so run-time performance is good and not affected by this. But on the other hand, generic programming in C++ has its flaws: it's hard to learn, the code is long and complex, hard to write, debug, read and understand. When you make a mistake, error messages are cryptic, which is inevitable with so many levels of indirection.

So here is what I thought about this: If all these traits and other helper, boilerplate code was not needed and if compile-time metaprogramming was imperative and iteration-based like the main language itself, not functional and recursion-based, then a dimension-agnostic distance function could look like as simple as this:

struct vec2 { float x, y; }
struct vec3 { float x, y, z };

declare components of vec2 as { x, y };
declare components of vec3 as { x, y, z };

float distance(a, b)
{
    float dist = 0;
    foreach(c in a.components)
        dist += (b.c - a.c) * (b.c - a.c);
    return sqrt(dist);
}

But because it doesn't, I will stick with 2D, 3D and 4D vectors of floats :)

Comments (7) | Tags: boost c++ | Author: Adam Sawicki | Share

Pages: 1 2 3 ... 16 >

STAT NO AD [Stat] [Admin] [STAT NO AD] [pub] [Mirror] Copyright © 2004-2015 Adam Sawicki
Copyright © 2004-2015 Adam Sawicki