int size = 2;
string *s = new string[size];
s[0] = "First";
s[1] = "Second";
//reallocating memory and increasing its size
size++;
s = (string*)realloc(s, size * sizeof(string));//here is an error
s[2] = "Third";
// Error: realloc():invalid pointer\
// Aborted core is dumped
For example the same approach with dynamic int array which works fine
int size = 5;
int *a = new int[size];
for(int i = 0; i < size; i++) a[i] = i*(3);
++size;
a = (int*)realloc(a, (size)*sizeof(int));
a[size-1] = 132321;
Question: How realloc is different for string and int???
Note!
- I was thinking which tag is more suitable for my post c or c++ tag. Since I use string I decided to use c++ tag.
- I know that I can use vector, but my assignment is about using dynamic string array without using a vector.