473,387 Members | 1,863 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,387 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 4646
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.