Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Unused Static Stack Varibles

Name: Anonymous 2009-10-31 1:48

Hey /prog/,

In C, are variables located in the stack recycled/freed when they are no longer needed? I suppose that this depends on the compiler, but does anyone know if GCC does this?

To be a little clearer, let's say I have these two translation units...

main.c/.h
settings.c/.h

main() calls the function get_settings() (located in settings.c) which will fill the variables (located in main.c) with data read from a file and then returns.

Since there is no longer any purpose for the staic variables declared in settings.c, are they freed once get_settings() returns?

Name: Anonymous 2009-10-31 1:50

As far as I know, any variable which is declared without using a pointer will be freed when that variable loses scope.

Variables declared using a pointer must be explicitly deleted or they will remain in memory.

Name: Anonymous 2009-10-31 2:00

>As far as I know, any variable which is declared without using a pointer will be freed when that variable loses scope.

How about variables declared at the top of files (outside the scope of functions) or better known as global variables?

When declared as static, do they consider the translation unit the scope?

Name: Anonymous 2009-10-31 2:22

>>3
Anything that is not malloc'd, calloc'd, realloc'd, or brk (or equivalent)'d is on the heap. Everything else is on the stack and is freed automatically when it loses scope. For variables at the top of files I believe they are on the stack for the entirety of execution.

Name: Anonymous 2009-10-31 2:25

>>4
Sorry, should be
Anything that is malloc'd ...

Name: Anonymous 2009-10-31 2:29

>>1
Since there is no longer any purpose for the staic variables declared in settings.c, are they freed once get_settings() returns?

Absolutely not. There's no way for the compiler to know whether get_settings() will be called again.

Name: Anonymous 2009-10-31 2:42

Local variables of limited extent(scope) are subject to register allocation in most sane compilers, and when a register or stack location which was asseigned to a variable which is no longer needed, that space can be reallocated to another variable. Any serious compiler does this, and I've examined plenty of disassemblies of compiled C programs which show the same register used for the different variables in different places in the function, this also applies to locations on the stack.

While the ways in which each compiler does register allocation may be different, you should know that once a variable is out of scope, its location in memory/regs may become used by something else.

Name: Anonymous 2009-10-31 2:54

>>4
No. File-local variables (i.e. variables in the global scrope but declared static) and global variables are just stored as part of the executable which is mapped into the process's address space by the linker.

Name: Anonymous 2009-10-31 3:20

THERE ARE FUCKING 8 RESPONSES THAT ARE SERIOUS AND NOT ONE IS RIGHT?  HOLY SHIT! At least troll him.

Static means the variable is static. It will never move or go away. It will last the entire program.

Now that you have your answer, please respond with sage!

Name: Anonymous 2009-10-31 3:20

>>6
I see, but is there any way I can tell a compiler that a function (or a whole translation unit) will never be called again so its memory can be reused for something else?

Name: Anonymous 2009-10-31 3:30

>>10
You'll have to consult your compiler's documentation for that.
Some compilers provide special declarations for such things, but first it might be better to just let the compiler perform register allocation by itself. What I said applies to variables, but you seem to mention functions. Functions are static pieces of code, and unless you're dynamically compiling function objects (for example Common Lisp has this, as well as some of the popular virtual machine based JIT engines do, but in less documented ways) and those function objects are treated as regular data that can be deallocated or garbage collected, then no. Unless you plan on allocating the code for your function dynamically and writing it there then freeing it, however for most normal C programs, code is allocated statically in the code section of your executable and stays there for the entire duration of the program. You could of course remove the code contained the function, but there is little use for doing that, as the space might not be easy to be reused for other things. The only reason people even do that is for crappy anti-reverse engineering tricks, which are way too futile.

Name: Anonymous 2009-10-31 3:33

>>10
What are you writing, and what are you writing it for.  You honestly should not be worrying about such things for 99% of programs.  You either have to be writing something that is very memory expensive, or on a arch that has a very small amount of memory and doesn't mind the overhead of dynamically allocating program memory.

Name: Anonymous 2009-10-31 4:31

>>7,11
register allocation bullshit
Is this even related to this thread or is it just your way of saying "I haven't read SICP"? Registers have no fucking relation to the discussion.

>>8
File-local variables (i.e. variables in the global scrope but declared static) and global variables are just stored as part of the executable which is mapped into the process's address space by the linker.
One of the less retarded replies in this thread.


OP, use locals instead of statics. Why would you even need statics in your settings code if you call it just once? If the problem is that multiple functions need to access the data, then make it a struct and pass it around via pointer. Then you can free it when you're done.

Why aren't your variables in get_settings() in the first place?

Name: Anonymous 2009-10-31 7:13

oh dear this is a digusting thread

Name: Anonymous 2009-10-31 13:36

>>13
Is this even related to this thread or is it just your way of saying "I haven't read SICP"? Registers have no fucking relation to the discussion.

Considering the question is about “freeing variables”, it's pretty fucking related that variables are likely not to exist anywhere that could even be freed.

Name: Anonymous 2009-10-31 13:40

>>15
IHBT

Name: Anonymous 2009-10-31 13:45

Jesus Fucking Christ, what a thread.

Name: Anonymous 2009-10-31 14:28

>>13
File-local variables (i.e. variables in the global scrope but declared static) and global variables are just stored as part of the executable which is mapped into the process's address space by the linker.
One of the less retarded replies in this thread.
This seems wrong. Why would they be stored in the executable if the values are not necessarily known at compile time? Not only that, but what if the size of the object changes, for example, if your global variable refers to a dynamic array?

Name: Anonymous 2009-10-31 14:36

>>18
WHERE DID YOU COME FROM STOP BEING A MORON IN MY PROG IHIHBT

Name: Anonymous 2009-10-31 14:41

How much you know about programming is inversely relational to how much you talk about programming on /prog/.  This thread is a GREAT example of that!

Name: Anonymous 2009-10-31 15:23

>>19
You could at least explain what's wrong with what I said in >>18. I don't have much experience in C, so if I'm wrong, I'd like to know why.

Name: Anonymous 2009-10-31 15:45

>>18
Please give a short code snippet that outlines a case that you don't understand. For the global variable referring to a dynamic array, I'm assuming you're talking about a global pointer that points to some malloced chunk of memory. In that case you're storing a single pointer in some uninitialized region (this is typically called the bss section) that is allocated (well, mapped in-- it'll likely be demand allocated) along with the rest of the bss section by the run-time linker on program load. Once your program starts running you set the value of the pointer with a pointer to a location in the heap given to you by malloc. Easy.

Name: Anonymous 2009-10-31 15:46

>>21
I don't have much experience in C
It shows

This seems wrong. Why would they be stored in the executable if the values are not necessarily known at compile time?
The value isn't stored in the executable, but space is reserved for the value.
if the size of the object changes
You recompile
if your global variable refers to a dynamic array?
The it would be a pointer which always talks 1 word of memory and the dynamic array would be on the heap


--
~This post has not been fact checked due to laziness, and if it's wrong then correct me and stop whining

1. The values aren't stored in the executable, but instead space would be reserved.
2. If a static object changed you would need to recompile it anyway
3.

Name: Anonymous 2009-10-31 15:46

>>21
>>18 is full of shit.  The values ARE known at compile time.  At least for static globals.

Name: Anonymous 2009-10-31 15:47

>>23
And apparently, I'm an idiot who forgot to delete half of his post, after rewriting it

Name: Anonymous 2009-10-31 16:04

>>23
Jeez, if you want to get more people visiting this board, you're going to have to change that attitude.

Name: Anonymous 2009-10-31 16:08

>>23
The value isn't stored in the executable, but space is reserved for the value.
It's stored if it has an initializer.

Name: Anonymous 2009-10-31 16:10

Isn't it inefficient in terms of space to store initialized static constants in the executable? They could potentially be quite large, and that extra space is basically wasted on every download of your executable file.

Name: Anonymous 2009-10-31 16:14

>>26
if you want to get more people visiting this board
That's a big if ;)
I'd settle for a 10% reduction in the number of visitors in a bid to rid us of the /pr/ quality threads

It's stored if it has an initializer.
Of course, but I took it that everyone would have known that. I guess I was a little naive

Name: Anonymous 2009-10-31 16:14

>>28
You store the information in program memory and not volatile memory.  That's the point.  You sacrifice executable size to decrease the amount of memory required at runtime.

IHBT,PICS101

Name: Anonymous 2009-10-31 17:09

>>28
What the hell are you talking about?  Storing literals takes less space than storing instructions to store literals at startup.

Name: Anonymous 2009-10-31 17:19

>>31
They end up to be the same thing.

static char[] hello = "Hello!\n"
...
char[] greetings = hello;

and

char[] greetings = "Hello!\n"

Will both allocate static memory for Hello and both reference it in the assignment.

Name: Anonymous 2009-10-31 17:25

>>32
1. How is that relevant to anything
2. What language is that
3. No

Name: Anonymous 2009-10-31 17:29

>>33
Back to /g/, please!

Name: Anonymous 2009-10-31 18:31

>>35
Please refrain from posting non-programming related material.

Thank you.

Name: Anonymous 2009-10-31 18:47

>>35
Wrong thread, bro

Name: Anonymous 2009-10-31 19:05

>>37
Women are always relevant!

Name: Anonymous 2009-10-31 20:46

I don't know if the right answer is here or not, I got too fed up reading bullshit to care?: there are no static variables on the stack. All static variables are on the heap and are memory resident for the life of the program. Some compilers may support hints to free the data, but it isn't standard C.

Name: Anonymous 2009-10-31 21:21

I've examined plenty of disassemblies of compiled C programs which show the same register used for the different variables in different places in the function.

._.

Name: Anonymous 2009-11-01 1:08

>>39
The GLOBL section is hardly the heap, bro.

Name: Anonymous 2009-11-01 2:47

>>39
They aren't on the heap.

Name: Anonymous 2009-11-01 3:09

I'm getting the feeling that WHBT

Name: Anonymous 2009-11-01 3:24

>>40
What don't you understand about that statement?

C:
{
  a = 1;
}
{
  b = 2;
}

//A fictional(a real compiler may remove (dead code elimination)that exact code since it isn't used) compiler may translate it to something like this:
mov edx,1 ; allocs edx to be used for a
mov edx,2 ; a is out of scope, allocs edx to be used for b

Name: Anonymous 2009-11-01 3:58

oops, forgot to declare the variables:
{int a; a = 1;} {int b; b = 1}

Name: Anonymous 2009-11-01 4:02

>>44
ur code tells me that your gay.

Name: Anonymous 2009-11-01 5:12

>>46
Well ur mr gay.

Name: Anonymous 2009-11-01 8:24

>>44
Can u explain further?  I don't understand how it is that the compiler is smart enough....

Another example would be good, perhaps a bit longer this time???

thx!

Name: Anonymous 2009-11-01 8:45

>>48
Smart enough? Let's say you have a pool of usable registers, for the variable `a', the compiler assigns one register (edx), and after `a' becomes out of scope, the register(edx) becomes free to be used for another variable, in which case it assigns it to b. This was just an example. Go read the dragon book or some other book on compiler design. Or if you can't be bothered to read some proper literature, http://en.wikipedia.org/wiki/Register_allocation

Name: Anonymous 2009-11-01 8:48

>>48
okay lesson one
in computer numbers are binary
so they are liek 010011101003010
or they are like adbagcbf then they are haxadecimal
you may also say 0xnumber to be hax
but they are harder so you must binary
so computer goes faster (no translate time)
you may also use other languages because
what i said is assembler what computer speaks you have to use assembler but can use other languages
like c (with the ++ extension maybe for serious programing) or pascal or javascript (java in short) or maybe haskell but i dont understnad it it has no loops so i dont konw how it works you cant code with no loops
get it so far?

Name: Anonymous 2009-11-01 10:55

>>49-50
YHBTFS

Name: Anonymous 2009-11-01 11:08

>>51
Not programming related.  Fuck you, you fucking normalfag piece of shit niggerfuck fucktard spammer.

Name: Anonymous 2009-11-01 11:11

>>49
Have any recommendations for reading on compiler design?

Name: Anonymous 2009-11-01 11:14

>>54
Is using google that hard?

Compilers: Principles, Techniques, and Tools
Modern Compiler Implementation in ML
Advanced Compiler Design and Implementation

Name: Anonymous 2009-11-01 11:19

How about just reading the Anonix C Compiler's source code?

Name: Anonymous 2009-11-01 12:22

COCK MY ASS

Name: Anonymous 2011-02-03 6:27

Name: Anonymous 2013-01-18 23:46

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List