473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

in overloading problem reference against pointer

Why if I use:
Array *Array::operator=( const Array *right )
{
if ( right != this )
{ // check for self-assignment

// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right->size )
{
delete [] ptr; // reclaim space
size = right->size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right->ptr[ i ]; // copy array into
object*/
}
return this; // enables x = y = z;
}
I have a
*** glibc detected *** cpp_book_test: double free or corruption
(fasttop): 0x09b3

and if I use the same codebut with references arguments I have not
that error:
Array &Array::operator=( const Array &right )
{
if ( right != this )
{ // check for self-assignment

// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right.size )
{
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}

for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object*/
}

return *this; // enables x = y = z;
}

Thanks

Feb 16 '07 #1
3 2237
josh wrote:
Why if I use:
Array *Array::operator=( const Array *right )
That's quite an unusual assignment operator. Why don't you use references?
{
if ( right != this )
{ // check for self-assignment

// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right->size )
{
delete [] ptr; // reclaim space
size = right->size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right->ptr[ i ]; // copy array into
object*/
}
return this; // enables x = y = z;
}
I have a
*** glibc detected *** cpp_book_test: double free or corruption
(fasttop): 0x09b3
Do you ensure that ptr is always either a valid pointer or a null pointer?
Otherwise, delete [] might choke.
and if I use the same codebut with references arguments I have not
that error:
Array &Array::operator=( const Array &right )
{
if ( right != this )
You probably mean:

if ( &right != this )
{ // check for self-assignment

// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right.size )
{
delete [] ptr; // reclaim space
size = right.size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}

for ( int i = 0; i < size; i++ )
ptr[ i ] = right.ptr[ i ]; // copy array into object*/
}

return *this; // enables x = y = z;
}
Feb 16 '07 #2
On 16 Feb, 11:43, Rolf Magnus <ramag...@t-online.dewrote:
josh wrote:
Why if I use:
Array *Array::operator=( const Array *right )

That's quite an unusual assignment operator. Why don't you use references?
Oh yes I do it only for testing purpose and to see how the compiler
"answer me"
>

{
if ( right != this )
{ // check for self-assignment
// for arrays of different sizes, deallocate original
// left side array, then allocate new left side array.
if ( size != right->size )
{
delete [] ptr; // reclaim space
size = right->size; // resize this object
ptr = new int[ size ]; // create space for array copy
assert( ptr != 0 ); // terminate if not allocated
}
for ( int i = 0; i < size; i++ )
ptr[ i ] = right->ptr[ i ]; // copy array into
object*/
}
return this; // enables x = y = z;
}
I have a
*** glibc detected *** cpp_book_test: double free or corruption
(fasttop): 0x09b3

Do you ensure that ptr is always either a valid pointer or a null pointer?
Otherwise, delete [] might choke.
and if I use the same codebut with references arguments I have not
that error:
Array &Array::operator=( const Array &right )
{
if ( right != this )

You probably mean:

if ( &right != this )
yes
but why in "the pointer case" I have that error? it seems the same
error we can
have when we try to assign an object to another and the one contains a
dynamic variable
allocated ...

Feb 16 '07 #3
Oh I found the error.

If I use a pointer or a reference notations nothing is changing but if
in my code I write (and in fact I wrote so...)

a1 = a2

than the compiler try to find a function in which there is an argument
of type Array or
Array& and if not than it make a deep-copy of a2 members in a1 members
and as in
a2 there is an object of type int* then it generates that errors as
both have the same
allocated memory and when a1 or a2 go out of the main the delete[] in
the destructor
function is called twice........

to resolve we MUST write

a1 = &a2

sorry for my "didn't see" ERROR

Feb 16 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Joe Seigh | last post by:
Is there a good write on this. The textbooks I have fluff over on this? Specifically, I trying to dereference with 2 levels of type conversion not 1, i.e. X<T> -> z => Y<T> -> z => T* ->...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
6
by: TuxC0d3 | last post by:
Hi! I'm diving into the some more ++ specific aspects of c++ (and finally accepting that c++ is more than "a plus added to c" :), so that means using namespaces, templates, std::strings, lists,...
6
by: Massimo Soricetti | last post by:
Hello, recently I wrote a little class which has to wrap two different type of data, showing the same external interface. I used operator overloading, but the same result I could eventually...
3
by: md | last post by:
Hi, the following code is working for static objects. ie the statement IntArray x(20); my problem is i want to use this overloading operator for dynamically created objects...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
16
by: Joseph Paterson | last post by:
Hello, I've created a class to store 2 dimensional matrices, and I've been trying to overload the operator, so access to the elements of the matrix is easy. I have 3 private variables, _m, _row...
11
by: dascandy | last post by:
Hello, I was wondering, why is overloading operator. (period) forbidden? It would make a few odd applications possible (dynamic inheritance and transparent remote method invocation spring to my...
7
by: Rahul | last post by:
Hi Everyone, I was overloading the operator= function as a class member function, #include <iostream.h> class A { int value; public : A& operator = (const A& ref)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.