Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: auto_ptr fo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 15 of 16 Topic 9517 of 9831
Post > Topic >>

Re: auto_ptr for array of built-ins

by Pavel Minaev <int19h@[EMAIL PROTECTED] > Apr 20, 2008 at 03:48 PM

On Apr 19, 1:02 am, Carlos Moreno <cm_n...@[EMAIL PROTECTED]
> wrote:

> I guess my question about the UB was not specific enough;
> I mean, yes, allocating with new [] and releasing with
> delete *is* UB...  My question was (or should have been):
> *in practical terms*, does this undefined behaviour goes
> all the way down to the memory allocation algorithms used
> by the compiler or the runtime memory-management system?

Yes, it might.

For example, U.B. gives the implementation freedom to do any run-time
checks. One of those could be to insert a flag somewhere in allocated
blocks to indicate array/non-array form of new used to allocate, and
then check it in delete, and assert(false) or throw if it doesn't
match. In fact, even if implementation doesn't do it out of the box, I
could very well overload global operator new/delete in my program for
the same purpose, and I am justified to expect any libraries I use to
handle this correctly:

void* operator new (std::size_t size)
{
	char* result = static_cast<char*>(std::malloc(size + 1)) + 1;
	result[-1] = 0;
	return result;
}

void* operator new[] (std::size_t size)
{
	char* result = static_cast<char*>(std::malloc(size + 1)) + 1;
	result[-1] = 1;
	return result;
}

void operator delete (void* p)
{
	char* block = static_cast<char*>(p);
	assert(block[-1] == 0);
	std::free(block - 1);
}

void operator delete[] (void* p)
{
	char* block = static_cast<char*>(p);
	assert(block[-1] == 1);
	std::free(block - 1);
}

I've actually seen one similar case of "well, but it's not really
U.B., 'cause I'm not doing anything wrong, so it must be okay!" in
real life: a person used &v[0] on vector without checking for empty()
first (and it could be empty there), passing the resulting pointer and
size() elsewhere. His reasoning was that if size()==0, he did not
dereference the pointer afterward, so whatever &v[0] produced in this
case, it would be okay. It worked fine, too, until we migrated to
Visual C++ 2005, which has a lot of checks in STL containers enabled
by default in debug builds; and then we've got assertion failures all
over our code because of that.



-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 16 Posts in Topic:
auto_ptr for array of built-ins
Carlos Moreno <cm_news  2008-04-17 12:43:22 
Re: auto_ptr for array of built-ins
red floyd <no.spam@[EM  2008-04-17 16:19:42 
Re: auto_ptr for array of built-ins
Howard Hinnant <howard  2008-04-17 16:22:24 
Re: auto_ptr for array of built-ins
markus2004x@[EMAIL PROTEC  2008-04-17 19:31:52 
Re: auto_ptr for array of built-ins
Carl Barron <cbarron41  2008-04-17 19:35:28 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-17 19:33:40 
Re: auto_ptr for array of built-ins
Tony Delroy <tony_in_d  2008-04-18 06:13:14 
Re: auto_ptr for array of built-ins
Howard Hinnant <howard  2008-04-18 14:59:22 
Re: auto_ptr for array of built-ins
Carlos Moreno <cm_news  2008-04-18 15:02:34 
Re: auto_ptr for array of built-ins
Pete Becker <pete@[EMA  2008-04-18 17:16:33 
Re: auto_ptr for array of built-ins
Lance Diduck <lancedid  2008-04-18 17:17:38 
Re: auto_ptr for array of built-ins
mark.zaytsev@[EMAIL PROTE  2008-04-18 17:18:22 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-19 02:02:07 
Re: auto_ptr for array of built-ins
"Bo Persson" &l  2008-04-19 18:13:15 
Re: auto_ptr for array of built-ins
Pavel Minaev <int19h@[  2008-04-20 15:48:01 
Re: auto_ptr for array of built-ins
"Martin T." <  2008-04-22 11:13:23 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 2:52:59 CDT 2008.