Quote:
Originally Posted by matiasar
Thanks for your answer.
I tried the following code,
|
That doesn't look like you did anything to fix the second problem rizwanrafique described.
Quote:
I get "segment violation" when using string type into the struct. The same program using const char * works ok.
|
The program is not correct even using const char*.
I understand why those serious bugs typically won't result in an immediate crash using const char*, but will using string. However, you are closer than you think to a crash even with the const char * version.
1) What you seem to be doing is inventing your own std::vector. I have done that in many places, because I know how and because I know which situations benefit enough from coding better than std::vector to be worth the trouble. You would be better off just using a std::vector<Datos> instead of reinventing that yourself.
2) If you must write it yourself, you need to be more systematic about the process of switching to the new copy. I suggest:
Code:
if ( n >= max)
{
int old_max = max;
Datos* old_Datos = p_Datos;
max = max + 15;
p_Datos = new Datos [max];
for (int i=0; i < old_max; i++)
{
p_Datos[i].fecha = old_Datos[i].fecha;
p_Datos[i].evento = old_Datos[i].evento;
}
delete [] old_Datos;
}
Unlike your version, this one deletes the correct buffer (the old one, not the new one) and this one makes the limit of reading the old buffer explicit rather than relying on the purely single increment growth in n.
But if you want a minimal change, you could just delete the right buffer. Instead of:
Code:
p_Datos = p_temp;
delete [] p_temp;
use
Code:
delete [] p_Datos;
p_Datos = p_temp;