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_WORD S * 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 15 4684 sj***@cantab.ne t (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
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_WORD S * 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 sj***@cantab.ne t (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
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
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
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_WORD S * 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
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"
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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>
#include <malloc.h>
#include <string.h>
|
by: mike79 |
last post by:
hi all..
if I wanted to malloc a struct, say the following:
struct myStruct1
{
int number;
char *string;
}
|
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 (perhaps mistakenly) that the
purpose of a void pointer was to cast into a legitimate date type. Is
this wrong? Why, and what is considered to be correct form?
|
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 type-safe and concise.
Compare:
NEW(int, x, 1000);
|
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);
printf("before malloc: %s (%p)\n", input+INPUTLEN);
hchars = (char *)malloc(freq*sizeof(char));
schars = (char *)malloc(freq*sizeof(char));
| |
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 virtual memory?
-Chess
|
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 http://www.eskimo.com/~scs/C-faq/faq.html
but it doesn't seem to answer my questions...
So, I've made an example behind, with some included questions...
|
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>
|
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 currently implementing, NULL is returned.
Is it wrong ?)
|
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 struct pxl {
double lon, lat;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |