-
March 14th, 2009, 09:39 AM
#1
Simple dynamic array question
I'm wondering how "delete[]" works, and if the following code does what I expect:
string* A = new string[10];
string* B = new string[20];
// now swap
string* temp = A;
A = B;
B = temp;
// do interesting stuff, then delete
delete []A;
delete []B;
I'm hoping the above "works" and that the correct sizes of arrays are deleted.
Obviously "delete[]" knows how many elements to delete somehow. But where is this stored? With the ptr? In the heap array allocation?
Storing with the ptr violates what I know about C/C++ ptrs. Storing with the heap array allocation means that normal ptr arithmetic for array elements would break. Is it maybe stored as a negative offset from the first array element?
Many thanks for any enlightenment. (Normally, I would just use a Vector or similar, but I want to understand what's going on behind the scenes here.)
-- penguinFan
-
March 14th, 2009, 09:48 AM
#2
Re: Simple dynamic array question
The Standard says nothing about where the size should be stored, so this depends on your compiler. For example, it can be stored just before the allocated memory. But it's never IN the array.
-
March 14th, 2009, 10:19 AM
#3
Re: Simple dynamic array question
OK, got it. But is my code correct? A simplified version goes this way:
string *A = new string [20];
string *B = A;
delete [] B;
Is the system going to be able to figure out how big the array is that B points to?
-
March 14th, 2009, 10:21 AM
#4
Re: Simple dynamic array question
Yes, that's fine.
You should consider using std::vector rather than using dynamic arrays directly. It will make your code cleaner.
-
March 14th, 2009, 10:37 AM
#5
Re: Simple dynamic array question
Thanks for the confirmation!
BTW I do use std::vector in normal life. I'm just trying to understand how dynamic arrays work here.
A related question: if the extent of a dynamic array is guaranteed to be knowable to the run-time, why isn't it accessible via an API call to the programmer? (I know, you didn't design the language ... but maybe there's a philosophical reason?)
Thanks again.
-
March 14th, 2009, 11:42 AM
#6
Re: Simple dynamic array question
Originally Posted by penguinFan
A related question: if the extent of a dynamic array is guaranteed to be knowable to the run-time, why isn't it accessible via an API call to the programmer?
But it is, via the capacity() member function.
EDIT:
Oh wait, you are talking about the dynamically allocated arrays managed manually, rather than dynamic array functionality provided by std::vector. In that case, perhaps a possible answer is that such knowledge is likely to be implementation dependent, and hence unsuitable for a standard library.
Last edited by laserlight; March 14th, 2009 at 11:44 AM.
-
March 14th, 2009, 12:47 PM
#7
Re: Simple dynamic array question
Code:
string *A = new string [20];
string *B = A;
delete [] B;
Do you really think that delete [] B will delete the array? It will not. I will just delete the pointer *B, with a size of 4 bytes.
Try this:
Code:
int *v=new int[10];
v[1]=7;
int *ptrv=v;
delete [] ptrv;
cout<<v[1]<<endl;
-
March 14th, 2009, 12:48 PM
#8
Re: Simple dynamic array question
Eh, I can't think of any reason not to provide a "query allocated size" mechanism. The information has to be there after all. It just wasn't included in the language for whatever reason.
There actually is one for malloc()ed memory, but it shouldn't be used with new'd memory since there's no guarantee that new uses malloc internally.
-
March 14th, 2009, 12:49 PM
#9
Re: Simple dynamic array question
Originally Posted by Tronfi
Code:
string *A = new string [20];
string *B = A;
delete [] B;
Do you really think that delete [] B will delete the array? It will not.
Yes it will.
-
March 14th, 2009, 12:57 PM
#10
Re: Simple dynamic array question
Originally Posted by Lindley
Yes it will.
Why you think so? After delete the pointer, v[1] is still accesible.
-
March 14th, 2009, 01:00 PM
#11
Re: Simple dynamic array question
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
March 14th, 2009, 01:03 PM
#12
Re: Simple dynamic array question
Originally Posted by TheCPUWizard
Most compilers store it in the lower left back corner of the computer. If you look with a microscope you may be able to see them. .
And Lindley is 100* correct in that "delete []" will delete the entire array. If the array elements have a destructor they wil each be fired (of course this does not apply to POD or raw pointers).
ps: before the array is a common location also. Qucik test: new something that is not an array, then new an array, look at the addresses and do some math....
And I guess int doesn't have destructor, does it?
Thank you!
-
March 14th, 2009, 01:16 PM
#13
Re: Simple dynamic array question
More to the point, destructing anything, POD or not, isn't guaranteed to make the values in that region of memory change at all. All it's guaranteed to do is make it inappropriate for you to touch that memory.
-
March 14th, 2009, 01:59 PM
#14
Re: Simple dynamic array question
> More to the point, destructing anything, POD or not, isn't guaranteed to make the values in that
> region of memory change at all. All it's guaranteed to do is make it inappropriate for you to
> touch that memory
I would hope that it would guarantee that the dynamic storage will not become a memory leak :-)
-
March 14th, 2009, 02:08 PM
#15
Re: Simple dynamic array question
Originally Posted by penguinFan
> More to the point, destructing anything, POD or not, isn't guaranteed to make the values in that
> region of memory change at all. All it's guaranteed to do is make it inappropriate for you to
> touch that memory
I would hope that it would guarantee that the dynamic storage will not become a memory leak :-)
WHY do you think it should????
Code:
class Foo
{
public:
Foo() { m_Leak = new char[100]; }
~ Foo() { }
private: char *m_Leak;
}
Foo *fp = new foo[100];
delete [] fp;
The bold line can not possibly prevent the leakage.....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|