can {m,c}alloc'ed arrays be freed without knowing size? 
Author Message
 can {m,c}alloc'ed arrays be freed without knowing size?

Hi,

I am writing a routine in which the correct amount of memory is allocated
at every call for a two or three dimensional array.

The problem is, the pointers that come in may be previously allocated
arrays, whose sizes are NOT available to my routine.  Can I still free
the two/three dimensional arrays?  here's a fragment on my allocation:

myroutine(GLfloat ***ctlPoints, ...etc...)
{
        /* first allocate array of pointers */
        *ctlPoints = calloc(curve->pol->n+1, sizeof(GLfloat *));
        if (*ctlPoints == NULL) {
                return(MEM_ERR);
        }

        /* now for each pointer, allocate a 3 or 4 element array */
        if (U_iscurr(curve)) {
                *type = GL_MAP1_VERTEX_4;
                for (i=0; i<=curve->pol->n; i++) {
                        (*ctlPoints)[i] = calloc(4, sizeof(GLfloat));
                        if ((*ctlPoints)[i] == NULL) {
                                return(MEM_ERR);
                        }
                }
        } else {
                *type = GL_MAP1_VERTEX_3;
                for (i=0; i<=curve->pol->n; i++) {
                        (*ctlPoints)[i] = calloc(3, sizeof(GLfloat));
                        if ((*ctlPoints)[i] == NULL) {
                                return(MEM_ERR);
                        }
                }
        }

Quote:
}

As you can see, when I allocate the 2D array in this case, I know the
size (curve->pol->n), but the next time around I won't know it, and in
fact, it doesn't even have to be the same as in the last call...it can
be anything.

If you do post to the newsgroups, please do send me a copy via email so
i won't miss the responses.  Thanks!

--
--

WEB URL: http://www.*-*-*.com/ ~gohel/     | things right in your life as
Geometric Modeling & Graphics Research Group | long as you don't do too many
U of South Florida, Tampa, FL. USA.          | things wrong - Warren Buffett



Sat, 19 Jun 1999 03:00:00 GMT  
 can {m,c}alloc'ed arrays be freed without knowing size?

Quote:

> Hi,

> I am writing a routine in which the correct amount of memory is allocated
> at every call for a two or three dimensional array.

> The problem is, the pointers that come in may be previously allocated
> arrays, whose sizes are NOT available to my routine.  Can I still free
> the two/three dimensional arrays?
> As you can see, when I allocate the 2D array in this case, I know the
> size (curve->pol->n), but the next time around I won't know it, and in
> fact, it doesn't even have to be the same as in the last call...it can
> be anything.

--
Sure you can free your allocated memory only by knowing curve->pol->n
Free it the way back you have allocated it.

if (*ctlPoints != NULL) {
    for (i=0; i<=curve->pol->n; i++)
        if ((*ctlPoints)[i] != NULL) {
            free ((*ctlPoints)[i]);
        }
    free (*ctlPoints):

Quote:
}

-------------------------------------------------------------------

Institut fuer         | Phone: +43 (0316) 380 5242
Theoretische Physik   |
Universitaetsplatz 5  | WWW  : http://physik.kfunigraz.ac.at/~rbr/
8010 Graz             |
Austria (Europe)      | The Amiable Dragon-==(UDIC)==-
-------------------------------------------------------------------


Tue, 22 Jun 1999 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. non-local pointers to c(m)alloc'ed arrays

2. How access alloc'ed string as array?

3. I am seachcing a free pascal editor if someone known a web site just called me

4. HELP: Dynamically Alloc'd array

5. Determining Malloc'ed size via pointer

6. how to access a table without knowing it's name

7. array declaration without size-specification

8. Passing sizes of arrays without a separate argument for the length

9. alloc and free in shared CRT dll (via /MDd compile option)

10. Microsoft C 6.00 alloc/free heap coelesce

11. How do I know if free() really free()ed?

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

 

 
Powered by phpBB® Forum Software