Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

memory allocated within function and passed out of it

Status
Not open for further replies.

lehe

Programmer
Jul 18, 2008
5
US
Hi,
I am asking for using "malloc" to allocate memory within a self-written function and then pass on to the outside of the function. I did it this way but guess that it might be the reason that caused run-time memory error. Could you please point out if it is wrong and give the correct way? Thanks in advance!

void main(){
int* ptr;
myfun(&ptr);
free(ptr);
}


void myfun(int**p){
int num;
// read the value of "num" from some file...
*p=(int*)malloc(num*sizeof(int));
}
 
Thanks for pointing out what I was not unware of.

Besides that, let's move back to my original question. Will it cause runtime memory error using "malloc" to allocate memory within a function and then pass on to its outside?
 
Don't worry. You may use a pointer to allocated memory here, there and everywhere.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top