Dynamic allocation! 
Author Message
 Dynamic allocation!

Hello,

I have a question regarding dynamic allocation...I am creating a two
dimensional "double" array that increases in size each time around a
loop, ie nxn (5x5 then 6x6 etc...) but I am using the same pointer to
reallocate the memory...I malloc the memory at the beginning of the loop
and de-allocate it at the end...using free...but when I start the loop
again and try to malloc again with the same pointer, I get a
segmentation fault....here is a portion of my code....

do{
  ...
  A = (double**)malloc(n * sizeof(double*));
  for (row = 0; row < n; row++){
    A[row] = (double*)malloc(n * sizeof(double));
  }
  .....
  for (row = 0; row < n; row++){
    free(A[row]);
  }
  free(A);
  n++;

Quote:
}while(...);

Thanks,
Zeenat


Thu, 08 Aug 2002 03:00:00 GMT  
 Dynamic allocation!

Quote:

> Hello,

> I have a question regarding dynamic allocation...I am creating a two
> dimensional "double" array that increases in size each time around a
> loop, ie nxn (5x5 then 6x6 etc...) but I am using the same pointer to
> reallocate the memory...I malloc the memory at the beginning of the loop
> and de-allocate it at the end...using free...but when I start the loop
> again and try to malloc again with the same pointer, I get a
> segmentation fault....here is a portion of my code....

> do{
>   ...
>   A = (double**)malloc(n * sizeof(double*));
>   for (row = 0; row < n; row++){
>     A[row] = (double*)malloc(n * sizeof(double));
>   }
>   .....
>   for (row = 0; row < n; row++){
>     free(A[row]);
>   }
>   free(A);
>   n++;
> }while(...);

It is not possible to determine the cause of the seg fault from this snippet.
You did not
provide the declaration of A. Improper declaration could cause the problem. You
did
not check the return values of the function malloc so the cause could be that
one of
the allocations failed and your code did not take this situation into
consideration. Always,
check the return values of the dynamic allocations. There is always a
possiblity that the
allocation did not succeed.

Ex:

#include <stdio.h>
#include <stdlib.h>

int alloc_2d(double ***d, unsigned n);
void free_2d(double ***d, unsigned n);

int main(void) {
   double **darray = NULL;
   int j,k;
   unsigned num = 5;

   do {  /* Test allocations in a loop */
      if(alloc_2d(&darray,num)) {
         for(j = 0; j < num;j++)
            for(k = 0; k < num; k++)
              darray[j][k] = k * (double)j * num;
         printf("In the %u by %u array: darray[4][4] = %.1f\n",
                                               num, num,darray[4][4]);
         free_2d(&darray,num);
         }
      num++;
      }while(num < 10);
   return 0;
   }

int alloc_2d(double ***d, unsigned n) {
   /* Function allocates space for 2d double array (*d)[n][n] */
   unsigned i;

   if(n == 0) return 0;
   if((*d = malloc(n * sizeof(**d))) == NULL) return 0;
   for(i = 0; i < n;i++)
      if(((*d)[i] = malloc(n * sizeof(***d))) == NULL) {
         for( ; i > 0;i--) free((*d)[i-1]);
         free(*d);
         *d = NULL;
         return 0;
        }
   return 1;
   }

void free_2d(double ***d, unsigned n) {
   unsigned i;

   for(i = 0; i < n; i++) free((*d)[i]);
   free(*d);
   *d = NULL;
   return;
   }

--
Al Bowers
Tampa, FL  USA

http://www.gate.net/~abowers/



Thu, 08 Aug 2002 03:00:00 GMT  
 Dynamic allocation!

Quote:
> Hello,

> I have a question regarding dynamic allocation...I am creating a two
> dimensional "double" array that increases in size each time around a
> loop, ie nxn (5x5 then 6x6 etc...) but I am using the same pointer to
> reallocate the memory...I malloc the memory at the beginning of the
loop
> and de-allocate it at the end...using free...but when I start the loop
> again and try to malloc again with the same pointer, I get a
> segmentation fault....here is a portion of my code....

...
Your posted code is correct, except that casting malloc is not
necessary in correct C (you did #include <stdlib.h> right?) and
you don't check for running out of memory (always a good idea,
but on any reasonable system double[6*6] won't be a problem,
unless the fault is happening when you get up to something
more like double[100*100]).  Barring this, segfault in malloc etc.
usually means you have corrupted the heap, somewhere in
the code you left out of your posting.  Desk-check everything
or use a debugging heap manager like Electric Fence or Purify.

--
- David.Thompson 1 now at worldnet.att.net



Fri, 16 Aug 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. dynamic allocation

2. C Dynamic allocation of Pointers

3. char **info; /*dynamic allocation*/

4. Dynamic Allocation of Multi-Dimensional Array (Simple Question)

5. Dynamic Allocation

6. Dynamic allocation of 2-dimensional array

7. Dynamic Allocation of 2D Arrays

8. Dynamic allocation and reallocation of two dimensional arrays

9. Character array dynamic allocation

10. Dynamic Allocation in ISR

11. dynamic allocation problem

12. Dynamic allocation of an array of structures.

 

 
Powered by phpBB® Forum Software