|
-
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
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
|