简体   繁体   中英

How to correct warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

I have a class with a buffer which is later filled from reading a file:

char* m_buffer;

... and in the class destructor I perform the following:

int i;
for(i=0; i < m_size; i++) {
  delete (char*)m_buffer[i];
}
delete m_buffer;

I am receiving a compiler warning at delete (char*)m_buffer[i] :

warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

I've read a few of the other questions regarding similar issues but I'm not able to understand how those solutions apply.

Update and to answer comments :
m_buffer is initialized as follows:

m_buffer = new char[m_size];

If m_buffer is an array then simply use:

delete [] m_buffer;

There is no need to individually delete its elements.

delete is intended to free an object allocated by new. So the important question is, how do you allocate m_buffer ? In your current code, it seems to be an array, because you iterate over the elements of one and cast every element to a char * before deleting it. If you allocated m_buffer with new m_buffer[m_size] , then the correct way would be delete[] m_buffer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM