In dynamic memory allocation for arrays, can we delete the memory of pointers only for specific parts of the array? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

In dynamic memory allocation for arrays, can we delete the memory of pointers only for specific parts of the array?

for freeing memory we use delete [ ] p; but that is used for freeing up the whole array. can we do so only for specific position in the array like P[20]

15th Sep 2018, 3:51 AM
Sushank Mishra
Sushank Mishra - avatar
11 Answers
+ 4
Ketan Lalcheta &a[0] == (a + 0) == a == base address &a[1] == (a + 1) == base address + 1 ... That proves one important fact. The delete[] does not know the size of the allocated block precisely on the heap while deleting, You might think "Well, I told the new[] to spare 100 elements when I used "new int[100]" ." That's true. But according to the standard (n4292)§3.7.4.1 p.67 " The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the [address of the start of a block of storage] whose length in bytes shall be [at least as large as the requested size]. There are no constraints on the contents of the allocated storage on return from the allocation function. [...]"
16th Sep 2018, 7:32 AM
Babak
Babak - avatar
+ 3
Sushank Mishra I don't think so...array is contiguous memory allocation... I have never tried this.eager to see other responses
15th Sep 2018, 3:53 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
just now checked... int* a = new int [50]; delete &a[0];// works fine.. delete &a[1]; // invalid pointer...error at run time.. aborted (core dump) seems you cannot access the other element for delete except zeroth one
15th Sep 2018, 4:32 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Ace I thought the same..this might delete entire array because in general also, array name itself point to array's first element..
15th Sep 2018, 3:12 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Ketan Lalcheta The error you've got!
16th Sep 2018, 7:36 AM
Babak
Babak - avatar
+ 2
Ketan Lalcheta Since the context is about interaction between new[] and delete[] functions let me wrap up my response by this easy to answer question: Why the language doesn't allow something like "delete[100] a", instead? Now, that we know how the block of the dynamically allocated memory be treated, and its size can be more than the initial allocated size, the answer becomes obvious. Because, delete[] begins to do its housekeeping process from the base address of the array to where the allocator has been defined. it might be 100 or 101 or a greater number.
16th Sep 2018, 8:10 AM
Babak
Babak - avatar
+ 1
I fear that this might create a segmentation fault or something but at the same time I know that I have no knowledge of C++!!!😂😂
15th Sep 2018, 3:55 AM
Sushank Mishra
Sushank Mishra - avatar
+ 1
C++ Soldier (Babak) What you want to convey? I know this what you said..but can't co relate with this question
16th Sep 2018, 7:35 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
C++ Soldier (Babak) yup.. I missed to link with my error... agree but I would like to differ on this.. array size is known as soon as we try to access it where it was defined... it is not proper once we try to get it somewhere else... refer below code : https://code.sololearn.com/cTKDK147biD4/?ref=app correct me if I have misinterpreted something..
16th Sep 2018, 7:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
thanks people
15th Sep 2018, 5:00 AM
Sushank Mishra
Sushank Mishra - avatar
0
In C/C++, we can store objects or pointers to objects in an array. If we store objects in it, then we cannot free an object at a specific index. If we use array to store pointers to objects then we surely can free the objects pointed by the pointers at some positions if we want to. It is like to delete a pointer. delete array[i];
16th Sep 2018, 5:05 AM
Rocky Lee
Rocky Lee - avatar