Board index » cppbuilder » Error using delete[]

Error using delete[]


2006-01-26 01:58:47 AM
cppbuilder49
Hi everyone!
I did something like this:
String str = new String[10];
...
delete[] str;
It works but sometimes it causes EAccesViolation when deleteting. Why it
happens?
thanks
---
Rodrigo
 
 

Re:Error using delete[]

Rodrigo wrote:
Quote
String str = new String[10];
[snip]
Quote
It works but sometimes it causes EAccesViolation when deleteting. Why it
happens?
I don't know why it did that, but to use a array of Strings, use it like
this:
String *str = new String[10];
...
delete[] str;
You can then access it like this: str[0] = "hello";
HTH
Jonathan
 

Re:Error using delete[]

"Rodrigo" < XXXX@XXXXX.COM >writes:
Quote
Hi everyone!

I did something like this:

String str = new String[10];
...
delete[] str;

It works but sometimes it causes EAccesViolation when deleteting. Why it
happens?
The answer lies in the code you chose to omit. (Marked as "..." in
your example.) Therefore we cannot help you, but at least now you
know where to search for the problem. Something is corrupting your
pointer.
--
Chris (TeamB);
 

{smallsort}

Re:Error using delete[]

"Rodrigo" < XXXX@XXXXX.COM >wrote in message
Quote
String str = new String[10];
...
delete[] str;
You need to use a pointer:
String *str = new String[10]; // <-- notice the '*'
...
delete[] str;
With that said, since you are using the VCL anyway, you can alternatively
use a TStirngList instead:
TStringList *str = new TStringList;
...
delete str;
Gambit