Hi everyone.. Suppose i have a function :
void foo(int ind,float **ptr1,float **ptr2) that allocates memory using malloc .. - double pointers are much morein reality-
I want to use this pointers in other functions too, but when i call them(functions) in main() .. memory leak message appears ..
Is there a way to stop this without the usage of global variables
void foo( char * ptr)
{
ptr = malloc(255); // allocate some memory
strcpy( ptr, "Hello World");
}
int main()
{
char *ptr = 0;
foo( ptr );
printf("%s\n", ptr);
free(ptr);
return 0;
}
The answer
void foo( char * ptr)
"The problem is that ptr is a local object which has scope only within the function foo(). So the memory allocated with malloc() will be tossed into the bit bucket as soon as foo() returns to main(), and that will cause a memory leak. Inside function main() the value of ptr will still be 0 after foo() returns and the next line, printf(), will probably crash because the second argument (ptr) is a NULL pointer. "
So in the above example suppose that we have more than one double pointers in the function.. and we can t use return or gloabal variables
RE:
You need to use double pointer there so that foo() can allocate memory for the pointer in main(). You can do the same with as many other pointers as you like, it is not limited to just one pointer. This solves the problem is returning more than one string at the same time.
void foo( char ** ptr)
{
*ptr = malloc(255); // allocate some memory
strcpy( *ptr, "Hello World");
}
int main()
{
char *ptr = 0;
foo( &ptr ); // <<< pointer to a pointer
}
void main()
{
float *u_trn; // float **u_trn
mem_alloc_main(0,&u_trn); //(0,u_trn)
.
.
void normal_val(&u_trn); //(u_trn)
.
.
mem_alloc_main(1.&u_trn);
}
void mem_alloc_main(int ind,float **u_trn)
{
.
.
u_trn=(float **)calloc((kf_trn+max_del+1),sizeof(float*)); assert(u_trn!=NULL);
for (i=0;i<=(kf_trn+max_del);i++)
{
u_trn[i]=(float *)calloc((con_inp+2),sizeof(float)); assert(u_trn[i]!=NULL);
}
}
void normal_val(float **u_trn)
{
for (k=1;k<=(kf_trn+max_del);k++)
{
for (j=1;j<=con_inp;j++)
{
fscanf( fin1,"%f",&inp );
inp=((upper-lower)*inp+maxi_in_trn[j]*lower-mini_in_trn[j]*upper)/(maxi_in_trn[j]-mini_in_trn[j]);
--> u_trn[k][j]=inp;
}
}
RE:
If you want mem_alloc_main() to allocate a two-dimensional array of floats, then you have to declare the parameter with three stars, not two. The rule is that mem_alloc_main() needs to have a pointer to a two-dimensional pointer. What you have done is to pass a pointer to a one-dimensional pointer, but allocating it as if it were a two dimensional pointer.
Also: C programs do not require the void* return values to be typecast.
void mem_alloc_main( float ***ptr)
{
*ptr = calloc((kf_trn+max_del+1),sizeof(float*));
}
int main()
{
float **arry = 0;
mem_alloc_main( &arry );
}
another way
void mem_alloc_main(int ind,float ***u_trn)
{
float** ay;
ay = (float **)malloc(10 * sizeof(float*));
for(int i = 0; i < 10; i++)
{
ay[i] = (float*)malloc(10*sizeof(float));
for(int j = 0; j < 10; j++)
ay[i][j] = (float)rand();
}
*u_trn = ay;
}
int main()
{
srand((unsigned int)time(0));
float** arry = 0;
mem_alloc_main(0, &arry);
for(int i = 0; i < 10; i++)
{
for(int j = 0; j < 10; j++)
{
printf("%0.2f ", arry[i][j]);
}
printf("\n");
}
}
No comments:
Post a Comment