- Google+ Tools
-
Make Google+ profile picture
Make Google plus banners for profile
Create and share your Google Plus profile banners.
- Language
- C++
- Tags
- Resize dynamically allocated arrays.
Resize dynamically allocated arrays.
#include <iostream>
#include <string>
// Use resize_array for int, double, float, unsigned ...
template <typename T>
void resize_array(T *&array, size_t old_size, size_t new_size)
{
// new_size has no be bigger than old_size.
if (new_size <= old_size)
return;
// Declare a new pointer to T, and allocate memory for new_size
// elements.
T *temp = new (std::nothrow) T[new_size];
// Check for NULL ptr, and throw a const char * exception if the
// memory couldn't be allocated.
if (temp == NULL)
throw ((const char*)"Memory allocation failed!\n");
// Copy the old data to temp.
for (size_t i = 0; i < old_size; i++)
temp[i] = array[i];
// Free the original array.
delete [] array;
// Make array point to temp.
array = temp;
}
// Use this for pointer to chars.
void resize_char_array(char *&str, size_t new_size)
{
// new_size has to be bigger than the size of str.
if (new_size <= strlen(str))
return;
// Declare a pointer to char, and allocate memory for new_size
// elements.
char *temp = new (std::nothrow) char[new_size];
// Check for NULL ptr, and throw a const char * exception if the
// memory couldn't be allocated.
if (temp == NULL)
throw ((const char*)"Memory allocation failed!\n");
size_t i;
// Copy the old data to temp.
for (i = 0; i < strlen(str); i++)
temp[i] = str[i];
// This is why we had to make a new function for char *, because
// temp has to be NULL terminated.
temp[i] = '\0';
// Delete old string and make str point to temp.
delete [] str;
str = temp;
}
void test()
{
// -----------------------------------------------
// RESIZE AN ARRAY OF DOUBLES:
// -----------------------------------------------
double *arr = new double[5];
size_t sz = 5;
// Just assign some data.
for (size_t i = 0; i < sz; i++)
arr[i] = (double)(i + 1);
// Now, after the function call, "arr" can hold
// 20 doubles (15 more elements).
resize_array<double>(arr, sz, 20);
// Starting from 5, assign 15 more doubles:
for (size_t i = sz; i < 20; i++)
arr[i] = (double)(i + 1);
sz = 20; // Now, the size is 20.
// Print:
for (size_t i = 0; i < sz; i++)
std::cout << arr[i] << " ";
delete [] arr;
std::cout << "\n\n";
// -----------------------------------------------
// -----------------------------------------------
// RESIZE A CHAR ARRAY:
// -----------------------------------------------
char *ptr = new char[10];
strcpy_s(ptr, strlen("Hello") + 1, "Hello");
// Now, ptr can hold 30 characters (20 more).
resize_char_array(ptr, 30);
// Safely assign (remember, the original could hold 10 elements,
// after the resize, it can hold 30 elements).
strcat_s(ptr, strlen(" DreamInCode") + strlen(ptr) + 1, " DreamInCode");
std::cout << ptr << "\nlength: " << strlen(ptr) << "\n\n";
delete [] ptr;
// -----------------------------------------------
}
int main()
{
test();
return 0;
}
Comments
blog comments powered by Disqus