UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » Destroying a static varible

#1 2006-05-24 09:21 AM

Lloyd
Member
Registered: 2006-02-13
Posts: 53

Re: Destroying a static varible

How can I destroy a local static variable before the program terminates, though it consumes lot of memory? The variable is declared inside a public method of a class and I would like to delete it from another class. Any efficient way to do this?

Offline

#2 2006-05-24 12:02 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Destroying a static varible

You can't. The whole point of static variables is that they exist all the time. So their memory is automatically reserved at program startup, and freed at program exit (like all other program memory still in use, so freeing memory just before exit doesn't make much sense anyway).

Offline

#3 2006-05-25 02:38 AM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: Destroying a static varible

I agree static variables cannot be destroyed/unallocated before process termination--that is what static means. It has the same lifetime as a global but is only scoped (accessible) locally. Why not just use heap memory (via new operator or the C malloc(), etc.)?

Offline

#4 2006-05-25 05:00 AM

Lloyd
Member
Registered: 2006-02-13
Posts: 53

Re: Destroying a static varible

You mentioned that its scope is local and life time is the whole program. I want to share variable's value commonly (This variable is an object) among the multiple instances of the classes at various point of time in the program. That's why I am using static but not interseted in global variable.

I convince myself that destroying a static variable doesn't make much sence. But how can I share a variable's value among multiple instances of a class?

Let me come to the actual scenario

I am using a linked list in my program. It is implemented using a class. I made this list as a local object of one method of another class. The program runs in a for loop and the class is instantiated multiple times and the method too is called multiple times. but I dont want to create the linked list object each time. So I made it as a static object. After the for loop exits I know that the list is no more needed. So I want to destroy it from outside the class. Is it possible through any good way?

Offline

#5 2006-05-25 11:06 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Destroying a static varible

I think you should post your code to make clear what exactly is going on. But from what you told us it seems you should't use a static local list, but only store a pointer to a list object which is given to the constructor or something. Using static is also fine, but don't bother freeing the used memory, as you can't. If you really want to free up that list, then don't make it static.

Offline

#6 2006-05-28 03:41 PM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: Destroying a static varible

Offline

  • Index
  • » C++
  • » Destroying a static varible

Board footer

Powered by FluxBB