Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Doubt on Stack variables.

Reply
Thread Tools

Doubt on Stack variables.

 
 
Keith Thompson
Guest
Posts: n/a
 
      01-15-2007
"David T. Ashley" <(E-Mail Removed)> writes:
> "deepak" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> 1) Will it allocate memory for all the char's together or allocate for
>> first char. then for int then for float and after
>> this only second char in the function gets memory.

>
> Generally, 'C' only guarantees that a given symbol (a varialble or an array)
> is allocated contiguously. There are no guarantees about how different
> symbols are allocated with respect to each other. One cannot make
> assumptions about the address relationship between ib and ic, for example.
>
> As other posters have pointed out, the notion of a "stack" does not appear
> in the standards. This is probably true. In fact, some small processor
> compilers have memory models where they don't use a stack. Instead, the
> compiler analyzes the calling tree from top to bottom and puts automatics
> into a static area of memory that is shared with all functions at the same
> level of the calling tree. (This happens with the understanding that
> recursion and corecursion are disallowed.) This is attractive in small
> machines because instructions to reference memory with respect to the stack
> pointer are very expensive, and one gets smaller code size by statically
> allocating automatic variables.


A conforming implementation must support recursion; it's not optional.
A compiler can allocate local (block-scope) variables statically *if*
it can prove that there will be no recursive calls to that function
(and it presumably will do so only if there's some advantage). A
compiler can have an option that forbids recursion, but it will be
non-conforming in that mode; it might nevertheless be useful.

Assuming support for recursion, an implementation *must* allocate
local variables in a stack-like fashion (last in first out). But the
term "stack", and particularly the phrase "the stack", usually refers
to a *contiguous* region of memory indexed by a "stack pointer" which
is typically a CPU register. The kind of "stack" is very common, but
it's no required by the standard. Someone here has mentioned an IBM
mainframe operating system in which the space for a called function is
allocated as if by calling malloc().

[...]

> For a typical large machine, 1000 small variables or an array of
> 1000 elements on the stack in a single function would probably be
> tolerated. At compilation, the processor has instructions to
> address memory in the stack frame with very large offsets, and
> usually the only limit from the compiler's point of view is whether
> it can choose the machine instructions it wants/needs to get to the
> data. At runtime, typically the stack is managed via the
> traditional virtual memory mechanisms. I doubt that a practical
> program (one designed to do a computing job rather than to test the
> stack limits) would run into these limits.


A recursive program can easily exceed any finite stack size or other
memory limit, either due to a logical error or because it's asked to
process a large amount of input data. And multi-user operating
systems typically impose memory limits on each process that are much
smaller than the total amount of memory (virtual or otherwise)
available on the system.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      01-16-2007
Keith Thompson wrote:
> ...
> A conforming implementation must support recursion; it's not optional.


No, it's a QoI issue.

The implementation shall be able to translate and execute at least
one
program that contains at least one instance of every one of the
following
limits:
...
- 127 arguments in one function call

One function call does not recursion make.

--
Peter

 
Reply With Quote
 
 
 
 
Peter Nilsson
Guest
Posts: n/a
 
      01-16-2007
David T. Ashley wrote:
> ...
> As other posters have pointed out, the notion of a "stack" does not
> appear in the standards.


No, the word stack does not appear, but the LIFO nature of function
calling certainly does.

--
Peter

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-16-2007
"Peter Nilsson" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> ...
>> A conforming implementation must support recursion; it's not optional.

>
> No, it's a QoI issue.
>
> The implementation shall be able to translate and execute at least
> one program that contains at least one instance of every one of
> the following limits:
> ...
> - 127 arguments in one function call
>
> One function call does not recursion make.


Well, yeah, but I think it stretches the point. C99 6.5.2.2p11 says:

Recursive function calls shall be permitted, both directly and
indirectly through any chain of other functions.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
stack frame size on linux/solaris of a running application stack Surinder Singh C Programming 1 12-20-2007 01:16 PM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM



Advertisments