Bug in G++ or Visual C++ ?

I

ironfelix

Hi there,
Simple program ( below ) provides to array destructor in G++ second
argument equal to 7 and in Visual C++ second argument, equal to 1. I
think it is a bug in Visual C++. Any comments ?

// copyConstructor.cpp : Defines the entry point for the console
application.
//

#include<stdlib.h>
#include<stdio.h>


class A {
public:
void * A::eek:perator new(size_t size); /* object's creator */
void * A::eek:perator new[](size_t size); /* array of objects creator */
void A::eek:perator delete(void *mem, size_t size); /* object's
destroyer */
void A::eek:perator delete[](void *mem, size_t size); /* array of
objects destroyer */
A::A(); /* constructor */
A::A(const A &src); /* copy constructor */
A &A::eek:perator = ( A &a); /* Assign operator */
A::~A(); /* destructor */
};


void * A::eek:perator new(size_t size){
void *mem;
mem= (void *) malloc( size);
printf("Create dynamic object %p size %d\n", mem, (int) size);
return mem;
};

void * A::eek:perator new[](size_t size){
void *mem;
mem= (void *) malloc( size);
printf("Create dynamic array of objects %p size %d\n", mem, (int)
size);
return mem;
};

void A::eek:perator delete(void *mem, size_t size){
printf("Destroy object %p size %d\n", mem, (int) size);
(void) free((void *)mem);
}

void A::eek:perator delete[](void *mem, size_t size){
printf("Destroy of array of objects %p size %d\n", mem, (int) size);
(void) free((void *)mem);
}

A::A(){
printf("Constructor %p\n", this);
};

A::A(const A &src){
printf("Copy constructor src: %p dst: %p\n", &src, this);
};

A & A::eek:perator = ( A &a){
printf("Assign operator src %p dst %p\n", &a, this);
return a;
};

A::~A(){
printf("Destructor... %p\n", this );
};


static void f(A a){
printf("F.... %p\n", &a);
}

int main(int argc, char ** argv)
{
A *zu;
zu= new A[3];
delete[]zu;

printf("Game over, man\n");
return 0;
}
 
S

Salt_Peter

Hi there,
Simple program ( below ) provides to array destructor in G++ second
argument equal to 7 and in Visual C++ second argument, equal to 1. I
think it is a bug in Visual C++. Any comments ?

<snip>

There is no such thing as an array ctor or an array d~tor. An array is
a primitive container which must simply invoke the default ctor ( and
eventually the d~tor ) of *each* one of its elements. This is done
transparently - a default ctor must be available (thats the law). Also,
there is no such thing as a void pointer in C++ either. You can always
ask your question in a group that deals with C.

So unless you have show the real need to overload operator new, like
allocating members via pointers, consider something a little more
usefull that could store anything with no need for overloading new:

#include <iostream>

template< typename T >
class A
{
T m_t;
public:
A() : m_t()
{
std::cout << "A() \n";
}
A( T t ) : m_t(t)
{
std::cout << "A( T t ) \n";
}
A( const A& copy )
{
std::cout << "A( const A& copy ) \n";
m_t = copy.m_t;
}
~A()
{
std::cout << "~A() \n";
}
A& operator=( const A& rhv )
{
std::cout << "operator= \n";
if (this == &rhv) return *this;
m_t = rhv.m_t;
return *this;
}
friend std::eek:stream& operator<<( std::eek:stream& os, const A< T >& r_a
)
{
os << r_a.m_t;
os << std::endl;
return os;
}
};

int main()
{
// allocating integers on the heap
A< int >* zu = new A< int >[3];
delete [] zu;
std::cout << std::endl;

// allocating characters - or whatever - on the stack
A< char > a('x');
std::cout << "a = " << a;

A< char > b(a);
std::cout << "b = " << b;

A< char > c;
c = b;
std::cout << "c = " << c;

std::cout << std::endl;

return 0;
}

you can replace int and char above with anything, including your own
user-type without having to modify class A in any way. Why allocate
bytes when you can allocate by object-types?
Why hunt bugs when you can guarentee success?

// array w 3 elements
A()
A()
A()
~A()
~A()
~A()

A( T t ) // parametized ctor
a = x
A( const A& copy ) // copy construction
b = x
A()
operator= // op=
c = x

~A()
~A()
~A()

no leaks 6 ctors + 6 d~tors
 
I

ironfelix

Salt_Peter пиÑал(а):
<snip>

There is no such thing as an array ctor or an array d~tor. An array is
a primitive container which must simply invoke the default ctor ( and
eventually the d~tor ) of *each* one of its elements. This is done
transparently - a default ctor must be available (thats the law). Also,
there is no such thing as a void pointer in C++ either. You can always
ask your question in a group that deals with C.

Ok, if there is no void pointers in C++, how come I have seen something
like this in Microsoft C++ manual:

http://msdn2.microsoft.com/en-us/library/248aa748.aspx
 
K

Kai-Uwe Bux

Salt_Peter wrote:
[snip]
There is no such thing as an array ctor or an array d~tor. An array is
a primitive container which must simply invoke the default ctor ( and
eventually the d~tor ) of *each* one of its elements. This is done
transparently - a default ctor must be available (thats the law). Also,
there is no such thing as a void pointer in C++ either. You can always
ask your question in a group that deals with C.
[snip]

I find the remark about void pointers very confusing. As far as I know,
void* is a perfectly valid type in C++. You even can convert T* to and from
void* via static_cast and there are guarantees about round trips. So, what
exactly did you mean by "there is no such thing as a void pointer in C++"?


Best

Kai-Uwe Bux
 
R

Ron Natalie

Hi there,
Simple program ( below ) provides to array destructor in G++ second
argument equal to 7 and in Visual C++ second argument, equal to 1. I
think it is a bug in Visual C++. Any comments ?

operator delete is NOT a destrcutor. It's a memory deallocator. The
destructors have already been run.

It should not be necessary to cast the return of malloc to void*.
It should already be that.

This one is a bit bizarre, but the standard doesn't say anything
about what the value passed to the size_t is in this case. The
void* argument is the one previously returned by operator new[]
but that's the only requirement.
 
I

ironfelix

Ron Natalie пиÑал(а):
operator delete is NOT a destrcutor. It's a memory deallocator. The
destructors have already been run.

Of course. Silly me :)
It should not be necessary to cast the return of malloc to void*.
It should already be that.

Agrred.

This one is a bit bizarre, but the standard doesn't say anything
about what the value passed to the size_t is in this case. The
void* argument is the one previously returned by operator new[]
but that's the only requirement.

But Microsoft documentation is saying: <<The second form takes two
arguments, the first of which is a pointer to the memory block to
deallocate and the second of which is the number of bytes to
deallocate>>. So it suppose to be the same second argument as second
argument for new[] operator. If you run my silly program on VC++ - it
shows different result.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,732
Latest member
ArronPalin

Latest Threads

Top