Question

Sample code:

float** a;  
a = (float**) malloc(numNodes * sizeof(float*));  
for(int i=0; i<`numNodes`; i++)  
{  
    a[i] = (float*)malloc((numNodes-1) * sizeof(float));  
}

I am creating a dynamic 2d array above. Before populating, I noticed that each block in the array already holds this value: -431602080.000000 and not NULL. Why is this?
There are situations where not all spaces within the array are used.
So, my query is simple, Is there an elegant way to check if the each block has this default value or a user defined value?

Thanks in advance.

Was it helpful?

Solution

In C automatic variables doesn't get automatically initialized. You need to explicitly set your variable to 0, if it's what you want.

The same is true for malloc that does'n initialize the space on the heap it allocates. You can use calloc if you want to initialize it:

a = malloc( numNodes*sizeof(float*) ); // no need to initialize this
for ... {
  a[i] = calloc( numNodes-1, sizeof(float) );
}

OTHER TIPS

The content of memory allocated with malloc (as well as of variables allocated on the stack) is undefined, so it may very well be anything. Usually you get space filled with zeroes (because the OS blanks memory pages that were used by other processes) or residues of the previous use of those memory pages (this is often the case if the memory page belonged to your process), but this is what happens under the hood, the C standard does not give any guarantees.

So, in general there's no "default value" and no way to check if your memory has been changed; however you can init the memory blocks you use with magic values that you're sure that will not be used as "real data", but it'll be just a convention internal to your application.

Luckily, for floating point variables there are several magic values like quiet NaN you can use for this purpose; in general you can use the macro NAN defined in <math.h> to set a float to NaN.

By the way, you shouldn't read uninitialized floats and doubles, since the usual format they are stored in (IEEE 754) contains some magic values (like the signaling NaN) that can raise arithmetic exceptions when they are read, so if your uninitialized memory happens to contain such bit pattern your application will probably crash.

C runtimes are not required to initialize any memory that you didn't initialize yourself and the values that they hold are essentially random garbage left over from the last time that memory was used. You will have to set them all to NULL explicitly first or use calloc.

Extending the good answer of Matteo Italia:

The code of initialization of a single array would look like:

float* row;

row = malloc( numNodes*sizeof(float) );
for (int i=0; i<numNodes; ++i) {
    row[i] = nanf(); // set a Not-a-Number magic value of type float
}

(I'll leave it up to you to change this for your multi-dimensional array)

Then somewhere:

float value = ...; // read the array
if (isnan(value)) {
    // not initialized
} else {
    // initialized - do something with this
}

One thing is important to remember: NaN == NaN will yield false, so it's best to use isnan(), not == to test for the presence of this value.

Before populating, I noticed that each block in the array already holds this value: -431602080.000000 and not NULL. Why is this?

malloc() doesn't initialize the memory which it allocates. You need to use calloc() if you want 0 initialization

void *calloc(size_t nelem, size_t elsize);

The calloc() function allocates unused space for an array of nelem elements each of whose size in bytes is elsize. The space shall be initialized to all bits 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top