473,783 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stack space, global space, heap space

Greetings:

I know that variables declared on a stack definitely does not reside in heap
space so there is only a very limited amount of stuff that you can store in
the stack of a function. But what about the global space? Is that the same
as the heap space or is that still a form of special stack? Because once
I've seen someone declare a 1 megabyte char array in global space like this:

char s[1000000];

and it worked.
Regards,

Shuo Xiang
Nov 13 '05 #1
10 8348
Shuo Xiang wrote:
I know that variables declared on a stack definitely does not reside in
heap space
You can't declare variables "on a stack" in C. The best you can do is make
them [non-static] function-locals, which *allows* them to be implemented on
a stack but certainly doesn't require it. [They could, for example, be
implemented in a garbage-collected heap. This might be mad, but it is
possible.]
so there is only a very limited amount of stuff that you can
store in the stack of a function.
Why? Stacks can be as big as the implementation likes. Caution suggests we
don't overdo it, since we know some popular implementations are a little ...
weedy ... but (unless the Standard says otherwise) that's a property of
implementations , not of the language.
Because once I've seen someone declare a 1 megabyte char array in global
space like this:

char s[1000000];

and it worked.


Machine must have had enough room for it, then.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #2
Shuo Xiang wrote:

Greetings:

I know that variables declared on a stack definitely does not reside in heap
space so there is only a very limited amount of stuff that you can store in
the stack of a function. But what about the global space? Is that the same
as the heap space or is that still a form of special stack? Because once
I've seen someone declare a 1 megabyte char array in global space like this:

char s[1000000];

and it worked.


First, the C answer. C has no "stack," no "heap," and no
"global space." C supports data objects with three kinds of
"storage duration," namely, automatic, dynamic, and static.
The details of where these objects are created and how their
lifetimes are managed is entirely up to the implementation; the
C Standard specifies how the objects behave, but says nothing
about how the behavior is achieved. The implementation may (or
may not) impose different size limitations on objects of the
three different storage durations; again, the C Standard is
silent on what form such strictures might take.

Second, the practical answer. Many C implementations use
a stack for automatic-duration variables, and many implementations
impose stricter size limits for stack-resident objects than for
other kinds of objects. The details of these limits (including
whether and how they can be adjusted) are implementation-specific,
and can only be answered by consulting the implementation' s
documentation and/or gurus.

Finally, note that a C implementation is not required to be
able to support a one-megabyte array at all, regardless of what
storage duration is used. Many implementations do in fact handle
objects of this size and larger, but the Standard does not require
"minimalist " C to be able to do so.

--
Er*********@sun .com
Nov 13 '05 #3

"Chris Dollin" <ke**@hpl.hp.co m> wrote in message

Why? Stacks can be as big as the implementation likes. Caution suggests
we don't overdo it, since we know some popular implementations are a
little ... weedy ... but (unless the Standard says otherwise) that's a property of implementations , not of the language.

There are however good reasons for providing small stacks. Firstly stack
usage increases only as the logarithm of the size of a structured program.
Secondly, most processors implement cache schemes and if the stack is small
enough to ensure that it is always held in the cache, there is likely to be
a performance improvement. Thirdly, it is unusual to know the size of a big
data item at compile time, and pre 99 C doesn't allow variable size stack
arrays.
Nov 13 '05 #4
Shuo Xiang wrote:
I know that variables declared on a stack
You probably meant *automatic storage*.
definitely does not reside in heap space
You probably meant *free storage*.
so there is only a very limited amount of stuff
that you can store in the stack of a function.
You probably meant *local storage*.
But what about the global space?
Is that the same as the heap space?
or is that still a form of special stack? Because once
I've seen someone declare a 1 megabyte char array in global space
like this:

char s[1000000];

and it worked.
So does this:
cat stack.c int main(int argc, char* argv[]) {
const int n = 1000000;
unsigned char s[n];
for (int j = 0; j < n; ++j) {
s[j] = j%256;
}
return 0;
}
gcc -Wall -std=c99 -pedantic -O2 -o stack stack.c


Your program stack size can be set by the operating system
through a [UNIX] shell command (limit for [t]csh)
or a compile time option appropriate for your compiler.

In the *typical implementation* , virtual memory looks like

00000000 text segment
Nov 13 '05 #5
Malcolm wrote:

"Chris Dollin" <ke**@hpl.hp.co m> wrote in message

Why? Stacks can be as big as the implementation likes. Caution suggests
we don't overdo it, since we know some popular implementations are a
little ... weedy ... but (unless the Standard says otherwise) that's a

property
of implementations , not of the language.

There are however good reasons for providing small stacks. Firstly stack
usage increases only as the logarithm of the size of a structured program.
Secondly, most processors implement cache schemes and if the stack is small
enough to ensure that it is always held in the cache, there is likely to be
a performance improvement. Thirdly, it is unusual to know the size of a big
data item at compile time, and pre 99 C doesn't allow variable size stack
arrays.


Troll? Or merely tripe? None of the three points
seems to make any sense at all:

- "Stack grows as the logarithm of program size"
First, there's no hint of how "program size" is
to be measured. Lines of code? Value of some
fundamental parameter (e.g., number of items to
sort)? Either way, it's dead easy to point to
plenty of existing counterexamples .

Of course, the counterexamples might be dismissed
on the grounds of not being "structured ," but no
definition of "structured " is evident. Perhaps we
should say that "a structured program is one whose
stack size grows as the logarithm of its own size,"
but then the whole argument degenerates to tautology.

- "Stack is small so as to fit in a cache"
Balderdash. Cache friendliness (which involves far
more than mere size, by the way) is just as important
for code and for non-stack data as for stack-resident
data, so this argument leads to no special criterion
for stack size that wouldn't apply to everything else.

- "Stack is small because VLAs are new in C99"
Nonsense. VLAs have been with us for years and years,
albeit not in Standard C. But where did anybody get
the notion that machines and their operating systems
are designed solely with C in mind? Fortran, anyone?
Pascal? C with extensions like alloca()? This
argument isn't right; it's not even wrong.

Size limitations on stack-resident data (if either the stack
or limitations on it exist) are entirely implementation-dependent,
and are imposed (or not) for reasons the implementor considers
important. What those reasons might be are of no concern to the
C programmer, and the pseudo-reasons advanced above are simply
useless. Or worse.

--
Er*********@sun .com
Nov 13 '05 #6

"Eric Sosman" <Er*********@su n.com> wrote in message

- "Stack grows as the logarithm of program size"
First, there's no hint of how "program size" is
to be measured. Lines of code? Value of some
fundamental parameter (e.g., number of items to
sort)? Either way, it's dead easy to point to
plenty of existing counterexamples .

Of course, the counterexamples might be dismissed
on the grounds of not being "structured ," but no
definition of "structured " is evident. Perhaps we
should say that "a structured program is one whose
stack size grows as the logarithm of its own size,"
but then the whole argument degenerates to tautology.
A structured program consists of a roughly balanced call tree of functions.
As it grows, the tree becomes deeper rather than the functions growing more
complicated.
It follows that such a program will use stack space proportionate to the
logarithm of its size (number of lines of code).

- "Stack is small so as to fit in a cache"
Balderdash. Cache friendliness (which involves far
more than mere size, by the way) is just as important
for code and for non-stack data as for stack-resident
data, so this argument leads to no special criterion
for stack size that wouldn't apply to everything else.
No it isn't, because the stack data tends to be used more intensively.
Take a totally typical line of code

for(i=0;i<N;i++ )
employee[i].wage += 1000.0;

The array employee is probably on the heap. If N is large and the cache
small it is probably inevitable that there will be some misses. However if i
and N aren't in the cache, then we are really in trouble.
- "Stack is small because VLAs are new in C99"
Nonsense. VLAs have been with us for years and years,
albeit not in Standard C. But where did anybody get
the notion that machines and their operating systems
are designed solely with C in mind? Fortran, anyone?
Pascal? C with extensions like alloca()? This
argument isn't right; it's not even wrong.
The stack is something imposed by the compiler, not the OS. An OS might make
it easy and natural to implement, say, an 8K stack using a special register,
but if you want to implement a larger stack you can do so.
Size limitations on stack-resident data (if either the stack
or limitations on it exist) are entirely implementation-dependent,
and are imposed (or not) for reasons the implementor considers
important. What those reasons might be are of no concern to the
C programmer, and the pseudo-reasons advanced above are simply
useless. Or worse.

If you understand why the stack might be small, you will remember that
stacks are small more easily. Also, when a technological change comes along
and makes those reasons irrelevant, you will find it easier to adapt to the
new environment, because you have some idea what is going on rather than
just parrotting "stack size is implementation dependent and often small".
Nov 13 '05 #7
"Malcolm" <ma*****@55bank .freeserve.co.u k> writes:
Take a totally typical line of code

for(i=0;i<N;i++ )
employee[i].wage += 1000.0;

The array employee is probably on the heap. If N is large and the cache
small it is probably inevitable that there will be some misses. However if i
and N aren't in the cache, then we are really in trouble.


Surely even if i and N are not in the cache at first, they will be as
soon as the loop is started. Since N is large, the delay caused by one
cache miss would be insignificant, wouldn't it?

--

John Devereux
Nov 13 '05 #8

"John Devereux" <jd*@devereux.m e.uk> wrote in message

Surely even if i and N are not in the cache at first, they will be as
soon as the loop is started. Since N is large, the delay caused by one
cache miss would be insignificant, wouldn't it?

Generally, yes, though not on some platforms (the Sony Playstation has a
special reserved area of memory that has fast access, for example, variables
aren't promted to it on read/write).

The point is that it is important for stack variables to be in the cache - i
N and the pointer "employee" are all on the stack and are accessed on every
cycle, whilst the "wage" variables are each accessed only once.

It would be a pretty puny cache that failed to keep all three variables in
cache, however they were distributed in memory. However make the loop more
complicated, maybe calling a subroutine to calculate the wage increase
rather than giving 1000.0 to everybody, and you could soon get a situation
where i, N, and employee are in the cache when they are close together in
memory, but not kept in when they are widely separated.
Nov 13 '05 #9
"Malcolm" <ma*****@55bank .freeserve.co.u k> writes:
"John Devereux" <jd*@devereux.m e.uk> wrote in message

Surely even if i and N are not in the cache at first, they will be as
soon as the loop is started. Since N is large, the delay caused by one
cache miss would be insignificant, wouldn't it?
Generally, yes, though not on some platforms (the Sony Playstation has a
special reserved area of memory that has fast access, for example, variables
aren't promted to it on read/write).


OK, although I would call that "special reserved area of memory that
has fast access"; it's not really a cache as I understand the term.
You would indeed probably put the stack there...
The point is that it is important for stack variables to be in the cache - i
N and the pointer "employee" are all on the stack and are accessed on every
cycle, whilst the "wage" variables are each accessed only once.

It would be a pretty puny cache that failed to keep all three variables in
cache, however they were distributed in memory. However make the loop more
complicated, maybe calling a subroutine to calculate the wage increase
rather than giving 1000.0 to everybody, and you could soon get a situation
where i, N, and employee are in the cache when they are close together in
memory, but not kept in when they are widely separated.


--

John Devereux
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
30099
by: Kevin Grigorenko | last post by:
Hello, I couldn't find an obvious answer to this in the FAQ. My basic question, is: Is there any difference in allocating on the heap versus the stack? If heap or stack implementation is not part of the standard, then just disregard this question. Here's some questions I'm confused about, and if you can add anything else, please do so! Is the stack limited for each program?
17
5052
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
20
3504
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate "heap" or "stack" I'm curious to know the implemenations which dont have stack or heap.
1
3768
by: Geiregat Jonas | last post by:
I'm reading Eric Gunnerson's book. He is talking about the heap and stack, he says you have 2types, value wich are in the stack or inline or reference types wich are in the heap. I don't get this what's heap stack and what's the main difference between those 2types ?
3
1712
by: nahur | last post by:
why do you need a heap and a stack why not all memory called a heap or call it a stack what is the purpose of having a heap and a stack
9
3385
by: shine | last post by:
what is the difference between a heap and a stack?
9
7331
by: Ajay | last post by:
Hi all, Can I know what is the stack space and heap space allocated by the compiler.Can i increase it or decrease it.if yes,pleae tell me theway to do it.Thanks in advance. Cheers, Ajay
16
4451
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
9
2545
by: coder_lol | last post by:
Thanks everybody for helping me with the Syntax confusion! The implicit conversion stuff really got me :) I have one more question... Array<int32ia; Does the above use the default constructor and get me an Array<int32> with a size 0? The memory used is the stack, right? ia = Array<int32>(10);
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.