Dynamic mem allocation for pointers

Discussion in 'C Programming' started by srini, Dec 14, 2005.

  1. srini

    srini Guest

    Hi

    I need to create two-dimensional array; number of rows and columns
    unknown at compile time. The idea is to read a file, print it if
    requested by user and to delete when the program quits.

    Here is what I came up with using C:

    struct diskinfo {

    char **part_name; /* name of each partition */
    int **num_blocs;

    }

    I need only one instance of 'diskinfo' in the program. What is the best
    way to create & free memory and what are the C libraries that I should
    include for this ?

    Thanks for your time and help
    -Srini
    srini, Dec 14, 2005
    #1
    1. Advertising

  2. srini

    Guest

    Hi Srini,

    By what i can understand thius is your requirement.

    A program which opens file on demand, reads the content, prints it if
    asked for and at exit cleans up the memory used.

    I am slightly modifying the structure

    u can use this

    typedef struct _files
    {
    char * data;
    int fd;
    } *pfile;

    then sonewhere in main u can say

    int main(){
    pfile pf;
    // U can maintain a seperate variable for storing the number of files.

    pf = malloc(some initial number of files);


    while ( no more files to be procesed )
    {
    // get the file name;

    // run "stat" on the file, and get the file size

    // pf.fd = open(current file, O_RDONLY);
    // pf.data = malloc(file size);

    // processing...

    }

    then in a simple for loop before exit u can just do the closing /
    deleting the files and freeing up the memory.

    }

    U need to use

    stdio.h, stdlib.h, malloc.h, fcntl.h, sys/stat.h, sys/types.h


    Hope u get the logic.

    -Vighnesh
    srini wrote:
    > Hi
    >
    > I need to create two-dimensional array; number of rows and columns
    > unknown at compile time. The idea is to read a file, print it if
    > requested by user and to delete when the program quits.
    >
    > Here is what I came up with using C:
    >
    > struct diskinfo {
    >
    > char **part_name; /* name of each partition */
    > int **num_blocs;
    >
    > }
    >
    > I need only one instance of 'diskinfo' in the program. What is the best
    > way to create & free memory and what are the C libraries that I should
    > include for this ?
    >
    > Thanks for your time and help
    > -Srini
    , Dec 14, 2005
    #2
    1. Advertising

  3. srini

    Chuck F. Guest

    wrote:
    >

    .... snip ...
    >
    > U need to use
    >
    > stdio.h, stdlib.h, malloc.h, fcntl.h, sys/stat.h, sys/types.h
    >
    > Hope u get the logic.


    Please do not top-post, especially in technical newsgroups. And
    especially do not use silly abbreviations, such as 'u'. They make
    you appear ignorant, besides creating a problem for readers.

    In addition, malloc.h, fcntl.h, sys/*.h are all non-standard
    headers and are off-topic in c.l.c. Such misinformation is worse
    than simply ignoring the OPs query.

    --
    Read about the Sony stealthware that is a security leak, phones
    home, and is generally illegal in most parts of the world. Also
    the apparent connivance of the various security software firms.
    http://www.schneier.com/blog/archives/2005/11/sonys_drm_rootk.html
    Chuck F., Dec 14, 2005
    #3
  4. srini

    srini Guest

    wrote:

    > I am slightly modifying the structure


    Vighnesh, thanks for the response; I'm specifically trying to implement
    dynamic two dimensional arrays to undestand usage of malloc() and
    free() for a structure that has 'pointers to pointers' along with other
    data types.
    srini, Dec 14, 2005
    #4
  5. On 13 Dec 2005 21:17:11 -0800, "srini" <> wrote:

    >Hi
    >
    >I need to create two-dimensional array; number of rows and columns
    >unknown at compile time. The idea is to read a file, print it if
    >requested by user and to delete when the program quits.


    See question 6.16 in the FAQ at
    http://www.eskimo.com/~scs/C-faq/top.html


    <<Remove the del for email>>
    Barry Schwarz, Dec 18, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nafai
    Replies:
    7
    Views:
    319
    John Harrison
    Sep 29, 2004
  2. Yossarian

    Pointer to Pointer & Mem allocation

    Yossarian, Jun 7, 2004, in forum: C Programming
    Replies:
    2
    Views:
    3,671
    Mike Wahler
    Jun 7, 2004
  3. Ken
    Replies:
    24
    Views:
    3,862
    Ben Bacarisse
    Nov 30, 2006
  4. Replies:
    8
    Views:
    313
    jaysome
    Mar 1, 2007
  5. news.aon.at
    Replies:
    11
    Views:
    645
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page