Can you allocate memory for an array already on the heap?
Answer: Yes (but not how you are doing it...)
Whenever you have memory already allocated, in order to expand or reduce the allocation size making up a given block of memory, you must (1) allocate a new block of memory of the desired size, and (2) copy the existing block to the newly allocated block (up to the size of the newly allocated block), before (3) freeing the original block. In essence since there is no equivalent to realloc
in C++, you simply have to do it yourself.
In your example, beginning with an allocation size of 3-int
, you can enter your for
loop and create a temporary block to hold 1-int
(one more than the loop index) and copy the number of existing bytes in x
that will fit in your new tmp
block to tmp
. You can then delete[] x;
and assign the beginning address of the new temporary block of memory to x
(e.g. x = tmp;
)
A short example continuing from your post could be:
#include <iostream>
#include <cstring>
int main (void) {
int nelem = 3, /* var to track no. of elements allocated */
*x = new int[nelem]; /* initial allocation of 3 int - for fun */
for (int i = 0; i < 5; i++) {
nelem = i + 1; /* update nelem */
/* create temporary block to hold nelem int */
int *tmp = new int[nelem]; /* allocate tmp for new x */
memcpy (tmp, x, i * sizeof *tmp); /* copy i elements to tmp */
delete[] x; /* free x */
x = tmp; /* assign tmp to x */
x[i] = i; /* assign x[i] */
for (int j = 0; j < nelem; j++) /* output all */
std::cout << " " << x[j];
std::cout << '\n';
}
delete[] x; /* free x */
}
(note: on the first iteration zero bytes are copied from x
-- which is fine. You can include an if (i)
before the memcpy
if you like)
Example Use/Output
$ ./bin/allocrealloc
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
Memory Use/Error Check
In any code you write that dynamically allocates memory, you have 2 responsibilities regarding any block of memory allocated: (1) always preserve a pointer to the starting address for the block of memory so, (2) it can be freed when it is no longer needed.
It is imperative that you use a memory error checking program to ensure you do not attempt to access memory or write beyond/outside the bounds of your allocated block, attempt to read or base a conditional jump on an uninitialized value, and finally, to confirm that you free all the memory you have allocated.
For Linux valgrind
is the normal choice. There are similar memory checkers for every platform. They are all simple to use, just run your program through it.
$ valgrind ./bin/allocrealloc
==6202== Memcheck, a memory error detector
==6202== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6202== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==6202== Command: ./bin/allocrealloc
==6202==
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
==6202==
==6202== HEAP SUMMARY:
==6202== in use at exit: 0 bytes in 0 blocks
==6202== total heap usage: 7 allocs, 7 frees, 72,776 bytes allocated
==6202==
==6202== All heap blocks were freed -- no leaks are possible
==6202==
==6202== For counts of detected and suppressed errors, rerun with: -v
==6202== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
Always confirm that you have freed all memory you have allocated and that there are no memory errors.
Look things over and let me know if you have further questions.