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));
}
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));
}