Link to home
Start Free TrialLog in
Avatar of stefarg
stefarg

asked on

Calling malloc twice or redefining array sizes?

Hey all,
I'm just curios about the consequences of calling malloc twice or assigning an array to a new size.
lets say we have

char* a = new char[10];
....
a = new char[20];

or

char* a = (char*) malloc(10);
.....
a = (char*) malloc(20);

If we do the above is it necessary to call delete or free etc before the second assignment to release the memory of (char) length 10 or is this released by default?  I'm a bit confused about this whole memory management thing.
Thanks,
Stef
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stefarg
stefarg

ASKER

Ok, so how do I clear this up, can I do the following:

char* a = new char[10];
....
delete [] a;

a = new char[20];

Is that correct and free of memory leaks?
Thanks for your response,
Stef
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stefarg

ASKER

That's great, thanks