This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
Closed 6 years ago.
Imagine I have a pointer to an array of integers and then I want to delete it like I do below:
int * numbers = new int[10];
delete[] numbers;
How does the delete operator knows where the array numbers ends to free that memory (since C++ does not keep track of the length of the array as far as I know)?
Thanks!
It can do it however it wants. There are two common ways:
The implementation may use an associative array of allocated pointers mapped to their sizes.
The implementation may allocate a few extra bytes at the beginning to store the size and pass a pointer into the block to the caller.
Related
This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
Closed 3 years ago.
How does delete operator work in C++?
int *ptr = new int[2];
delete[] ptr;
How does delete operator know the amount of memory allocated, since ptr is just a int pointer and increment-ing(++) it would simply make it point to the next location in the continuously allocated structure.
Allocators typically hide allocation information just before the pointer in question. The allocation includes that space, but the pointer is moved after it so you don't access/modify it. This is part of why writing to a pointer at a negative index breaks things so badly.
As noted in the comments, your code is broken as written, since you used delete ptr;, not delete[] ptr;; only the latter knows to look for the information needed to destruct the whole array, not just a single element.
This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
Closed 3 years ago.
when we allocate memory dynamically using new operator for a int data type. it makes sense to use delete operator.
For example
if a code would like bellow :
int *p=new int;
delete p;
Here it makes sense to use delete . Here we can think like this that the block, p points ,delete/de-allocate that memory block .
But for the bellow code :
int *p=new int[5];
delete[] p;
How does it make any sense to use delete[] here. I am asking this because p is not the name of the array. Here p is just a simple pointer which is pointing to the first element of the array memory block. Now how does delete[] works to delete the whole array.As here was not mentioned the size of the array. Then how does the statement delete[] p; delete the whole array.
It's up to the compiler to figure out how to do that. One fairly standard way to do this is to store the size of the array in a technical header that precedes the allocated memory block.
This question already has answers here:
How do I create an array of pointers?
(4 answers)
Closed 6 years ago.
table = new myObject*[TABLE_SIZE];
I know that * are for declaring pointers variable and to obtain the value of the variable from a pointer, but what does this mean?
It means exactly that.
table is a pointer to an array (with size TABLE_SIZE) of pointers to objects of type myObject. Note that you haven't allocated any memory for those pointers at this point.
Don't forget to call delete[] table; once you're done with it.
This means that you allocate an array of type myObject* of size TABLE_SIZE.
So in this case we allocate space for the pointers but not the objects.
Your variable table will then be a pointer to an array of pointers, or a myObject**.
So table will point to the first element in this list of pointers.
It means exactly what you already know it means :-)
The type myObject * is a pointer to a myObject object so what you are declaring is an array of said pointers, TABLE_SIZE of them to be exact.
The asterisk means pointer here too. I am assuming that table is declared like
myObject** table;
What you are doing is allocating an "array" of TABLE_SIZE pointers to myObject.
This question already has answers here:
Dynamically allocating an array of objects
(7 answers)
Closed 8 years ago.
Im learning at the moment c++ with a book.
I have many problems with the pointers...
int i;
cin >> i;
const int *quantity = &i;
int array[*quantity];
Is that correct? Can i control now the size of the array during the programm is running?
That's very nearly correct, except that the size of the array (which you've attempted to allocate on the stack here) has to be known at compile time, so no you can't do this.
However, you can allocate an array at runtime using new[], which will let you pass in a size that's not a compile time constant (new allocates on the heap):
int* array = new int[*quantity];
// ...
delete[] array; // Manual allocations with `new` require manual deallocations
Note that in your particular code example, there's no need to play with pointers at all -- you can just use i directly:
int* array = new int[i];
No, this is incorrect. Array size must be a constant expression, which means that it can be evaluated during compilation, and this is not the case for your code.
If you want arrays of different sizes, you can use dynamic allocation:
int* array = new int[i];
Or much better use std::vector:
std::vector<int> array(i);
Arrays are a fixed size in memory. Because arrays also represent a contiguous block of objects in memory, by definition the same array cannot change size in memory, because it would then need to move memory that may not even belong to the same application.
There are ways to move your array, however, copying it to a new array with more space when it gets full, and there are more mutable types such as std::Vector, but an array as deifned here cannot ever change size.
What this code would instead do is
place a value in i
Place a value in quantity representing the address (pointer) of i
Create a new array using the value in the address quantity
Note that pointers are addresses, specifically saying the byte address in RAM of a given variable (try printing a pointer directly, for example!). The * and & operators quickly say "get value at address" and "get address of"
This question already has answers here:
Can I delete[] a pointer that points into an allocated array, but not to the start of it?
(7 answers)
Closed 9 years ago.
What would happen in the following code?
int *p1 = new int[100];
int *p2 = &p1[50];
delete [] p2;
I've heard that some implementations of new store the size of the array in the (-1)th array index, but then wouldn't things go horribly wrong in the above?
Things would definitely go wrong!
The delete [] operator is defined to only work on proper array pointers. And by proper I mean it must receive a pointer that was previously initialized to point to a location in memory where an array was created with the new operator.
You should also never mix and match new/delete and malloc/free. As a rule always delete memory that has been allocated with new, and free memory that has been allocated with malloc (and derivatives)