In this section:

Overview

The use of uninitialized memory is a difficult problem to isolate because the effects of the problem may not show up until much later. Many references to uninitialized memory are harmless, which further complicates the problem.

Insure++ distinguishes two sub-categories of the READ_UNINIT_MEM error class

The difference between these two categories is illustrated in the following examples.

Full checking may be disabled by setting the checking_uninit off option. See Configuration Options (psrc) for details.

If full uninitialized memory checking is disabled, uninitialized pointers will still be detected, but will be reported in the READ_UNINIT_PTR category.

CodeDescriptionEnabledReportedPlatform
READ_UNINIT_MEMReading uninitialized memory(error)RuntimeWindows/Unix
(copy)

Copy from uninitialized region

(error)RuntimeWindows/Unix
(read)

Use of uninitialized value

(tick)RuntimeWindows/Unix


Problem 1

The following code attempts to use a structure element which has never been initialized.

/*
 * File: readuni1.c
 */
#include <stdio.h>

main()
	{
		struct rectangle {
		int width;
		int height;
	};

	struct rectangle box;
	int area;

	box.width = 5;
	area = box.width*box.height;
	printf("area = %d\n", area);
	return (0);
}

Diagnosis at Runtime

[readuni1.c:17] **READ_UNINIT_MEM(read)**
>>		area = box.width * box.height;
Reading uninitialized memory: box.height
Stack trace where the error occurred:
		main() readuni1.c, 17

Problem 2

The following code assigns the value b using memory returned by the malloc system call, which is uninitialized.

/*
 * File: readuni2.c
 */
#include <stdlib.h>

main()
{
	int *a = (int *)malloc(5);
	int b;

	b = *a;
	return (0);
}

Diagnosis at Runtime

[readuni2.c:11] **READ_UNINIT_MEM(copy)**
>>			 b = *a;
Reading uninitialized memory: *a
In block: 0x00062058 thru 0x0006205c (5 bytes)
			block allocated at:
				malloc() (interface)
				main() readuni2.c, 8
Stack trace where the error occurred:
			main() readuni2.c, 11

Repair

The READ_UNINIT_MEM(copy) error category is suppressed by default, so you will normally only see errors in the read category. In many cases, these will be errors that can be simply corrected by initializing the appropriate variables. In other cases, these values will have been assigned from other uninitialized variables, which can be detected by unsuppressing the copy sub-category and running again.