This question already has answers here:
How to find the size of an array (from a pointer pointing to the first element array)?
(17 answers)
Closed 9 years ago.
Is there a function (that could be written) which allows to know the size of an array defined with new:
int *a=new int[3];
*a=4;
*(a+1)=5;
*(a+2)=6;
Thanks!
There is not a standard way get the size of an array allocated with new.
A better approach to array allocation is std::vector, which does have a size() member -- and it automatically cleans up after itself when it goes out of scope.
Short answer: No.
Use std::vector instead.
It would be possible to write a function for this. But in the real world, it's a poor idea.
Although the act of calling new most likely stores the number of elements in the array that is allocated (or at least, the size of the actual allocation underneath it), there is no way that you can get that information in a way that doesn't rely on knowing how new works on your particular system, and that could change if you compile your code differently (e.g. debug or release version of the code), change version of the compiler (or runtime library), etc, etc.
Using the std::vector as mentioned is a much better way, since you then ALSO don't have to worry about freeing your array somewhere else.
If, for some reason, you don't want to [or have been told by your tutor, that you can't] use std::vector, you need to "remember" the size of the allocation.
Related
This question already has answers here:
Is it required to check a pointer validity if new fails?
(4 answers)
Closed 4 years ago.
I don't know a lot about C or C++ but I think with C when you create an array you use malloc to ask for the memory and then you check that the memory was allocated before assigning values. In C++ you implement an array using new instead. Would check the allocation of memory for the array in C++ the same as in C by checking that the array is not null?
For example,
int main()
{
int* myArr = new int[10];
if(myArr!=NULL)
{
//DO SOMETHING
}
}
I get that most computers have a lot of memory, making running out of memory less likely today, but I also understand that failing to do things like this can lead to unexpected bugs later on down the road.
UPDATE: I was trying to keep my example simple. As mentioned in the comments I was referring to dynamic memory allocation. I am trying to implement a heap data structure. As part of the heap there exists an array to store the values of the heap. When the heap is full the array has to be expanded by design by the next power of 2. Thus i may initialize the heap as 10 but when I reach 10 I will need to expand to 16. Given enough items the heap will reach size 2^n. Therefore I am calling a function that has a parameter for the HEAP pointer. Then I create a new array and copy the values from the existing array to the new array. While doing this I was thinking about what happens if the new array cannot be created because of being out of memory. I may want to write the values to a file before exiting.
No, to make an array you do this:
int main()
{
int myArr[10];
// DO SOMETHING
}
If you must use dynamic allocation, use new without the std::nothrow specifier, so that it throws a std::bad_alloc exception if the allocation fails. That is ultimately the equivalent of checking for malloc returning a NULL pointer.
If you do use the std::nothrow specifier then, yes, check for a NULL pointer. In modern C++, that's nullptr. In antique C++, that's NULL (or 0) as you've shown.
This question already has answers here:
Calling delete on variable allocated on the stack
(12 answers)
Closed 8 years ago.
I know that C++ holds memory management internally with a lot of given points, and I know of the delete command to remove dynamically allocated data, and this question, could seem pointless within the fact that it may not ever come to be an issue due to the destruction of variables outside of the scope of the function that is using them, but is it possible to use a function such like delete to remove a variable that the user is no longer putting to use.
Like say that you are on a heavily memory depleted piece of hardware, and need to make sure that even something as small as the 4 bytes that an integer normally takes up are given straight back. Is it possible to do this without wrapping the variable inside some function to make the assembler know to remove it immediately?
This is in a sense of a point that I don't believe could ever happen, due to the expansion of memory, and the ways that it could be manipulated these days, but it seems as if it may have been an issue before, if I'm not mistaken.
Summary:
Is there a way to manage non dynamic data directly, allocate it to the stack, and remove it from the stack through a function call, or is this completely run by the programs internal instructions?
Example:
void foo(){
short int operator;
/*Did what needed to be done with the operator variable***********/
//Pseudo-code
delete operator;
/*Even though it was not allocated dynamically,
and with the use of another function call*/
}
As you said, easy way of doing this would just be to put variable in lower scope and allow it to free itself.
If it's any container, you can clear it with appropriate function call.
Basically:
delete what you new, delete[] what you new[].
Also see this:
https://stackoverflow.com/a/441837/2975193
This question already has answers here:
How does delete[] "know" the size of the operand array?
(9 answers)
How does delete[] know it's an array?
(16 answers)
Closed 9 years ago.
int* i = new int[4];
delete[] i;
While we call delete[], how does the program know "i" is 4 byte-length. Is 4 be stored in somewhere in memory?
The implementation of delete[] depend on System or Compilers?
Is there some System API to get the length of i?
As HadeS said, which will hold the information how much memory has been allocated? And where?
It must be hold in memory, or maybe nearby the pointer i.
First off, i is not "4-byte length". Rather, i is a pointer to an array of four ints.
Next, delete[] doesn't need to know anything, because int has no destructor. All that has to happen is that the memory needs to be freed, which is done by the system's allocator. This is the same situation as with free(p) -- you don't need to tell free how much memory needs to be freed, since you expect it to figure that out.
The situation is different when destructors need to be called; in that case, the C++ implementation does indeed need to remember the number of C++ objects separately. The method for this is up to the implementation, although many compilers follow the popular Itanium ABI, which allows linking together of object code compiled by those different compilers.
There is no way for you to query this information. You should consider dynamic arrays a misfeature of C++; there is essentially no reason to use them*, and you can always do better with some kind of class that manages memory and object separately and individually: Since you'll have to remember the number of array elements anyway, it's much better to encapsulate the size and the allocation in one coherent class, rather than have vague dynamic arrays that you cannot really use without passing extra information along anyway (unless you had self-terminating semantics, but then you'd just be using the extra space for the terminator).
*) And there are at least two standard defects about dynamic arrays that nobody is too bothered to worry about fixing
When you dynamically allocate a memory; compiler allocates an extra block of memory apart from what you have asked, which will hold the information how much memory has been allocated.
when you try to delete this memory using delete this extra block of memory will be read by the compiler to see how much memory was allocated and free the space accordingly.
I don't think there is any API which will fetch this information.
This question already has answers here:
Why should C++ programmers minimize use of 'new'?
(19 answers)
Closed 9 years ago.
Say I have two sets of code,
std::vector<float>v1;
and
std::vector<float> *pV2 = new std::vector<float>(10);
What is the difference between the two other than the fact that you will have a larger chunk of memory allocated with the pointer to the vector? Is there an advantage to one vs. the other?
In my mind, it seems like allocating the pointer is just more of a hassle because you have to deal with deallocating it later.
What is the difference between the two other than the fact that you will have a larger chunk of memory allocated with the pointer to the vector?
'will have a larger chunk of memory allocated'
This isn't necessarily true! The std::vector might choose a much larger default initial size for the internally managed data array than 10.
'What is the difference between the two'
The main difference is that the 1st one is allocated on the local scopes stack,
and the 2nd one (usually) goes to the heap. Note: The internally managed data array goes to the heap anyway!!
To ensure proper memory management when you really have to use a std::vector<float>* pointer allocated from the heap, I'd recommend the use of c++ smart pointers, e.g.:
std::unique_ptr<std::vector<float> > pV2(new std::vector<float>(10));
For more details have a look at the documentation of <memory>.
One of the critical differences is scope. In your first example, the vector will probably either be a member of a class, or it will be local to a function. If it's a class member, it will be destroyed when the containing object is destroyed. If it's local to a function, it will be destroyed when the function ends. The object absolutely cannot exist beyond that, so you have to be very careful if you try passing its address to another part of your program.
When you manually allocate something on the heap instead, it will exist for as long as you want. You're in complete control of the deallocation, which means you can create it in one object/function, and use or delete it in another whenever you need to.
It's also quite useful in various situations to be able to delay instantiation of an object until it's actually required. For example, it may need different construction parameters depending on user input, or you may want to take advantage of polymorphism (i.e. decide at runtime which sub-class to instantiate).
Another key difference for some situations is available memory. If you create an object locally to a function, it will reside on the stack. There is a lot less space available on the stack than on the heap, so you can run into difficulties when using particularly large objects (although that won't happen with a vector because it allocates on the heap internally anyway).
It's worth noting that the actual amount of memory used by the object is the same, whether it's on the stack or on the heap. The only difference is that if you manually allocate something on the heap, then you will also have a pointer to it. That's only an extra 4 or 8 bytes though, which is negligible in most cases.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why [] is used in delete ( delete [] ) to free dynamically allocated array?
Why does C++ still have a delete[] AND a delete operator?
I'm wondering what's their difference and I know the obvious answer some might say, that one is to delete an array and the other is to delete a single object but I'm wondering why should there be two different deletion methods for these two operations? I mean delete is basically implemented using C free method which doesn't care if the pointer is actually pointing toward an array or a single object. The only reason I can think of is two be able to know if it's an array and call destructor for each cell instead of only the first object but that wouldn't also be possible since compiler can not guess the length of array just looking at it's pointer. By the way though it's said to invoke undefined behavior to call delete for memory allocated with new[] I can't imagine anything that could possibly go wrong.
As you have discovered the compiler needs to know the length of an array (at least for non-trivial types) to be able to call destructors for each element. For this new[] typically allocates some extra bytes to record the element count and returns a pointer to the end of this bookkeeping area.
When you use delete[] the compiler will look at the memory before the array to find the count and adjust the pointer, so that the originally allocated block is freed.
If you use delete to destroy a dynamically allocated array, destructors for elements (except the first) won't be called and typically this will end up attempting to free a pointer that doesn't point to the beginning of an allocated block, which may corrupt the heap.
but that wouldn't also be possible since compiler can not guess the
length of array just looking at it's pointer
That's not really true. The compiler itself doesn't need to guess anything, but it does decide which function to call to free the memory based on the operator it sees. There is a separate function dedicated to releasing arrays, and this function does indeed know the length of the array to be freed so it can appropriately call destructors.
It knows the length of the array because typically new[] allocates memory that includes the array length (since this is known on allocation) and returns a pointer to just the "usable" memory allocated. When delete[] is called it knows how to access this memory based on the pointer to the usable part of the array that was given.
When you allocate memory using new[], the compiler not only needs to construct each element, it also needs to keep track of how many elements have been allocated. This is needed for delete[] to work correctly.
Since new and delete operate on scalars, they don't need to do that, and could save on a little bit of overhead.
There is absolutely no requirement for new to be compatible with delete[] and vice versa. Mixing the two is undefined behaviour.