Memory allocation using malloc 
Author Message
 Memory allocation using malloc

: I have a C memory allocation question.

: I know if I define a variable as such:

: char in[21][6];

: it will hold 20 strings, each 5 characters long.

: but if I don't know the length of the string, I can use:

: char *in[21];

: which will create 20 pointers.  These pointers can then be
: malloc'd to the proper size as such:

: for(j=0;j<=20;j++) in[j] = (char *) malloc(whatever);

: ---------------

: But how do I *properly* allocate memory if the size and the amount of
: strings
: is unknown?  I take it that I define the variable as:

: char **in;

: but then what?

: -Rich

**************begin r.s. response*****************

        a ( simplistic ? ) way is ...

        a) allocate buffer array
           large enough
           for max length string ...
        b) load strings into buffer,
           and size strings -
           on the fly as program runs
        c) with string size now known
           allocate memory suitably for
           each string ...
        d) n.b.
                'c' programming language
           allows 'ragged sized matrix',
           ( when dynamic allocation is used ),
           and such are much used in text
           processing !!!
           also ...
           allocation of each string
           may be,  but do not need to be,
           done in a compact group,
           as the loop
           (  in posting cited )

**************end r.s. response*******************
Ralph Silverman

Sat, 28 Aug 1999 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. Sun f77 dynamic memory allocation, malloc, automatic arrays

2. Memory allocation (was: Re: Herman wants dynamic allocation)

3. Memory allocation using int 21h

4. Pseudo-dynamic memory allocation using pool arrays

5. Using dynamic memory allocation and Fortran 77

6. Allocation (malloc)

7. freeing only part of malloc-ed memory?

8. list conversion to malloc'd memory

9. Creating malloc using asm

10. using malloc & realloc

11. Using malloc and friends

12. Qs about using MALLOC in SUN F77


Powered by phpBB® Forum Software