Why if I use:
Array *Array::operato r=( 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::operato r=( 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 3 2250
josh wrote:
Why if I use:
Array *Array::operato r=( 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::operato r=( 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;
}
On 16 Feb, 11:43, Rolf Magnus <ramag...@t-online.dewrote:
josh wrote:
Why if I use:
Array *Array::operato r=( 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::operato r=( 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 ...
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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* -> z
and
*X<T> => *Y<T> => *T
The Y<T> conversion has to be done as an expression temp. It cannot be done
|
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 must be an object
instead of
|
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, vectors, operator
overloading and what not.. And i was wondering if there is a way to
override the global dereference operator, so to be able to check the
address that one tries to dereference. This gives the ability to throw
an exception when...
|
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
obtain with two classes derived from an abstract base class containing
only the interface functions. So, when it's worth to use overloading
mechanism instead of abstract/inherited?
|
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
for example the statement IntArray *x;
Please give me some help regarding this
//////////////////////////////////////////////////////////////////////////////////////////////////////////
class IntArray
| |
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 memory allocation?
like complex<int> * c1,*c2,*c3;
i still want to do something like c3 = c1+c2 ; rether than *c3 =
*c1+*c2;
|
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 and _col which are
respectively the array to store the data in, the number of rows and the
number of columns. _m is of type T, and is size _row * _col * sizeof
(T).
I've managed to overload the subscript operator like this:
|
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 mind) and it would
be fairly generic. The only sidecase I can see is that operator.
itself would not be looked up through operator. .
I read that there was previous debate on the subject, but I haven't
been able to find why it was rejected.
|
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)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |