473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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
Nov 13 '05 #1
15 4643
sj***@cantab.net (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
Nov 13 '05 #2
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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #3
sj***@cantab.net (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
Nov 13 '05 #4
Tom Zych <tz******@pobox.com> 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
Nov 13 '05 #5
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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #6
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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #7
Tom Zych <tz******@pobox.com> 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"
Nov 13 '05 #8
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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #9
Tom Zych <tz******@pobox.com> 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
Nov 13 '05 #10
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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #11
Tom Zych <tz******@pobox.com> 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"
Nov 13 '05 #12
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 'g******@cbobk.pbz' | rot13
Nov 13 '05 #13
bd
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

Nov 13 '05 #14
bd
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

Nov 13 '05 #15
Tom Zych <tz******@pobox.com> wrote in message news:<3F***************@pobox.com>...
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
Nov 13 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h>...
13
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
231
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought...
36
by: MSG | last post by:
The answer is neither. Use macros. #define ALLOC(size, type) ((type) *) malloc((size) * sizeof(type)) #define NEW(type, name, size) (type) * (name) = ALLOC((size), (type)) They are both ...
11
by: Gustavo G. Rondina | last post by:
Hi all I'm writting a simple code to solve an ACM problem (http://acm.uva.es, it is the problem #468). In its code I have the following fragment: freq = calcfreq(hashfreq, strfreq, input);...
27
by: Chess Saurus | last post by:
I'm getting a little bit tired of writing if (a = malloc(...) == NULL) { // error code } I mean, is it really possible that a malloc call could fail, except in the case of running out of...
7
by: Rano | last post by:
/* Hello, I've got some troubles with a stupid program... In fact, I just start with the C language and sometime I don't understand how I really have to use malloc. I've readden the FAQ...
19
by: ceo | last post by:
hi, why do i get the following error? do i need to explicitly typecast the pointer returned by malloc to char *? thanks, ceo # cat malloc.cpp #include <stdio.h>
82
by: quiberon2 | last post by:
Hi, Sorry if it might be a stupid question but what should returns malloc(0) ? void *ptr = malloc(0); I am running gcc 3.3.5 and a non-null address is returned. ( in the compiler that I am...
23
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.