473,785 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_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
Nov 13 '05 #1
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
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_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
Nov 13 '05 #3
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
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_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
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

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

Similar topics

59
5204
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>
13
83901
by: mike79 | last post by:
hi all.. if I wanted to malloc a struct, say the following: struct myStruct1 { int number; char *string; }
231
23247
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?
36
6695
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);
11
1645
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));
27
4279
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
7
2216
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...
19
2470
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
31157
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 ?)
23
2731
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;
0
9647
marktang
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...
0
10356
Oralloy
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...
0
10161
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 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...
1
10098
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,...
0
9958
tracyyun
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...
0
8986
agi2029
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...
1
7506
isladogs
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...
0
5390
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...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.