﻿# Use of uninitialized memory

The use of uninitialized memory is one of the most common errors in languages with manual memory management\. The purpose of this process is to read data from an allocated buffer that hasn't yet been filled with initial values\. The program behavior is a bug that is sometimes difficult to detect\. This is the so\-called "[Heisenbug](https://en.wikipedia.org/wiki/Heisenbug)"\. Whether it appears or not depends on the version of the compiler or OS we're using, and whether we're running a "debug" or "release" build\.

Most often, the error occurs due to an incorrect initialization order or synchronization error in a multithreaded application\. Nevertheless, the data is used before it is initialized\.

Let's take a look at such an example:

```cpp
dgCollisionCompoundBreakable::dgCollisionCompoundBreakable(....)
{
  ....
  dgInt32 faceOffsetHitogram[256];
  dgSubMesh* mainSegmenst[256];
  ....
  memset(faceOffsetHitogram, 0, sizeof(faceOffsetHitogram));
  memset(mainSegmenst, 0, sizeof(faceOffsetHitogram));
  ....
}
```

The error occurs because of the incomplete initialization of the _mainSegments _array\. In the second call of the _memset_ function, someone made a mistake in the third argument and passed the size of the _faceOffsetHitogram_ array\. The code will properly function only when it compiles on a 32\-bit platform, where the pointer size matches the size of the _dgInt32_ type\. The program won't operate properly when it compiles on a 64\-bit platform\.

**References**

1. [Wikipedia\. Uninitialized variable](https://en.wikipedia.org/wiki/Uninitialized_variable)
1. [Uninitialized variable](https://pvs-studio.com/en/blog/terms/0078/)
1. [COVID\-19 research and uninitialized variable](https://pvs-studio.com/en/blog/posts/cpp/0796/)