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

  • Thread starter Simon Nickerson
  • Start date
S

Simon Nickerson

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?
 
M

Martin Dickopp

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
 
T

Tom Zych

Simon said:
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));

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.
 
B

Ben Pfaff

Tom Zych said:
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.
 
T

Tom Zych

Ben said:
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,
 
B

Ben Pfaff

Tom Zych said:
Ben said:
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"
 
B

bd

Simon said:
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)
 
S

Simon Nickerson

Tom Zych said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top