Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How can I use malloc-ed memory inside a local function

Reply
Thread Tools

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

 
 
Simon Nickerson
Guest
Posts: n/a
 
      09-05-2003
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
 
Reply With Quote
 
 
 
 
Martin Dickopp
Guest
Posts: n/a
 
      09-05-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Tom Zych
Guest
Posts: n/a
 
      09-05-2003
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 '(E-Mail Removed)' | rot13
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-05-2003
Tom Zych <(E-Mail Removed)> 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
 
Reply With Quote
 
Tom Zych
Guest
Posts: n/a
 
      09-05-2003
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 '(E-Mail Removed)' | rot13
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-05-2003
Tom Zych <(E-Mail Removed)> 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"
 
Reply With Quote
 
Tom Zych
Guest
Posts: n/a
 
      09-05-2003
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 '(E-Mail Removed)' | rot13
 
Reply With Quote
 
bd
Guest
Posts: n/a
 
      09-05-2003
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

 
Reply With Quote
 
Simon Nickerson
Guest
Posts: n/a
 
      09-09-2003
Tom Zych <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Defining a function inside a function. Whats this feature ? How touse inside a class ? Sur Ruby 4 01-08-2008 02:50 PM
Can local function access local variables in main program? Sullivan WxPyQtKinter Python 10 11-08-2007 02:51 PM
Re: Can We Use EXEC CGI Inside a JavaScript Applet Inside an HTML File? David Dorward HTML 1 06-28-2003 09:30 AM
Can't use 'local' to find sql server instances on local machine karim ASP .Net 1 06-26-2003 09:17 PM



Advertisments