Checking if char out of not fully filled array is empty - c++

As you may know, if you want to delete any type of dynamically allocated array (that you used the operator new[]on), you should use delete[] to get rid of it.
Now, I would like to know if there was any way I'd be able to delete a single object of a dynamically allocated array.
char* someNewArray = new char[someInt];
//Add values to the array
delete &someNewArray[2] //or maybe delete[] instead of just delete

No you cannot.
Operator new in its array form allocates one single memory element able to contain the number of elements you asked. You can the only deallocate the whole block with delete[].
If you want to deallocate blocks one by one, you will have to allocate them with same granularity, that is one by one.
EDIT :
It would have little sense for object of size 1 because the overload of allocating a block will be large facing sizeof(char). In that case it is more efficient to allocate a new block of size someInt -1, use memcpy and then free initial block with delete[]
char * tmp = new char[someInt -1];
memcpy(tmp, someNewArray, 2);
memcpy(tmp+2, someNewArray + 3, someInt - 3);
delete[] someNewArray;
someNewArray = tmp;
But it you add an array of larger objects say LObj, instead of
LObj * newArray = new LObj[someInt];
you could use :
LObj ** newArray = new (LObj *)[someInt];
for (i=0; i<someInt; i++) {
newArray[i] = new LObj;
}
Then is makes sense to use :
delete newArray[2];
newArray[2] = NULL;

Related

Shrinking the size of a dynamically allocated array in C++ [duplicate]

I have created an array pointer as a global variable like this:
T *bag;
bag = new T[size];
I have a method where I insert things into the array; however, if it detects that it will overflow the array, I need to resize the array (without vectors). I've been reading about this question all over stack overflow but the answers don't seem to apply to me because I need the data from the old array copied into the new array. Additionally, if I create a new array of a larger size inside the method and then copy the data over to the new array, once the method ends, the array will disappear, but I need it to be a global variable again so all my methods can see it...How should I proceed?
Thank you
Memory, allocated by new, would not disappear after your method ends.
You can return pointer to a new array by usung reference: void f(int *&ptr, size_t &size).
Also, be aware, that you need to clear memory manually arter you use it. For example:
int* newArray = new int[newSize];
... copying from old array ...
int* temp = oldArray;
oldArray = newArray;
delete[] temp;
To resize an array you have to allocate a new array and copy the old elements to the new array, then delete the old array.
T * p_bag;
p_bag = new T[old_size];
//...
T * p_expanded_bag = new T[new_size];
for (unsigned int i = 0; i < old_size; ++i)
{
p_expanded_bag[i] = p_bag[i];
}
delete[] p_bag;
p_bag = p_expanded_bag;
You could use std::copy instead of the for loop.
The thing you need can do the following things
Automatically handle the resizing when requested size is larger than current array size.
When resizing, they can copy the original content to the new space, then drop the old allocation immediately .
There is a non-global-variable way mechanism they can track the array pointer and the current size.
The thing is very similar to std::vector. If it is not allowed to use, you may need manage a dynamic allocated resource like std::vector on your own. You can reference the implementation in that answer link.
If eventually you need to wrap it in a class, make sure to follow the big 3 rules (5 rules in C++11)
You can use realloc from c if you have array of chars/ints/doubles... or some other fundamental data type or classes with only those variables (eg. array of strings won't work).
http://www.cplusplus.com/reference/cstdlib/realloc/
bag = (T*) realloc(bag, new_size * sizeof(T));
Realloc automatically allocate space for your new array (maybe into the same place in memory) and copy all data from given array.
"The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location."
Example:
#include <stdio.h> /* printf*/
#include <stdlib.h> /* realloc, free */
#include <iostream>
int main()
{
int old_size = 5;
int new_size = 10;
int *array = new int[old_size];
printf("Old array\n");
for (int i=0; i<old_size; i++) {
array[i] = i;
printf("%d ", array[i]);
}
printf("\nArray address: %d\n", array);
array = (int*) realloc(array, new_size * sizeof(int));
printf("New array\n");
for (int i=0; i<new_size; i++)
printf("%d ", array[i]);
printf("\nArray address: %d\n", array);
free(array);
return 0;
}

C++ delete[] vs. delete in array of arrays

I know that if I use new[] I have to delete[], but this is my doubt:
Declaration:
char **data; // This will be a bi-dimensional array
Construct:
data = new char*[10];
for (int i=0; i<10; ++i)
{
data[i] = new char[128];
}
Destroy:
for (int i=0; i<10; ++i)
{
delete data[i]; // <--- delete or delete[] ???
}
delete[] data;
Another case where I have an array of objects:
Declaration:
myClass **object;
Construct:
object = new myClass*[10];
for (int i=0; i<10; ++i)
{
object[i] = new myClass();
}
Destroy:
for (int i=0; i<10; ++i)
{
delete object[i]; // <--- this object[] is not an array, right?
}
delete[] object; // <--- Is this correct?
You basically answered your own question. Since data[i] is an array, you need to delete[]. object[i] was allocated with new so you would delete it. Since both object and data are arrays, you would need to delete[] them.
The first dimension of your pointer data is an array. The second dimension is an array as well. Therefore you must use delete[] to properly deallocate memory reserved from your new[].
For your object, the first dimension was allocated using new. So you must use delete. Then use delete[] on object because it's an array.
The mnemonic that you mentioned (delete [] for new [] and delete for new) is a a good practice, and should be enforced as mandatory.
However, there is a huge difference between delete and delete[] when it comes to base types (int, char, void*,etc) and classes.
The difference between new and malloc is that one calls the constructor while the other doesn't. Same relation happens with delete and free when it comes to destructor.
In your example you gave two situations : one when an array of simple type is allocated and one with an array of objects allocated.
The behavior is different.
For the simple type, delete and delete[] will produce the same result .
Reason ? well, you basically allocated a block of 10 pointers of char* , so deleting it is no problem (the allocated memory paged will be freed).
In other words, if you have
char* a = new char[100];
then
delete a;
and
delete[] a;
will produce the same results (note that you used a new []);
This behavior is different when it comes to objects that have constructors and destructors.
Let's take the following class :
class SimpleClass
{
public:
SimpleClass(){printf("constructed");
~SimpleClass()(printf("destructed");
}
and the following code :
SimpleClass* arr1 = new SimpleClass[5];
SimpleClass* arr2 = new SimpleClass[5];
char* arr3 = new char[5];
char* arr4 = new char[5];
delete[] arr4;
delete arr3;
delete[] arr2;
delete arr1; //notice the surprise ?
Run the above code (if you can) in Visual Studio and open up a memory viewer. You will see that the memory allocated at ptrs arr4 and arr3 is correctly invalidated, same for arr2.
However when attempting to delete arr1 without the [] call you will get a SigSev error .
Why ?
Because in case of arr2 you have an array of allocated objects, and the delete[] statement means "pass through each destructor from the array"".
In case of arr1 it means : call the destructor of the object allocated at pointer arr1... a bit unfortunate if you take into account that there is more then 1 object allocated at that address, and you end up attempting to release a page which is lesser then the initial allocated size.
Bottom line :
delete[] for new[] and delete for new !
Works every time !

How can we modify the size of array in C++?

I have an array of characters allocated with new and i want to modify the size of the array. Can i use realloc function for that? What is the best way to do so?
No, you can't... realloc() can only be used with malloc()/free()
Best call for a new[] allocated array is to create a new one and then memcpy() the data from one to another.
Better way - use an std::vector or std::string instead of array if you know you'll need resizing. Internally they're pretty much the same array.
In C++ it is best to use the STL std::vector class for this kind of thing. Either that, or a std::string.
I have an array of characters allocated with new and i want to modify the size of the array.
You can't resize an array, you can only allocate a new, larger one, move the contents to the new array, and delete the old one.
Can i use realloc function for that?
If you used malloc to allocate the original array, yes. But that's usually a bad idea in C++, where you usually want to deal with arrays of non-trivial objects not raw memory.
What is the best way to do so?
Use std::string (or perhaps std::vector<char>) to manage a dynamic array of characters automatically. These also have the advantage of using RAII to reduce the risk of memory leaks and other memory management errors.
You can use realloc(), if your array is allocated dynamically(via malloc/calloc/realloc). If you have static array, you can't resize it.If you have allocated with new:
int* Copy = new int[newSize];
std::copy(oldCopy,oldCopy+size,Copy);
But the best way would be to use std::vector<type> from c++ standard library
As was said by others, you cannot resize the array that was allocated per se, but you can create a larger one, copy the content of the first array to the second and delete the first one.
Here's an example, using std::copy().
int main(int argc, char** argv) {
int* arr = new int[5];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
arr[3] = 4;
arr[4] = 5;
int* tmp = new int[10];
std::copy(arr, arr + 5, tmp);
std::copy(arr, arr, tmp + 5);
delete[] arr;
arr = tmp;
for(int i = 0; i < 10; i++) {
std::cout << arr[i] << "\n";
}
delete[] arr;
std::cin.get();
return 0;
}
This first creates an integer array and fills it. It then creates a larger array and fills it with the content of the first array twice and display that new larger array.
The principle is the same for an array of characters.
As the others have mentionned, your best bet in C++ is to use the standard library. For a resizable array of characters, you should probably use std::string or a vector of strings, but it's a bit overkill in some situations.

How memory will be deleted?

Let's say I have structure
struct Foo {
int whatever;
int whatever2;
...
};
And I have this type of array
Foo **array = new Foo*[128];
Then, to every pointer I assign new structure
for (int i = 0; i < 128; i++)
array[i] = new Foo;
Then, after a while I deallocate:
delete[] array;
How memory will be deallocated?
Only the space for pointers will be freed
Space for pointer + the objects pointed by those pointers themselves?
Your code leaks memory. You need to call delete once for every new call (assuming the new doesn't fail and either return NULL or throw bad_alloc). So, in this case, you'll need to have a similar loop to the one that calls array[i] = new Foo; that does delete array[i];
I think this is one of points in Scott Meyers book, Effective C++ have read long back. But basic idea is, to avoid memory leak when you use new. make sure to call delete as many times as new is called and map new with delete and new[] with delete[] accordingly. so in your case new is called 128 times in for loop and new[] is called once, so similarly delete should be called same way to array[i] 128 times and delete[] should be called once for array. Hope this helps.

How does dynamic space allocation actually work in this algorithm?

I have made for school purposes my own take on a dynamically allocated array using templates.
While what I'm about to ask works, I don't know how and why and I've reached the point where I need to know.
template <typename TElement>
DynamicArray<TElement>::ensureCapacity () {
if (capacity >= elemNumb) {
return; //we have space to store the values
}
//we need to allocate more space for the values
TElement *auxArray = myArray;
//create space to hold more numbers
capacity = capacity * 2;
myArray = new TElement[capacity];
//copy the values
for (int i = 0; i < size; i++) {
myArray[i] = auxArray[i];
}
//release the memory
delete[] auxArray;
}
I need to know: TElement *auxArray = myArray; How does this work ? is it using pointers, are elements copied one by one ? I need to understand how it works so that I can figure out the complexity of my algorithm. I don't mind if some one tells me the complexity but the real answer I'm looking for is how does that work ?
Also myArray = new TElement[capacity]; I do this before deleting the old myArray does this delete the old one ? or is it still floating somewhere in memory in one form or another ?
This
TElement *auxArray = myArray;
just means that auxArray points to whatever myArray is pointing to. There is no copying of anything else, it is just a pointer copy.
This
myArray = new TElement[capacity];
means that myArray now points to a new, dynamically allocated TElement array. The expression doesn't delete anything. But auxArray is pointing to what myArray was pointing before this assignment, so when you delete auxArray, you release the resources originally pointed to by myArray.