﻿# V572\. Object created using 'new' operator is immediately cast to another type\. Consider inspecting the expression\.

The analyzer detected a potential error: an object created by the 'new' operator is explicitly cast to a different type\.

For example:

```cpp
T_A *p = (T_A *)(new T_B());
...
delete p;
```

There are three possible ways of how this code has appeared and what to do with it\.

1\) T\_B was not inherited from the T\_A class\.

Most probable, it is an unfortunate misprint or crude error\. The way of correcting it depends upon the purpose of the code\.

2\) T\_B is inherited from the T\_A class\. The T\_A class does not have a virtual destructor\.

In this case you cannot cast T\_B to T\_A because you will not be able to correctly destroy the created object then\. This is the correct code:

```cpp
T_B *p = new T_B();
...
delete p;
```

3\) T\_B is inherited from the T\_A class\. The T\_A class has a virtual destructor\.

In this case the code is correct but the explicit type conversion is meaningless\. We can write it in a simpler way:

```cpp
T_A *p = new T_B();
...
delete p;
```

There can be other cases when the V572 warning is generated\. Let's consider a code sample taken from a real application:

```cpp
DWORD CCompRemoteDriver::Open(HDRVR,
  char *, LPVIDEO_OPEN_PARMS)
{
  return (DWORD)new CCompRemote();
}
```

The program handles the pointer as a descriptor for its purposes\. To do that, it explicitly converts the pointer to the DWORD type\. This code will work correctly in 32\-bit systems but might fail in a 64\-bit program\. You may avoid the [64\-bit error](https://pvs-studio.com/en/blog/terms/0002/) using a more suitable data type DWORD\_PTR:

```cpp
DWORD_PTR CCompRemoteDriver::Open(HDRVR,
  char *, LPVIDEO_OPEN_PARMS)
{
  return (DWORD_PTR)new CCompRemote();
}
```

Sometimes the V572 warning may be aroused by an atavism remaining since the time when the code was written in C\. Let's consider such a sample:

```cpp
struct Joint {
  ...
};
joints=(Joint*)new Joint[n]; //malloc(sizeof(Joint)*n);
```

The comment tells us that the 'malloc' function was used earlier to allocate memory\. Now it is the 'new' operator which is used for this purpose\. But the programmers forgot to remove the type conversion\. The code is correct but the type conversion is needless here\. We may write a shorter code:

```cpp
joints = new Joint[n];
```