structure member allocation

From: Aaron Walker (ka0ttic_at_REMOVETHIScfl.rr.com)
Date: 11/29/03

  • Next message: nrk: "Re: structure member allocation"
    Date: Sat, 29 Nov 2003 02:05:53 GMT
    
    

    I am trying to write this piece of code that reads a config file, in the
    format:

    ENTRY value

    and I want to read the config file, then read the "value" into the
    appropriate member of the following structure:

    struct conf_data {
            char *root;
            char *pid_file;
            char *log_dir;
            char *local_addr;
            unsigned int port;
            unsigned short child_min;
            unsigned short child_max;
    };

    typedef struct conf_data Config;

    I want the 4 char pointers to be dynamically allocated at runtime,
    depending on the length of the "value" in the config file.

    I have a fuction, get_conf_data that is run from main():

    Config *get_conf_data(void)
    {
            Config *conf;
            ...
            return conf;
    }

    My question is, do I need to allocate memory for the structure first
    then the members, like:

    conf = malloc(sizeof(Config));
    conf->root = malloc(strlen(this_value));
    conf->pid_file = malloc(strlen(that_value));

    if I do need to do that way, then when I am free()ing, do I do it in the
    opposite order?

    int main()
    {
            Config *conf = get_conf_data();
            ...
            free(conf->root);
            free(conf->pid_file);
            ...
            free(conf);
    }

    Or am I totally off-base here?

    Thanks,
    Aaron


  • Next message: nrk: "Re: structure member allocation"

    Relevant Pages

    • Re: [SLE] Again: cant write to floppy
      ... And Doug, if you're nervous about changing a config file make ... it a habit to never delete an entry but comment it out. ... So your edited conf file should look: ...
      (SuSE)
    • Re: "Class not registered" error when assembly registered with multiple versions
      ... When the framework starts up to look for your assembly I believe it ... takes note of redirects in your config file. ... Alternatively you need to fix up the CLSID registration entries. ... the 1.0.2819.34866 entry that messes things up. ...
      (microsoft.public.dotnet.framework.interop)
    • Re: structure member allocation
      ... > ENTRY value ... > appropriate member of the following structure: ... > I want the 4 char pointers to be dynamically allocated at runtime, ...
      (comp.lang.c)
    • Re: [SLE] Ports for NFS.
      ... On Tuesday 07 Sep 2004 20:03 pm, John N. Alegre wrote: ... >> the config file is, and how to restart the Firewall after the ... > get past the Firewall I am guessing that the entry ...
      (SuSE)
    • Re: remove entry from text file [NOT A HOMEWORK]
      ... Roman Mashak wrote: ... I implemented simple program to eliminate entry from the file having the following structure (actually it's config file of 'named' DNS package for those who care and know): ...
      (comp.lang.c)