C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The new operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
sarathy
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: The new operator Reply with quote



Hi all,
I was wondering how the program below was working. I
overloaded the new operator, which is responsible for allocating memory
for the object. But I made an explicit memory allocation [ zero bytes
]. When the object was created, somehow the value was assigned
correctly and it prints the correct result. How is this possible ???

# include <iostream>



using namespace std;



class A
{
long a;
public:
A(long x) { a=x; }
long getA(){return this->a;}
void* operator new (size_t);
void operator delete (void *);
};



void* A::operator new (size_t size)
{
void *ptr = malloc(0);
return ptr;
}



void A::operator delete (void *p)
{
free(p);
}

int main()
{
A *a=new A(1234567);
cout << a->getA() << endl;
delete a;
return 0;
}
Back to top
John Carson
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: The new operator Reply with quote



"sarathy" <sps.sarathy (AT) gmail (DOT) com> wrote in message
news:1155110050.296751.53470 (AT) h48g2000cwc (DOT) googlegroups.com
Quote:
Hi all,
I was wondering how the program below was working. I
overloaded the new operator, which is responsible for allocating
memory for the object. But I made an explicit memory allocation [
zero bytes ]. When the object was created, somehow the value was
assigned correctly and it prints the correct result. How is this
possible ???

I believe that the result of a malloc call with a zero argument is
implementation defined. malloc can either return a NULL pointer or it can
return a unique pointer to a zero amount of memory. The VC++ docs say this
about malloc:

"If size is 0, malloc allocates a zero-length item in the heap and returns a
valid pointer to that item."

See below.


Quote:
# include <iostream



using namespace std;



class A
{
long a;
public:
A(long x) { a=x; }
long getA(){return this->a;}
void* operator new (size_t);
void operator delete (void *);
};



void* A::operator new (size_t size)
{
void *ptr = malloc(0);
return ptr;
}



void A::operator delete (void *p)
{
free(p);
}

int main()
{
A *a=new A(1234567);

Pointer a ends up pointing somewhere on the heap and then the constructor
writes an A object to the memory that a points to. Because a zero amount of
memory has been allocated, this involves writing on memory that hasn't been
allocated for the purpose, so your application is likely to crash or
otherwise misbehave. However, it may behave well enough for the constructor
to write the integer to A's member variable and for getA() to retrieve it.

Quote:
cout << a->getA() << endl;
delete a;
return 0;
}


--
John Carson
Back to top
Alan Johnson
Guest





PostPosted: Wed Aug 09, 2006 9:10 am    Post subject: Re: The new operator Reply with quote



sarathy wrote:
Quote:
Hi all,
I was wondering how the program below was working. I
overloaded the new operator, which is responsible for allocating memory
for the object. But I made an explicit memory allocation [ zero bytes
]. When the object was created, somehow the value was assigned
correctly and it prints the correct result. How is this possible ???

# include <iostream



using namespace std;



class A
{
long a;
public:
A(long x) { a=x; }
long getA(){return this->a;}
void* operator new (size_t);
void operator delete (void *);
};



void* A::operator new (size_t size)
{
void *ptr = malloc(0);
return ptr;
}



void A::operator delete (void *p)
{
free(p);
}

int main()
{
A *a=new A(1234567);
cout << a->getA() << endl;
delete a;
return 0;
}


What you have is undefined behavior, which means anything could happen,
including appearing to "work" correctly.

--
Alan Johnson
Back to top
Jerry Coffin
Guest





PostPosted: Sun Aug 13, 2006 5:19 am    Post subject: Re: The new operator Reply with quote

In article <44dcded5$0$17541$61c65585@un-2park-reader-
01.sydney.pipenetworks.com.au>, jcarson_n_o_sp_am_ (AT) netspace (DOT) net.au
says...

[ ... ]

Quote:
I am sceptical of this. I can't find anything in the C++ Standard that says
it (and the C standard definitely makes the behaviour implementation
dependent). Further, typing in malloc(0) in Google Groups for this newsgroup
and its moderated counterpart yields various threads in which knowledgeable
people make the same statement that I did or don't contradict other people
when they make the statement.

You're right. The current version of C++ (i.e. the 2003 version) lists
C99 as a normative reference.

In C99 ($7.20.3) it says:

If the size of the space requested is zero, the behavior
is implementation-defined: either a null pointer is
returned, or the behavior is as if the size were some
nonzero value, except that the returned pointer shall not
be used to access an object.

The C++ requirements are covered in section 20.4.6:

3 The functions calloc(), malloc(), and realloc() do not
attempt to allocate storage by calling ::operator new()
(18.4).
4 The function free() does not attempt to deallocate
storage by calling ::operator delete().

So, in C++ (just as in C) malloc(0) can return either a null pointer, or
a some unique non-null pointer.

I suppose if somebody wanted to badly enough, they'd have some (minimal)
foundation for claiming ambiguity in C++ on this point though -- C++
2003 contains an inaccurate cross reference to the wrong section of the
C standard (to section 7.11.2 instead of 7.20.3, where the description
of malloc now lives).

--
Later,
Jerry.

The universe is a figment of its own imagination.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.