Re: Copying objects and arrays
From: Brad Herald (bherald_at_vt.edu)
Date: 10/26/04
- Next message: Adrian: "Re: Reading command line parameters when main does not pass them??"
- Previous message: Jonathan Turkanis: "Re: Your C++ Homework"
- In reply to: Arijit: "Re: Copying objects and arrays"
- Next in thread: Victor Bazarov: "Re: Copying objects and arrays"
- Reply: Victor Bazarov: "Re: Copying objects and arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 26 Oct 2004 17:57:13 -0400
a memory leak occurs when? dynamically allocated space assigned to a
pointer and it isn't deleted before the pointer goes out of scope. If we
wanted to get into the heavy dynamic stuff, I'm game.
int* first = new int(20);
int* second = new int(10);
first = second; // memory leak, the memory is allocated in the heap and
'first' is no longer pointing to it's allocation
//add before the previous line, 'delete [] first;' and then define first
again, int* first = second;
-------------------------------
(using the above declarations)
int length = sizeof(int);
int i=0;
for( i=0; i < 10; i++ )
cout << *(first + i*length); // regardless of what values are in the
memory space, it'll be output
----------------------
(using the above declarations)
int i=0;
for( i=0; i < 10; i++ )
cout << first[i];
--------------------------
The second two examples do the exact same thing.
I was trying to bring out the carefullness required when assigning two
pointers:
1) know what each one points to
2) know if the pointer's destination is static or dynamic
3) if the pointer to be assigned points to something dynamic, delete the
contents or else instantiate another pointer
---------------------------
A linked list is much like an array, except it is not contiguous. The nodes
are scattered throughout memory, but each node (in a working list) has at
least one pointer to the next node and possibly (double linked list) another
pointer to the node which points to it. All of this is for internal nodes
and the first node's previous pointer points to NULL and the last node's
next pointer points to NULL. Unless it is a circular list, that is if you
were to give the command to print out the node's contents and the next in
line without checking for the root of the list, then you get an infinite
loop.
I've written my own linked list (and templated it) and used it in many
programs.
-----------------------------
Lastly, what is an array exactly? Is an array a C++ type?
Take a look at C++ abstract declarators in any good C++ book.
"Arrays are derived types and can therefore be constructed from any other
derived or fundamental type except functions, references, and void." VS.NET
C++ Reference
----------------------------
"Arijit" <pal_ari@yahoo.co.in> wrote in message
news:dff492d.0410260737.3effe78f@posting.google.com...
> "Brad Herald" <bherald@vt.edu> wrote in message
> news:<clk80t$4mp$1@solaris.cc.vt.edu>...
>> arrays are really pointers to data in memory.
>>
>> A[10] is a pointer to 10 ints in memory (at least the space is assigned),
>> contiguous one right after the other.
>>
>> A = B; // this is trying to assign the B array to the A array, but won't
>> work unless it's from a class with a copy constructor.
>>
>> *A = *B; // this will work because it reassigns the A pointer....
>
> No it won't work. This statement will not give a compilation error
> like A=B will, but it won't copy B to A either. All it will do is
> copy the first element of B to first element of A. The rest of A and B
> will remain unchanged.
>
>> /* but this will lead to a memory leak because the array A
>> was
>> assigned to is now stuck in memory without a way to access the values,
>> the
>> array is now lost into the memory. if you do want to have two pointers
>> to
>
> No memory leak occurs. You can't have a memory leak unless you use
> pointers explicitly in your code, or do something fancy with references.
> AFAIK, it is impossible to cause memory leaks without using pointers or
> references. And arrays are *not* pointers.
>
> -Arijit
>
>> the same object, then declare an int* A pointer instead and assign it to
>> B[]
>> what this leads to is two pointers where either one can change the data
>> without the other pointer this can be bad because large programs may have
>> these two pointers and your program may output both at different times
>> leading to something unexpected since they point to the same data. The
>> best
>> way to have two arrays is to implement a copy constructor for a class or
>> simply a copy function if the arrays are not part of a class. */
>>
>> Brad
>>
>>
>> "Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
>> news:S6bfd.6874$Ae.3891@newsread1.dllstx09.us.to.verio.net...
>> > Matteo Settenvini wrote:
>> >> Probably it's me doing just a mess... anyway I'm using a book wrote by
>> >> some italians and also "Thinking in C++".
>> >>
>> >> Sorry to appear so "newbieish" but from somewhere you've to start...
>> >>
>> >> But why this isn't legal, then?
>> >>
>> >> ----------------
>> >> #include<iostream>
>> >>
>> >> int main()
>> >> {
>> >> int A[10], B[10];
>> >> A = B;
>> >> }
>> >
>> > It's not legal because arrays are not assignable. IOW, an object of
>> > type "array of T" cannot appear on the left side of an assignment op.
>> >
>> > V
- Next message: Adrian: "Re: Reading command line parameters when main does not pass them??"
- Previous message: Jonathan Turkanis: "Re: Your C++ Homework"
- In reply to: Arijit: "Re: Copying objects and arrays"
- Next in thread: Victor Bazarov: "Re: Copying objects and arrays"
- Reply: Victor Bazarov: "Re: Copying objects and arrays"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|