Hi everyone.. Suppose i have a function :

void foo(int ind,float **ptr1,float **ptr2) that allocates memory using malloc .. - double pointers are much more in 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 gloabal variables..

To be more understanding here s an example of a older post :

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

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

    <snipped>
}

First of all thanks a lot.. in main() i have others functions that wait the same double pointer as an argument - have you got any idea what to do ?

i passed the address of the pointer as you said..
Ancient Dragon

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

[B]void mem_alloc_main(int ind,float **u_trn)[/B]
 {
                        .
                        .
 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);
    } 

} 

[B]void normal_val(float **u_trn) [/B]
{
    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;
    }
}

An access violation problem appears to me .. focusing in the u_trn[k][j] that arrow points..

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

now is giving me an an "Access Violation " message in

for (i=0;i<=(kf_trn+max_del);i++)
{
--- > [B] u_trn[i]=(float *)calloc((con_inp+2),sizeof(float));[/B] assert(u_trn[i]!=NULL);
}

even if i try *u_trn=......

This is one way to do it

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

}

thanx a lot !

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

Why the restriction about using return? I would tend to prefer something with this sort of an interface:

T** array2d_create(int rows, int cols);
void array2d_init(T **array, int rows, int cols);
void array2d_print(T **array, int rows, int cols);
void array2d_destroy(T **array, int rows);

After you valuable help :

void mem_alloc_main(int ind,float  ***u_trn)
 {
  *u_trn=(float**)calloc(kf_trn+max_del+1),size0f(float*))

     for(int i=0;i<=(kf_trn+max+del;i++)
    {
 *--->     (*u_trn)[i]=(float *)calloc((con_inp+2),sizeof(float));
    }
 }

*The change needed to the Ancient Dragon code

main() 
{

  float  **u_trn;
  void mem_alloc_main(0,&u_trn)
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.