Deleting dynamic arrays!! 
Author Message
 Deleting dynamic arrays!!

Quote:

>How can I delete an array created with
>float *mp ;
>mp = (float *) malloc (50*sizeof(float)) ;

>Pietro

free (mp) ;

You dont need the cast malloc() returns void *
You *do* need to
#include <stdlib.h>
which contains prototypes for the malloc/free family.

--
Happy hacking,

Adriaan van Kessel.
Ingres DBA, C/Unix hacker

(remove NotThere. from the address in the header)
*** Nederlandstalige zachtwaar is een pijn in de aars ***



Sat, 30 Dec 2000 03:00:00 GMT  
 Deleting dynamic arrays!!

|How can I delete an array created with
|float *mp ;
|mp = (float *) malloc (50*sizeof(float)) ;

    free(mp);

BTW: remember to #include <stdlib.h>.  The unnecessary and generally
frowned upon cast (in C) suggests that you did not do this.  e.g.:

#include <stdlib.h>
int main(void)
{
    float *mp;
    if (!(mp = malloc(50 * sizeof *mp)))
    {
        /* handle failed attempt to allocate here */
    }
    free(mp);
    return 0;

Quote:
}


/* Newsgroup posts also e-mailed */
=========


Sat, 30 Dec 2000 03:00:00 GMT  
 Deleting dynamic arrays!!
free(mp);
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com
Quote:

>How can I delete an array created with
>float *mp ;
>mp = (float *) malloc (50*sizeof(float)) ;

>Pietro



Sat, 30 Dec 2000 03:00:00 GMT  
 Deleting dynamic arrays!!

...

Quote:
>OK, what am I missing here?

>I have a copy of K&R2 here, and it says: " In C, the proper method is to
>declare that malloc returns a pointer to void, then explicitly coerce the
>pointer into the desired type with a cast." (p. 142) They then provide two
>examples of that.

>Do I have a 'fatal: reader context error' or what?

You're missing the context in which K&R2 was written. It was first published
in 1988 prior to the release of the final ANSI standard and has only
had minor corrections since then (although generally speaking that is all it
has needed). At that time there were essentially no ANSI C compilers to work
with. K&R themselves acknowledge in the preface that the code in the book was
tested mainly using a C++ compiler. Prior to ANSI C malloc() usually
returned char * which did need casting to other pointer types. C++ requires
the cast. So for practical purposes at the time K&R2 was written the cast
was necessary. However now that ANSI C is well established this sort of
cast is generally undesirable in C code.

Steve has promised to remove them from the FAQ one day. :-)

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


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



Sun, 31 Dec 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. how to write a function to delete dynamic array of object

2. Newbie Question: using delete with dynamic 2-d arrays

3. Deleting Dynamic Multidimensional Array

4. Dynamic arrays not really dynamic in managec C++?

5. How to inspect dynamic array ( my_struct* array) ?

6. Control Arrays/Dynamic Arrays

7. Lists VS. Dynamic Arrays VS. Re-defining Arrays

8. delete DocTemplate / dynamic resources

9. Dynamic database creation with CASCADE/DELETE option

10. dynamic memory overrun when using new and delete

11. HELP -> dynamic array of char** in dynamic array of mystruct**

12. C# Dynamic Array?

 

 
Powered by phpBB® Forum Software