473,666 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Allocating heap to unfreeable function static variables.

I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stu ff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?

Cheers,
Charlie
Sep 12 '08 #1
13 1986
charlie <ch************ *@gmail.comwrit es:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stu ff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?
It is certainly horrible to name static variables "temp" anything.

But to answer your question this is simply horrible. You have no concept
of ownership and this could and will lead to horrific bugs later down
the line where someone else calls fn() without paying proper heed to
already "owns" the contents it pretends to guard.
Sep 12 '08 #2
On Sep 12, 10:58*am, charlie <ch************ *@gmail.comwrot e:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
* * *static int *temp_stuff = NULL;
* * *static int ntemp_stuffs = 0;

* * *if(nstuffs ntemp_stuffs) {
* * * * *ntemp_stuffs = nstuffs;
* * * * *temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
* * *}

* * */* Use the temp stuffs for something */
* * *memcpy(temp_st uff, stuff, nstuffs * sizeof(int));

}

Is this a horrible thing to do?
It's rather inconvenient and unintuitive, not to mention it isn't
thread-safe. Also, you didn't check whether realloc() succeeded, and
if you do check, and if it fails, you can only return without doing
anything because your function returns void. The solution is to use a
VLA (variable-length array).

Sebastian

Sep 12 '08 #3
charlie wrote:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stu ff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?

Cheers,
Charlie
It depends on your discipline when using it.

I had a similar problem in my IDE for C programming
when I was programming under windows 3.1 (16 bits).

To avoid mallocing each time I needed some buffer
and to save stack space that was at a premimum
I allocated at startup a global "buf" of 35K

I used it for temporary strings before I stuffed
them into list boxes, to pass data between functions
(saving stack), and many othere terribly "clever"
uses.

Then, windows 95 came along, and memory and stack weren't
THAT important, even though still important. The IDE gew,
I added a debugger, and "buf" started producing strange
problems. A procedure would use buf to pass data to another
but some routine in that call tree would ALSO use buf

This produced a lot of bugs, and led me to a big rewrite
eliminating buf from the software (as far as I could)

The rule is:

"Never assume buf is alive through a function call"
"Only use buf in a SINGLE THREAD", maybe the main thread.
It is a horrible thing to do?

Under Vista 64 YES.
Under windows 3.1 NO
Under a small embedded system? Maybe, it depends the security
requirements of the system.
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Sep 12 '08 #4


charlie wrote:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
If you're worried about it being unfreeable, just provide a mechanism
to free it:
void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;
if(stuff == NULL)
{ // you should be checking for this anyway
free(temp_stuff );
temp_stuff = NULL;
ntemp_stuffs = 0;
}
else
if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stu ff, stuff, nstuffs * sizeof(int));
}
Sep 12 '08 #5
On Sep 12, 12:20*pm, s0s...@gmail.co m wrote:
On Sep 12, 10:58*am, charlie <charlie.burr.. .@gmail.comwrot e:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
void fn(int *stuff, int nstuffs)
{
* * *static int *temp_stuff = NULL;
* * *static int ntemp_stuffs = 0;
* * *if(nstuffs ntemp_stuffs) {
* * * * *ntemp_stuffs = nstuffs;
* * * * *temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
* * *}
* * */* Use the temp stuffs for something */
* * *memcpy(temp_st uff, stuff, nstuffs * sizeof(int));
}
Is this a horrible thing to do?

It's rather inconvenient and unintuitive, not to mention it isn't
thread-safe. Also, you didn't check whether realloc() succeeded, and
if you do check, and if it fails, you can only return without doing
anything because your function returns void. The solution is to use a
VLA (variable-length array).

Sebastian
Thank-you, you make great arguments. Thankfully I can mandate c99 and
just use VLAs and yes, the code would have to be thread safe.

C
Sep 12 '08 #6
charlie <ch************ *@gmail.comwrit es:
On Sep 12, 12:20*pm, s0s...@gmail.co m wrote:
>On Sep 12, 10:58*am, charlie <charlie.burr.. .@gmail.comwrot e:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
void fn(int *stuff, int nstuffs)
{
* * *static int *temp_stuff = NULL;
* * *static int ntemp_stuffs = 0;
* * *if(nstuffs ntemp_stuffs) {
* * * * *ntemp_stuffs = nstuffs;
* * * * *temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
The value of temp_stuff at this point is guaranteed to be NULL. Why
use realloc rather than malloc? (Perhaps realloc makes sense in your
actual code.)
* * *}
* * */* Use the temp stuffs for something */
* * *memcpy(temp_st uff, stuff, nstuffs * sizeof(int));
}
Is this a horrible thing to do?

It's rather inconvenient and unintuitive, not to mention it isn't
thread-safe. Also, you didn't check whether realloc() succeeded, and
if you do check, and if it fails, you can only return without doing
anything because your function returns void. The solution is to use a
VLA (variable-length array).

Sebastian

Thank-you, you make great arguments. Thankfully I can mandate c99 and
just use VLAs and yes, the code would have to be thread safe.
A few problems with VLAs:

There's no standard way to detect an allocation failure; if there's
not enough memory to create a VLA, the behavior is undefined. (The
same problem occurs with ordinary fixed-size arrays, but it's probably
more likely to show up with VLAs.)

There's no equivalent of realloc for VLAs. The array is allocated
once when it's created, and deallocated when it leaves its scope. The
size can't change during its lifetime.

At least one major implementation, gcc, has a "broken" implementation
of VLAs. I don't know just what the problem is (probably some
misbehavior in some relatively obscure cases), but if you're using gcc
you'll need to watch out for that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 12 '08 #7
On Sep 12, 2:58*pm, Keith Thompson <ks***@mib.orgw rote:
charlie <ch************ *@gmail.comwrit es:
>On Sep 12, 12:20*pm, s0s...@gmail.co m wrote:
>>On Sep 12, 10:58*am, charlie <charlie.burr.. .@gmail.comwrot e:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:
>void fn(int *stuff, int nstuffs)
{
* * *static int *temp_stuff = NULL;
* * *static int ntemp_stuffs = 0;
>* * *if(nstuffs ntemp_stuffs) {
* * * * *ntemp_stuffs = nstuffs;
* * * * *temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));

The value of temp_stuff at this point is guaranteed to be NULL. *Why
use realloc rather than malloc? *(Perhaps realloc makes sense in your
actual code.)
>* * *}
>* * */* Use the temp stuffs for something */
* * *memcpy(temp_st uff, stuff, nstuffs * sizeof(int));
>}
>Is this a horrible thing to do?
>>It's rather inconvenient and unintuitive, not to mention it isn't
thread-safe. Also, you didn't check whether realloc() succeeded, and
if you do check, and if it fails, you can only return without doing
anything because your function returns void. The solution is to use a
VLA (variable-length array).
>>Sebastian
>Thank-you, you make great arguments. Thankfully I can mandate c99 and
just use VLAs and yes, the code would have to be thread safe.

A few problems with VLAs:

There's no standard way to detect an allocation failure; if there's
not enough memory to create a VLA, the behavior is undefined. *(The
same problem occurs with ordinary fixed-size arrays, but it's probably
more likely to show up with VLAs.)
Why? The compiler generates instructions to allocate the given size. I
would expect that the probability of failure depends on the size of
the array, but you can check that before allocating the VLA anyway.
There's no equivalent of realloc for VLAs. *The array is allocated
once when it's created, and deallocated when it leaves its scope. *The
size can't change during its lifetime.
....which is precisely what the OP needed. Hence the "temp" variables.
malloc() and realloc() are only inconvenient here.

Sebastian

Sep 12 '08 #8
On Fri, 12 Sep 2008 08:58:29 -0700 (PDT), charlie
<ch************ *@gmail.comwrot e:
>I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
static int *temp_stuff = NULL;
static int ntemp_stuffs = 0;

if(nstuffs ntemp_stuffs) {
ntemp_stuffs = nstuffs;
temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));
}

/* Use the temp stuffs for something */
memcpy(temp_stu ff, stuff, nstuffs * sizeof(int));
}

Is this a horrible thing to do?
The answer is, Maybe.

There are some faults that others have noted, e.g., your code
doesn't handle allocation failures and it doesn't provide for
freeing the space. The faults are easily fixed; fix them.

The up side is that in some applications you can get a
significant performance boost with this device. I have seen
applications where 40% of the execution time was spent in
allocating and freeing scratch space.

There are some caveats that others have noted, basically that it
requires usage discipline and that it is not thread safe. Also
it doesn't work with recursion.

Thread safety, recursion don't mix very well with static storage.

Your usage is okay because it is providing scratch space that is
not used outside the function -- if fn does not implicitly or
explicitly call itself.

I have used that device extensively in some code in the past. I
wouldn't do it now. Rather, I use special purpose allocators
that supply temporary space without the overhead of malloc/free.
Calling a special purpose allocator isn't quite as time efficient
as inline code but it is safer and cleaner. YMMV.

Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Sep 12 '08 #9
On Fri, 12 Sep 2008 12:58:17 -0700, Keith Thompson
<ks***@mib.orgw rote:
>charlie <ch************ *@gmail.comwrit es:
>On Sep 12, 12:20*pm, s0s...@gmail.co m wrote:
>>On Sep 12, 10:58*am, charlie <charlie.burr.. .@gmail.comwrot e:
I came up with this idiom for cases where a function needs a variable
amount of memory for it's temporary storage so as to avoid constantly
mallocing. It makes me feel a little uncomfortable to have unfreeable
(but still technically reachable) memory hanging around but it is, I
think still a useful thing to do:

void fn(int *stuff, int nstuffs)
{
* * *static int *temp_stuff = NULL;
* * *static int ntemp_stuffs = 0;

* * *if(nstuffs ntemp_stuffs) {
* * * * *ntemp_stuffs = nstuffs;
* * * * *temp_stuff = realloc(temp_st uff, ntemp_stuffs * sizeof(int));

The value of temp_stuff at this point is guaranteed to be NULL. Why
use realloc rather than malloc? (Perhaps realloc makes sense in your
actual code.)
???? temp_stuff is static; it will only be NULL on the first
call; thereafter it will be whatever it was at the end of the
previous invocation. That's the point of this, er, hack - the
space only has to be reallocated if the amount of space used last
time isn't enough.
>
>* * *}

* * */* Use the temp stuffs for something */
* * *memcpy(temp_st uff, stuff, nstuffs * sizeof(int));

}

Is this a horrible thing to do?

It's rather inconvenient and unintuitive, not to mention it isn't
thread-safe. Also, you didn't check whether realloc() succeeded, and
if you do check, and if it fails, you can only return without doing
anything because your function returns void. The solution is to use a
VLA (variable-length array).

Sebastian

Thank-you, you make great arguments. Thankfully I can mandate c99 and
just use VLAs and yes, the code would have to be thread safe.

A few problems with VLAs:

There's no standard way to detect an allocation failure; if there's
not enough memory to create a VLA, the behavior is undefined. (The
same problem occurs with ordinary fixed-size arrays, but it's probably
more likely to show up with VLAs.)

There's no equivalent of realloc for VLAs. The array is allocated
once when it's created, and deallocated when it leaves its scope. The
size can't change during its lifetime.

At least one major implementation, gcc, has a "broken" implementation
of VLAs. I don't know just what the problem is (probably some
misbehavior in some relatively obscure cases), but if you're using gcc
you'll need to watch out for that.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
Save the Earth now!!
It's the only planet with chocolate.
Sep 12 '08 #10

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

Similar topics

17
5029
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
10
8328
by: Shuo Xiang | last post by:
Greetings: I know that variables declared on a stack definitely does not reside in heap space so there is only a very limited amount of stuff that you can store in the stack of a function. But what about the global space? Is that the same as the heap space or is that still a form of special stack? Because once I've seen someone declare a 1 megabyte char array in global space like this: char s;
22
3041
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said structure. The compiler obviously allows VLA however it craps out after the above amount of bytes. I was told i was attempting to put everythng on the stack and not the heap. So i was wondering if anyone can maybe clear it up, is that true? would i...
24
2861
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as are structs, where classes are on the heap... when value types go out of scope the memory is re- allocated, object remain in memory waiting to be cleaned up by the garbage collector, etc, but he responded 'so why not just put say a class on the...
16
4438
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
53
26363
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
20
2079
by: Neclepsio | last post by:
Hi everyone. I've made a class Matrix, which contains a pointer to the data and some methods which all return a copy of the matrix modified in some way. The program works quite well for small matrices, but as matrix dimension grows up it crashes in deterministic locations depending on data, machine and OS. In particular, this happens when I test it with 3000x3000 matrices (which take up more than 30MB) under three different machines/OSes....
13
4445
by: karthikbalaguru | last post by:
Hi, Memory allocated in heap remains until the end of the program. So, global variables & static variables are allocated on heap. In the same time, I find that BSS also allows the placement of static variables and global variables initialised to zero. So, while run-time, where does the static/global variables lie in the Memory Organisation.
1
232
by: charlie | last post by:
I came up with this idiom for cases where a function needs a variable amount of memory for it's temporary storage so as to avoid constantly mallocing. It makes me feel a little uncomfortable to have unfreeable (but still technically reachable) memory hanging around but it is, I think still a useful thing to do: void fn(int *stuff, int nstuffs) { static int *temp_stuff = NULL; static int ntemp_stuffs = 0;
0
8445
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
8781
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
8551
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
8640
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...
1
6198
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
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.