How can I use malloc-ed memory inside a local function

Discussion in 'C Programming' started by Simon Nickerson, Sep 5, 2003.

  1. I have a function which looks like this:

    void rho(matrix_t *out, matrix_t *in)
    {
    static int firsttime = 1;
    static matrix_t *words;
    /* ... other variables ... */

    if (firsttime) {
    firsttime = 0
    words = malloc(NUM_WORDS * sizeof(matrix_t));
    if (!words) {
    /* complain */
    }
    /* calculate words[0], ... words[NUM_WORDS-1] */
    }

    /* other processing */
    }

    The idea is that a number of matrices are computed the first time
    the function is run, which can then be used for subsequent runs.
    However, the memory never gets freed. Is this legal, and if not,
    what's the best way round it?

    --
    Simon Nickerson
     
    Simon Nickerson, Sep 5, 2003
    #1
    1. Advertising

  2. (Simon Nickerson) writes:

    > However, the memory never gets freed. Is this legal,


    The C standard doesn't say anything about it. Whether or not it is good
    style has already been debated here without a consensus in the past.

    As matter of fact, in some environments all the memory is automatically
    freed when a program terminates. In others, it's not.

    Martin
     
    Martin Dickopp, Sep 5, 2003
    #2
    1. Advertising

  3. Simon Nickerson

    Tom Zych Guest

    Simon Nickerson wrote:

    > I have a function which looks like this:


    > void rho(matrix_t *out, matrix_t *in)
    > {
    > static int firsttime = 1;
    > static matrix_t *words;
    > /* ... other variables ... */
    >
    > if (firsttime) {
    > firsttime = 0


    I hope the actual function has a semicolon there.

    > words = malloc(NUM_WORDS * sizeof(matrix_t));


    <snip>

    > The idea is that a number of matrices are computed the first time
    > the function is run, which can then be used for subsequent runs.
    > However, the memory never gets freed. Is this legal, and if not,
    > what's the best way round it?


    It's perfectly legal. Whether it's good design depends on whether
    your program will want to keep these matrices until it terminates,
    and whether your platform frees everything upon termination
    (bearing in mind that either or both could change).

    If you want to free it, I'd suggest putting the function in a
    module[1] by itself, make the matrix object a local global[1],
    and add another function that frees it. Then just call that on
    exit.

    [1] I trust my meaning is clear, but would the more experienced
    readers please tell me what you would call these things? Thanks.

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 5, 2003
    #3
  4. Simon Nickerson

    Ben Pfaff Guest

    Tom Zych <> writes:

    > If you want to free it, I'd suggest putting the function in a
    > module[1] by itself, make the matrix object a local global[1],
    > and add another function that frees it. Then just call that on
    > exit.
    >
    > [1] I trust my meaning is clear, but would the more experienced
    > readers please tell me what you would call these things? Thanks.


    I think you mean a variable with internal linkage, static
    lifetime, and file scope.
    --
    "A lesson for us all: Even in trivia there are traps."
    --Eric Sosman
     
    Ben Pfaff, Sep 5, 2003
    #4
  5. Simon Nickerson

    Tom Zych Guest

    Ben Pfaff wrote:

    > > If you want to free it, I'd suggest putting the function in a
    > > module[1] by itself, make the matrix object a local global[1],
    > > and add another function that frees it. Then just call that on
    > > exit.


    > I think you mean a variable with internal linkage, static
    > lifetime, and file scope.


    Internal linkage meaning that the resulting object code neither
    exports the symbols, not expects to find them elsewhere? If so, 3
    out of 3.

    Is there a shorter term in common use?

    Thanks,
    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 5, 2003
    #5
  6. Simon Nickerson

    Ben Pfaff Guest

    Tom Zych <> writes:

    > Ben Pfaff wrote:
    >
    > > > If you want to free it, I'd suggest putting the function in a
    > > > module[1] by itself, make the matrix object a local global[1],
    > > > and add another function that frees it. Then just call that on
    > > > exit.

    >
    > > I think you mean a variable with internal linkage, static
    > > lifetime, and file scope.

    >
    > Internal linkage meaning that the resulting object code neither
    > exports the symbols, not expects to find them elsewhere?


    Yes.

    > If so, 3 out of 3.
    >
    > Is there a shorter term in common use?


    "file-scope static"
     
    Ben Pfaff, Sep 5, 2003
    #6
  7. Simon Nickerson

    Tom Zych Guest

    Ben Pfaff wrote:

    > > Is there a shorter term in common use?


    > "file-scope static"


    Ah. Thank you.

    --
    Tom Zych
    This email address will expire at some point to thwart spammers.
    Permanent address: echo '' | rot13
     
    Tom Zych, Sep 5, 2003
    #7
  8. Simon Nickerson

    bd Guest

    Simon Nickerson wrote:

    > I have a function which looks like this:
    >
    > void rho(matrix_t *out, matrix_t *in)
    > {
    > static int firsttime = 1;
    > static matrix_t *words;
    > /* ... other variables ... */
    >
    > if (firsttime) {
    > firsttime = 0
    > words = malloc(NUM_WORDS * sizeof(matrix_t));


    If it's a fixed size, why not:
    void rho(matrix_t *out, matrix_t *in)
    {
    static int firsttime = 1;
    static matrix_t words[NUM_WORDS];
    /* ... */
    }

    > The idea is that a number of matrices are computed the first time
    > the function is run, which can then be used for subsequent runs.
    > However, the memory never gets freed. Is this legal, and if not,
    > what's the best way round it?


    I don't know if it's legal as-is, but you could use atexit() to free it,
    provided you move the pointer out of the function. (You can use static to
    restrict it to the file it's defined in, though)

    --
    Work expands to fill the time available.
    -- Cyril Northcote Parkinson, "The Economist", 1955
     
    bd, Sep 6, 2003
    #8
  9. Tom Zych <> wrote in message news:<>...
    > Simon Nickerson wrote:
    >
    > > void rho(matrix_t *out, matrix_t *in)
    > > {
    > > static int firsttime = 1;
    > > static matrix_t *words;
    > > /* ... other variables ... */
    > >
    > > if (firsttime) {
    > > firsttime = 0

    >
    > I hope the actual function has a semicolon there.


    Yes, mea culpa.

    > It's perfectly legal. Whether it's good design depends on whether
    > your program will want to keep these matrices until it terminates,
    > and whether your platform frees everything upon termination
    > (bearing in mind that either or both could change).


    Thanks very much to everyone who replied. As it happens, the old
    approach
    turned out to be too inflexible, so in the end, I decided to split my
    function up into three functions:

    int create_rho_context(rhocontext_t *context /* and some other
    variables */);
    void rho(rhocontext_t *context, matrix_t *out, matrix_t *in);
    void destroy_rho_context(rhocontext_t *context);

    where rhocontext_t is a (typedef for) a struct containing the
    precomputed matrices and some other data.

    --
    Simon Nickerson
     
    Simon Nickerson, Sep 9, 2003
    #9
    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. karim
    Replies:
    1
    Views:
    783
    George Ter-Saakov
    Jun 26, 2003
  2. Peter
    Replies:
    34
    Views:
    2,019
    Richard Tobin
    Oct 22, 2004
  3. Sullivan WxPyQtKinter
    Replies:
    10
    Views:
    696
    Antoon Pardon
    Nov 8, 2007
  4. Gene
    Replies:
    0
    Views:
    460
  5. Sur
    Replies:
    4
    Views:
    211
Loading...

Share This Page