Memory leak

S

shilpi.harpavat

Hi,
I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??


if(numLevels >= numAlloc) {
char **oldLevels = levels;
long *oldOrder = order;
numAlloc *= 10;
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));
for(i = 0; i < numLevels; i++) {
levels = oldLevels;
order = oldOrder;
}
free(oldLevels);
free(oldOrder);
}
Thanks in advance
Shilpi
 
V

Victor Bazarov

I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??


if(numLevels >= numAlloc) {
char **oldLevels = levels;
long *oldOrder = order;
numAlloc *= 10;

Are you sure that 'numLevels' is not 10 times 'numAlloc'? If it isn't,
then you're not allocating enough, probably.
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));
for(i = 0; i < numLevels; i++) {
levels = oldLevels;
order = oldOrder;


If (numLevels > numAlloc) here yields 'true', you're overrunning the
buffers you just allocated. You never check that, AFAICS.
}
free(oldLevels);
free(oldOrder);
}

I don't know about memory leaks. When do you free those 'levels' and
'order'?

V
 
J

Josh Mcfarlane

Hi,
I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??


if(numLevels >= numAlloc) {
char **oldLevels = levels;
long *oldOrder = order;
numAlloc *= 10;
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));
for(i = 0; i < numLevels; i++) {
levels = oldLevels;
order = oldOrder;
}
free(oldLevels);
free(oldOrder);
}
Thanks in advance
Shilpi


If numLevels >= (numAlloc*10), this will fail as you will exceed levels
and order.
 
E

E. Mark Ping

Hi,
I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??

Since this is in a C++ newsgroup I'll ask: why are you manually
allocating like this? If you must have an array of pointers, why not
use std::vector?
 
O

Old Wolf

I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??

if(numLevels >= numAlloc) {
char **oldLevels = levels;
long *oldOrder = order;
numAlloc *= 10;
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));
for(i = 0; i < numLevels; i++) {
levels = oldLevels;
order = oldOrder;
}
free(oldLevels);
free(oldOrder);
}


The logic is OK (assuming numLevels <= numAlloc).
But you do not check for failure of calloc.
Note that calloc with (char *) does not necessarily
make the pointers null.

Also, you should use realloc(): it will automatically copy the
old items, and may be more efficient in that it only has to
extend a memory block, rather than selecting a new one:

levels = (char **)realloc(levels, numAlloc * sizeof(char *));
if (levels == NULL)
{
levels = oldLevels;
// process error and return
}

etc.

Finally, assuming you posted to a C++ group instead of a
C group on purpose, you should do away with all of these functions
and use a standard container, which automatically grows its memory.
 
S

Serge Paccalin

Le mercredi 10 août 2005 à 20:01, (e-mail address removed) a écrit dans
comp.lang.c++ :
Hi,
I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??

if(numLevels >= numAlloc) {
char **oldLevels = levels;
long *oldOrder = order;
numAlloc *= 10;
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));
for(i = 0; i < numLevels; i++) {
levels = oldLevels;
order = oldOrder;
}
free(oldLevels);
free(oldOrder);
}
Thanks in advance
Shilpi


std::vector<char *> levels(numAlloc);
std::vector<long> order(numAlloc);

....

if (numLevels >= numAlloc)
{
numAlloc *= 10;
levels.resize(numAlloc);
order.resize(numAlloc);
}

Isn't C++ beautiful?

--
___________ 11/08/2005 09:22:26
_/ _ \_`_`_`_) Serge PACCALIN -- sp ad mailclub.net
\ \_L_) Il faut donc que les hommes commencent
-'(__) par n'être pas fanatiques pour mériter
_/___(_) la tolérance. -- Voltaire, 1763
 
R

Ram

I am trying to allocate more memory if the following condition is true.
IS it free of memory leaks??


if(numLevels >= numAlloc) {
char **oldLevels = levels;
long *oldOrder = order;
numAlloc *= 10;
Seems like you are allocating more buffer as needed. But if your new
requirement is greater than 10 times of old, it will not work as you
expect.
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));
for(i = 0; i < numLevels; i++) {
levels = oldLevels;
order = oldOrder;
}

You are copying the old data to new buffer, but the number of items in
your old data is not numLevels, you may cause buffer over-run. Since
you already increased numAlloc ten times, it should be-

for(i = 0; i < numAlloc/10; i++) {
levels = oldLevels;
order = oldOrder;
}
free(oldLevels);
free(oldOrder);
}
There is no memory leak if you take care to free levels and order.
However as already posted, if you are using C++ best way would be to
use a std::vector<char*> & std::vector<long> and use their resize()
functions.

-Ramashish
 
A

ambar.shome

levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));

you did not write anything about "levels" and "order". If they have
been assigned any memory before

"if(numLevels >= numAlloc) { "

and you are re-assigning them in

;
levels = (char **) calloc(numAlloc, sizeof(char *));
order = (long *) calloc(numAlloc, sizeof(long));

So make sure that they are not assigned any memory.

Otherwise , they would cause memory leak.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top