How to free memory for dyna int array 
Author Message
 How to free memory for dyna int array

        int **p;

        p = (int **)malloc(numOfRows * sizeof(int *));

        for (i=0; i < numOfCols; i++){
                p[i] = (int *)malloc(numofCols * sizeof(int));
                p[i] = 5;
        }

Now how do I free memory for the array 'p', I tried the following:

        for(i=0; i < numOfCols; i++)
                free ( p[i] );

        free ( p );

But that gives me bus error. SO HOW DO I FREE MEMORY FOR THIS ARRAY?

Thanks
Tahir



Tue, 22 Aug 2000 03:00:00 GMT  
 How to free memory for dyna int array

On Fri, 6 Mar 1998 00:06:33 GMT, Quraishi Tahir

Quote:

>    int **p;

>    p = (int **)malloc(numOfRows * sizeof(int *));

>    for (i=0; i < numOfCols; i++){
>            p[i] = (int *)malloc(numofCols * sizeof(int));
>            p[i] = 5;
>    }

Hi!

A good compiler would have issued a warning on the statement

                p[i] = 5;

This has at least two problems.  First, you are assigning p[i] to
point at the value of the integer 5 cast to an integer pointer.
Second, you clobbered the value that was just returned from malloc in
the previous line

                p[i] = (int *)malloc(numofCols * sizeof(int));

Quote:
>Now how do I free memory for the array 'p', I tried the following:
>    for(i=0; i < numOfCols; i++)
>            free ( p[i] );

>    free ( p );

This is the right way to go about it, modulo the problems noted earlier
above.  That is the

                free ( p[i] );

calls invoke undefined behavior if numOfCols > 0 and if malloc never
returned the value 5.  It might still further invoke undefined
behavior if numOfCols > 1, since you are freeing the same pointer
value more than once.

Quote:
>But that gives me bus error. SO HOW DO I FREE MEMORY FOR THIS ARRAY?

I hope my answer has provided some clues as to why this might be
happening.

"Life is short.  Read the C-faq."

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Tue, 22 Aug 2000 03:00:00 GMT  
 How to free memory for dyna int array

:       int **p;

:       p = (int **)malloc(numOfRows * sizeof(int *));

:       for (i=0; i < numOfCols; i++){
:               p[i] = (int *)malloc(numofCols * sizeof(int));
:               p[i] = 5;

I cannot understand this statement p[i] is a pointer to an int an you
cannot do such assignments explicitly like p[i]=5 esp in unix systems
Deepu

:       }

: Now how do I free memory for the array 'p', I tried the following:

:       for(i=0; i < numOfCols; i++)
:               free ( p[i] );

:       free ( p );

: But that gives me bus error. SO HOW DO I FREE MEMORY FOR THIS ARRAY?

: Thanks
: Tahir
--
------------------------------------------------------------------------
Res:
628, South 12 street, Apt# 5
Lincoln, NE-68508



Wed, 23 Aug 2000 03:00:00 GMT  
 How to free memory for dyna int array

Quote:

>         int **p;

>         p = (int **)malloc(numOfRows * sizeof(int *));

>         for (i=0; i < numOfCols; i++){
>                 p[i] = (int *)malloc(numofCols * sizeof(int));
>                 p[i] = 5;
>         }

> Now how do I free memory for the array 'p', I tried the following:

>         for(i=0; i < numOfCols; i++)
>                 free ( p[i] );

>         free ( p );

> But that gives me bus error.

Of course it does.

Right after you allocated the memory you did p[i] = 5;

This means you are now doing free(5)...

--
<\___/>
/ O O \
\_____/ FTB.



Wed, 23 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. problem allocating/freeing memory of multidimensional arrays

2. freeing memory from arrays

3. freeing memory in an array.

4. how to free the memory of arrays?

5. Freeing Array Memory

6. How to free memory space for a member dynamic array

7. int pointer to int array

8. int to int array problem

9. can I know the amount of memory freed by call to free

10. how free realy frees memory ?

11. Problems freeing memory using "free"

12. Free memory, memory usage

 

 
Powered by phpBB® Forum Software