By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,917 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,917 IT Pros & Developers. It's quick & easy.

Allocate more memory

P: n/a
Hi,
I am trying to allocate mroe memory if the following condition is true.
But I get Invalid pointer write memory error when running this code
through purifier.It is not freeing up oldlevels
Any help appreciated.

if(numLevels >= numAlloc) {
//printf("Allocate more memory\n");
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[i] = oldLevels[i];
order[i] = oldOrder[i];
}
free(oldLevels);
free(oldOrder);
}

Aug 10 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
shilpi wrote:
Hi,
I am trying to allocate mroe memory if the following condition is true.
But I get Invalid pointer write memory error when running this code
through purifier.It is not freeing up oldlevels
Any help appreciated.

if(numLevels >= numAlloc) {
//printf("Allocate more memory\n");
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[i] = oldLevels[i];
order[i] = oldOrder[i];
}
free(oldLevels);
free(oldOrder);
}

Please don't double post on the list. But the error you are having
might be due to the fact that if numLevels >= (numAlloc*10), you will
still not have enough allocated memory.

Aug 10 '05 #2

P: n/a
hi shilpi,

there is a function in the c library that does exactly the same thing
viz realloc
u can code in the following way...
if(numLevels >= numAlloc) {
//printf("Allocate more memory\n");
numAlloc *= 10;
levels = (char **)realloc(levels, numAlloc *
sizeof(char *));
order = (long *) realloc(order , numAlloc *
sizeof(long));
}

in case of any clarification,plz see the documentation of realloc.

thanks
rt

Aug 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.