Solved

Calling malloc twice or redefining array sizes?

Posted on 2004-04-16
4
771 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:stefarg
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 85 total points
ID: 10846353
>>If we do the above is it necessary to call delete or free etc before the second assignment

Definitely. Otherwise, you'd create a memory leak.

>> is this released by default?

No. You are responsible for freeing memory when you no longer need it.
0
 
LVL 1

Author Comment

by:stefarg
ID: 10846383
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
0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 40 total points
ID: 10846424
Yes, this is the right way to do it. It's important that you use the "[]" form of delete if you allocated an array with new.
0
 
LVL 1

Author Comment

by:stefarg
ID: 10846531
That's great, thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now