473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Allocate more memory

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
2 1941
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Curt | last post by:
If this is the complete program (ie, the address of the const is never taken, only its value used) is it likely the compiler will allocate ram for constantA or constantB? Or simply substitute the...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
5
by: lixiaoyao | last post by:
hi all I use matrix & vector function to allocate the space myself in c, typedef struct matrix_array newdata; struct matrix_array{ float **sy,*sxx; }; newdata ndata;//new data struct...
12
by: gc | last post by:
I am writing a function that given nx1 vector a and a nx1 b solves a system of equations f(a,c)=b for a nx1 c. While writing the function: 1] Should I allocate the memory for c within the...
8
by: M. Moennigmann | last post by:
Dear all: I would like to write a function that opens a file, reads and stores data into an 2d array, and passes that array back to the caller (=main). The size of the array is not known before...
11
by: Divick | last post by:
Hi, can somebody help me figure out how can I make write a function which inturn uses malloc routine to allocate memory which is 2^k aligned? The condition is such that there should not be any...
14
by: raghu | last post by:
Hello , This is Raghu. I have a problem in declaring a structure. Consider struct hai{ int id; char sex; int age; }; here when a variable is instianted for this structure then immediately
2
by: lovecreatesbea... | last post by:
If the built-in operator keyword new doesn't allocate memory on heap and it calls global operator new (::operator new) or class member operator new to do that. What are the two kinds of operator...
11
by: Bryan Parkoff | last post by:
I want to know how much static memory is limited before execution program starts. I would write a large array. The large array has 65,536 elements. The data size is double word. The static...
1
by: ulrik | last post by:
Hi, I've read somewhere that there is an upper limit of memory allocation within a single AppDomain of 800 MB. Is this true? If yes, is there anything I can do to get around this? My...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.