All Blog Entries

All blog entries, ordered from most recent. Entry count: 1198.

Warning! Some information on this page is older than 6 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

Pages: > 1 ... 20 21 22 23 24 ... 150 >

# Operator New and Delete - Unnecessary Conditions

Fri
09
Dec 2016

I've seen following constructs many times in C++ code:

MyClass* obj = new MyClass();
if(obj == nullptr)
    // Handle allocation error.

// ...

if(obj != nullptr)
    delete obj;

Both of these conditions are unnecessary. Strictly speaking they are not a bug - program will run correctly, but they make no sense. If you used to write any of these, you should know that:

1. Operator new doesn't return null on failed allocation. By default it throws an exception of type std::bad_alloc in this case, so this is what you should handle if you really care about the state of your program after it runs out of memory (or if the allocated object is particularly big).

There is also special nothrow version of the new operator that returns null on failure, but you must call it explicitly. An alternative would be to overload new operator (global or for particular class) to change its behavior. But again, this is not what happens by default.

Note this is different behavior than malloc function from C. Obviously there are no exceptions in C language, so this function just returns null on failure.

2. Operator delete doesn't crash when you pass null pointer to it - it just does nothing, so the check for not-null is already inside, you don't have to write it. Of course trying to delete object from any non-null address that was already freed or is just invalid address still crashes, or causes other undefined behavior.

Note this is the same behavior as free function from C - it also accepts null pointer.

Comments | #c++ Share

# 32-bit Applications on 64-bit Windows

Wed
30
Nov 2016

As you probably know, the processor, operating system and applications on a PC may be 32-bit or 64-bit. CPU-s we have in our computers are 64-bit for a long time already. Windows XP tended to be used in 32-bit version, but now I can see most people use Windows 7/8/8.1/10 in 64-bit version as well. Only apps still exist in various forms. Shell extensions and drivers must match the version of the operating system, but other programs can be used in 32-bit version even on 64-bit system. Different combinations are possible:

  1. 32-bit Windows, 32-bit application
  2. 64-bit Windows, 64-bit application
  3. 64-bit Windows, 32-bit application
  4. 32-bit Windows, 64-bit application - cannot run.

We may ask a question about where does Windows store files and settings of such apps. It is especially interesting as the answer is very counter-intuitive. Location for (2) – 64-bit apps on 64-bit Windows – may contain “32” in its name (because of backward compatibility), while location for (3) – 32-bit apps on 64-bit Windows – may contain “64” (because of the name WoW64). Here is the list of such locations:

Program Files folder:

  1. C:\Program Files
  2. C:\Program Files
  3. C:\Program Files (x86)

System folder:

  1. C:\Windows\System32
  2. C:\Windows\System32
  3. C:\Windows\SysWOW64

Registry key:

  1. HKEY_LOCAL_MACHINE\SOFTWARE
  2. HKEY_LOCAL_MACHINE\SOFTWARE
  3. HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node

(Same applies to HKEY_CURRENT_USER.)

See also: Windows 64-bit: The 'Program Files (x86)' and 'SysWOW64' folders explained

Comments | #windows Share

# Color Temperature of Your Lighting

Sun
02
Oct 2016

In photography, video and all graphics in general there are so much more parameters to consider than just exposure, meaning lighter or darker image. One of them is color temperature, or white balance. It's about what we consider "white" - a frame of reference, especially concerning light source and so all the objects lit by it. It's not real temperature, but we measure it in Kelvins. Paradoxically, lower color temperature values (like 3000K) mean colors that we call "warmer" - more towards yellow, orange or red. Higher temeratures (like 7000K) mean "cooler" colors - more towards blue. Values like 6500K are considered equivalent of a sunlight during the day, while light bulbs usually have around 3000K. Color temperature of your lighting is important when you work with colors on a computer. They recommend to use 6500K light source for that purpose.

I decided to make an experiment. Below you can see photos of part of my room, with a test screen displayed on my monitor, piece of my wall (behind it, supposed to be white) and a piece of furniture (the right part, also should be white). The monitor is LG 23MA73D-PZ, with IPS panel, calibrated to what I believe should be around 6500K (setting Colour Temperature = Warm2).

Left column shows a photo taken in the middle of the night, with lighting by LED lamps having 3000K color temperature. Middle column is the same scene lit by different LED lamps having 6500K. Finally, right column show a photo taken during the day, using only sunlight.

The only remainig variable is white balance of the photo itself. That's why I introduced two rows. Top row show all three photos calibrated to same white balance = 6500K. As you can see, the image on the screen looks pretty much the same on all of them, because monitor emits its own light. But the wall and the furniture, lit by a specific light source, seems orange or reddish on the first photo, while on the other photos it's more or less gray.

Our eyes, as well as cameras can adjust to changing color temperature to compensate for it and make everything looking neutral-white again. So the second row shows same photos calibrated to white balance of the specific light source. Now the wall and the furniture looks neutral gray on all of them, but notice what happened with the image on the screen when light was 3000K - it completely changed colors of the picture, making everything looking blue.

That's why it's so important to consider color temperature of your light sources when working with color correction and grading of photos, videos or some other graphics. Otherwise you can produce an image that looked good at the time of making, but turns out to have some color cast when seen under different lighting conditions. Of course, if you just work with text or code, it doesn't matter that much. It is more important then to just have a pleasant lighting that doesn't cause eye strain, which would probably be something more like the 3000K lamps.

Comments | #graphics #photography #video Share

# How to Boost Your RAM to Declared 3000 MHz?

Thu
29
Sep 2016

I recently upgraded some components of my desktop PC. I was suprised to discover that RAM doesn't work with declared speed of 3000 MHz. Here is the solution I've found to this problem.

Back in the days of DOS I can remember having to set up everything manually, like selecting IRQ number and DMA channel to make sound working in games. But today, in the era of Plug&Play, assembling a computer is easy and everything works automatically. Almost everything...

Although I found that both my new motherboard (Gigabyte GA-Z170-HD3P) and RAM modules (Corsair Vengeance LPX DDR4, 32GB(2x16GB), 3000MHz, CL15 (CMK32GX4M2B3000C15)) support 3000 MHz frequency, it worked on 2133 MHz. Motherboard specification says: "Support for DDR4 3466(O.C.) /3400(O.C.) /3333(O.C.) /3300(O.C.) /3200(O.C.) /3000(O.C.) /2800(O.C.) /2666(O.C.) /2400(O.C.) /2133 MHz memory modules", while specification of the memory has "3000MHz" even in its title. What happened? Motherboard spec calling all the frequencies higher than 2133 "OC" (like in "overclocking") gave me some clue that it is not standard.

After few minutes of searching on Google, I've learn about a thing called XMP (Extreme Memory Profile). It's an extension to SPD (Serial Presence Detect) - a protocol used by RAM modules to report to the motherboard what parameters do they support. I then checked in the specs that my motherboard, as well as my memory support XMP 2.0.

So what I finally did was:

  1. I restarted my PC.
  2. I entered BIOS/UEFI during boot with [Del] key.
  3. I located a setting related to XMP. It is called "Extreme Memory Profile(X.M.P.)".
  4. I changed it from "Disabled" to "Profile1" - the only other option available.
  5. I exited BIOS with saving changes.

That's all! Fortunately I didn't need to manually set any frequency, timings or voltage of my Skylake processor, memory or any other components, like overclockers do. With all the other settings left to default "Auto", the computer still works stable and RAM now runs with 3000 MHz frequency.

By the way: Please don't be worried when you see only half of this frequency in HWiNFO64 tool as "Memory - Current Memory Clock". All in all we are talking about DDR here, which means "Double Data Rate", so the real frequency is just that, but data is transferred on both rising and falling edge of the clock signal.

Warning! It turned out that enabling XMP on my machine makes it working very unstable. Firefox, The Witcher 3 and basically all memory-intensive applications crashed randomly. So if you experience similar issues, you better disable XMP or, if you know any better solution, please post a comment about it.

Comments | #hardware Share

# Internet in Poland - My History

Tue
27
Sep 2016

This article at forbes.pl says that yesterday there was a 26-th anniversary of first Internet connection in Poland. On 26 September 1990 scientists made a first connection between Warsaw and Geneva to transfer some data. I thought it might be a good opportunity to write down some memories of my personal beginnings with the Internet. I think it can be interesting to some younger readers that know only the modern Internet as it looks today, as well as to some foreigners, because history of the Internet it other countries may be a little bit different than in Poland.

I know there were things before, like people dialing specific numbers and connecting to so called BBS-es, but my first experiences were already dealing with "this" global Internet. I was in high school back then. At first I started to go to Internet cafes - venues throughout the city where you paid per hours you could spend working on a computer connected to the global network, and possibly downloading some files to your floppy disks. Going there after (or instead of :) school, I first learned how to use IRC and of course WWW. IRC was a protocol that required a client app (mIRC was the most popular one for Windows) and allowed to chat with people, privately or on numerous topic channels, so it was possible e.g. to meet local girls in my city :)

Of course the Web existed already too, with many pages about programming that I've been reading to learn Delphi and download some new components for it. There was no all-knowing Google then, not to mention StackOverflow. Instead there were multiple competing search engines (e.g. Yahoo, AltaVista, Infoseek, Lycos, HotBot) and their algorithms were not so good yet. Page directories were also popular, with manually managed lists of websites grouped into categories and subcategories. Many people created websites about the topics of their interest, like "John's website about programming", or about fishing, or whatever. Pages looked different than today. Their style was to be later called "Web 1.0", with the use of HTML frames, textured backgrounds and animated GIF-s.

 

 

 

Comments | #history #web Share

# Pitfalls of Floating-Point Numbers - Slides

Sat
24
Sep 2016

Just as I announced in my previous blog post, today I gave a lecture on a "Kariera IT" event - organized by CareerCon, dedicated to IT jobs.

Here you can find slides from my presentation, in Polish. It's called "Pułapki liczb zmiennoprzecinkowych" ("Pitfalls of floating-point numbers").

Here are links to the Floating-Point Formats Cheatsheet (in English) that I mentioned in my presentation:

Comments | #events #teaching #math Share

# Pitfalls of Floating-Point Numbers - My Lecture on CareerCon

Fri
19
Aug 2016

CareerCon is an event organized in various cities in Poland, dedicated to IT jobs, e.g. for programmers. You can find there many companies advertising their job offers. Entrance is free, but requires previous registration on their website. There are also some presentations every time.

24 September 2016 the event will take place in Sopot, where I will give a lecture "Pułapki liczb zmiennoprzecinkowych" ("Pitfalls of floating-point numbers"). I will talk about properties of floating-point data types, which are the same regardless of the programming language you use. I will show their limitations, common mistakes to avoid and some good practices. If you are a professional programmer or a student interested in career in IT, I'd like to invite you to come and listen.

Comments | #events #teaching Share

# How to disable C++ exception handling using macros?

Thu
28
Jul 2016

Some time ago my colleague showed me a clever way to disable exception handling in C++ using a set of preprocessor macros:

#define throw
#define try          if(true)
#define catch(...)   if(false)

Please note that:

So following code that uses exception:

try
{
    // Do something
    if(somethingFailed)
        throw std::exception("Message.");
}
catch(const std::exception& e)
{
    // Handle the exception.
}

Will resolve after defining these three macros to following:

if(true)
{
    // Do something
    if(somethingFailed)
        std::exception("Message.");
}
if(false)
{
    // Handle the exception.
}

Of course it's not a solution to any real problem, unless you just want your try...catch blocks to stop working. Disabling exception handling for real (and associated performance penalty, as well as binary code size overhead) is the matter of compiler options. And of course this trick makes errors not handled properly, so when an exception would be thrown, the program will just continue and something bad will happen instead.

But I think the trick is interesting anyway, because it shows how powerful C++ is (it empowers you to do stupid things :)

Comments | #c++ Share

Pages: > 1 ... 20 21 22 23 24 ... 150 >

[Download] [Dropbox] [pub] [Mirror] [Privacy policy]
Copyright © 2004-2026