Solved

Dynamically Allocated Static Variable

Posted on 2008-06-09
10
600 Views
Last Modified: 2011-10-03
if i have a function with a static local variable, that's dynamically allocated once:
int funct(void)
{
     static char* buffer = NULL;

     if (!buffer)
          buffer = malloc(SIZESLOW);
}

If there's no explicit free call, and if the process is killed; does this constitute as a leak? When the process is killed, is the heap essentially freed? I guess I'm after an in depth explanation on what happens on a windows platform (i imagine unix would be in the same boat)
0
Comment
Question by:spiffles
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21748163
When the process is killed, memory is freed
0
 

Author Comment

by:spiffles
ID: 21748290
And from an efficiency and/or design perspective would you use this kind of allocation?
0
 
LVL 84

Accepted Solution

by:
ozo earned 20 total points
ID: 21748319
If SIZESLOW is unknown until runtime I might use it.
What is the purpose of buffer in your function?
What is the purpose of funct in your program?
0
 
LVL 15

Assisted Solution

by:efn
efn earned 20 total points
ID: 21748337
It's platform-dependent, not guaranteed by the language.  I believe ozo's answer is correct for most, if not all, current operating systems, but I believe it is not true for some old versions of Windows.

From an efficiency perspective, it's fine.  If you have a good reason for allocating the memory dynamically (e.g., SIZESLOW is not known until run time, as ozo said), it's fine.  If you could just use a statically allocated array, that would be simpler and save you from worrying about whether the memory leaks, so I'd consider that preferable, but not by much.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 84

Expert Comment

by:ozo
ID: 21748385
> not guaranteed by the language
This is true.  The language specs say nothing about what happens after the program exits.
0
 

Author Comment

by:spiffles
ID: 21748554
Yeah I'm doing some code reviews and was worried about a potential leak since there's no explicit free call. By the sounds of it, it's probably safe for the platform it's deployed on.

Closer inspection reveals SIZESLOW has elements which could be updated after a product restart. There's also a comment about dynamic allocation being used to avoid stack overflow. Anyways thanks ozo, efn for the feedback.

0
 
LVL 3

Expert Comment

by:aphillips
ID: 21756236
This is not specified by the language.  However, I have never heard of any compiler that does not release the memory when the program terminates.

Note that the behaviour is implementation specific but depends on the compiler run-time routines (in cooperation with the operating system in some implementations).

However, one important thing to note is that some run-time libraries may report this as a problem.  For example, Microsoft compilers have for some time been reporting this sort of allocation as a "memory leak" in debug builds, although a one-off allocation is not a "leak" more a "splash".  But if you rely on this reporting to find real memory leaks you may want to free the memory when the program terminates.  In C you can do this by registering a function pointer with atexit().
0
 

Author Comment

by:spiffles
ID: 21766960
Yes atexit would be a good alternative, but since there's no precedence in the existing code based, I probably won't use it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now