Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 30th, 2005, 12:16 PM   #11
aloksave
Programmer
 
aloksave's Avatar
 
Join Date: Sep 2005
Posts: 33
Rep Power: 0 aloksave is on a distinguished road
Quote:
Originally Posted by Ringo
What development environment are you talking about dude ?

Anyway, i may have been incorrect - not completely, but a bit:

A compiler can allocate variables to the stack or to a register (if it's only used locally) - that is the compilers choice.

I am used to programming in an environment where memory is expensive (not so true now but where physical size is an issue (ie cell phones) its still holds) and efficient code is paramount - embedded software or thereabouts. Anyway, i wouldn't really be very happy if a compiler decided to put a temp variable on the stack - thats what registers are for. HOWEVER, i have no experience in other environments - so your words may be correct for your circumstances.

Just be careful - "All variables created in C are created on the stack". It's nothing to do with C - its to do with the compiler.

As i said before, sorry for hogging your thread and sorry for not providing an answer to your original question.
Hey Ringo thanks for the correction, yes i messed out in that post..
as you pointed out compiler will decide whether to allocate the variable on a stack or register( this is for optimization?? right...i mean fetching from registers is much faster than from memory so is it? )..we can declare variables as having register storage type which suggests the compiler to store variable in a register since it will be used more frequently,but its mere suggestion compiler decides on it..
but i do not understand how compiler will decide on whether to store in stack or in register..i mean what are criterions...can you elaborate on that..I will be very grateful if you do.
Thanks in advance
aloksave is offline   Reply With Quote
Old Sep 30th, 2005, 4:19 PM   #12
Ringo
Newbie
 
Join Date: Sep 2005
Location: Cambridge, UK
Posts: 13
Rep Power: 0 Ringo is on a distinguished road
Here's a list ripped from: http://www.ece.utexas.edu/~valvano/assmbly/local.htm

Reasons why we place local variables on the stack include:

- dynamic allocation/release allows for reuse of memory
- limited scope of access provides for data protection
- only the program that created the local variable can access it
- since an interrupt will save registers and create its own stack frame, the code can be made reentrant.
- since absolute addressing is not used, the code is relocatable
- the number of variables is only limited by the size of the stack allocation
that can be much larger than using local variables in registers

Draw your own conclusions from that!
Ringo is offline   Reply With Quote
Old Sep 30th, 2005, 4:26 PM   #13
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
The C standard from the getgo was to allocate auto variables in user stack space.
COBOL, FORTRAN and other languages didn't do that when C was originally designed.
It's part of the reason that native C code is faster and less memory intense than FORTRAN and COBOL were.

I learned C in 1978 on a Digitial PDP-8 with a 16K core. You had to mind your p's and q's memory-wise, so functions like alloca() really helped in memory management.

alloca - like malloc but on the stack of the calling functions, freed when the function reutrns.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:34 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC