Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBacterius

Posted 15 January 2013 - 01:02 AM

Well, it's undefined behaviour, so technically, while there is no guarantee it will work, there is no guarantee it will fail, either.

 

I suggest using it the right way, the reason it worked here is probably because int is a simple data type. If you were using a more complicated class, the new[] will create each element of the array as expected, and the delete[] will call the destructor of each element, whereas the delete would just free the array without calling the destructor for each element, which may result in a memory leak and possibly much worse errors. Again, just because it works, doesn't mean it's correct.

 

Also, I suppose the lack of compiler warnings comes from the fact that it's not always possible to distinguish an array from a pointer to a single object, but I could be wrong on this.


#3Bacterius

Posted 15 January 2013 - 12:31 AM

Well, it's undefined behaviour, so technically, while there is no guarantee it will work, there is no guarantee it will fail, either.

 

I suggest using it the right way, the reason it worked here is probably because int is a simple data type. If you were using a more complicated class, the new[] will create each element of the array as expected, and the delete[] will call the destructor of each element, whereas the delete would just free the array without calling the destructor for each element, which may result in a memory leak and possibly much worse errors. Again, just because it works, doesn't mean it's correct.

 

Also, I suppose the lack of error comes from the fact that it's not always possible to distinguish an array from a pointer to a single object, but I could be wrong on this.


#2Bacterius

Posted 15 January 2013 - 12:31 AM

Well, it's undefined behaviour, so technically, while there is no guarantee it will work, there is no guarantee it will fail, either.

 

I suggest using it the right way, the reason it worked here is probably because int is a simple data type. If you were using a more complicated class, the new[] will create each element of the array as expected, and the delete[] will call the destructor of each element, whereas the delete would just free the array without calling the destructor for each element, which may result in a memory leak and possibly much worse errors. Again, just because it works, doesn't mean it's correct.

 

Also, I suppose the lack of error comes from the fact that it's not always possible to distinguish an array from a pointer, but I could be wrong on this.


#1Bacterius

Posted 14 January 2013 - 11:59 PM

Well, it's undefined behaviour, so technically, while there is no guarantee it will work, there is no guarantee it will fail, either.

 

I suggest using it the right way, the reason it worked here is probably because int is a simple data type. If you were using a more complicated class, the new[] will create each element of the array as expected, and the delete[] will call the destructor of each element, whereas the delete would just free the array without calling the destructor for each element, which may result in a memory leak and possibly much worse errors. Again, just because it works, doesn't mean it's correct.


PARTNERS