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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That's great, thanks
ASKER
char* a = new char[10];
....
delete [] a;
a = new char[20];
Is that correct and free of memory leaks?
Thanks for your response,
Stef