How to understand 'Uninitialized memory access'?

How to understand 'Uninitialized memory access'?

Dear all,

I have the following code:

...

    node *node3=new node[npoint];
    for (int i=0;i<npoint;i++)
    {
        node3[i].id=i;
        node3[i].x=doublearray[j++];
        node3[i].y=doublearray[j++];
        node3[i].z=doublearray[j++];
    }
    std::qsort(node3,npoint,sizeof(node),comparenode);

...

The Intel Inspector tells me the 'Uninitialized memory access' error at line

    node *node3=new node[npoint];

and qsort function.

How to understand this error? How to modify the code to remove this error message?

 

Thanks,

Zhanghong Tang

 

4 Beiträge / 0 neu
Letzter Beitrag
Nähere Informationen zur Compiler-Optimierung finden Sie in unserem Optimierungshinweis.

>>>The Intel Inspector tells me the 'Uninitialized memory access' error at line>>>

Uninitialized memory access means that pointer which is supposed to contain the valid address was not initialized(does not contain valid address) probably null or some junk.

>>> node *node3=new node[npoint];>>>

Did actually try to run your code? Does it crash?

The quoted example while disassembled node3 pointer to array of objects type node should contain the address to heap allocated memory. The size of node array is the size of npoint elements multiplied by the object type node size.

Btw, that message could be also false positive for example when you earlier initialized *node3 pointer to null and Inspector was not able to track the changes(call to new operator).

 

Hi iliyapolak,

Thank you very much for your kindly reply. The code doesn't crash. I just use Intel Inspector to check possible hidden problems of my code.

Do you mean that I should initialize the pointer like this?

for (int i=0;i<npoint;i++)node3[i]=0;

but I think the 'new' operation has assigned address for every node3[i].

I think I have assigned values for every node3 after the 'new' operation. On the other hand, if I have the following code after the 'new' operation, the error message disappeared.

    memset(node3,0,npoint*sizeof(node));

but I don't think it is necessary since I have assigned values for every node3.

 

That is fine what you are doing . I also run strict rules code analysis from time to time.

I do not think that you need to initialize node array members to null. Operator new initialized *node3 pointer to point allocated heap memory.

IIRC at assembly level your objects are decomposed into primitive member variables (float,double) which are copied probably by vectorized mem copy function.

Kommentar hinterlassen

Bitte anmelden, um einen Kommentar hinzuzufügen. Sie sind noch nicht Mitglied? Jetzt teilnehmen