473,566 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Heap Vs Stack

I would like to know which is dynamic in nature.
if i refer the C memory model (Richard Steven), it is shown that both
stack and heap grow towards each other.
Now, can one go into other's area and hence effecting the size of
other memory area.

Does any limit exist upto which a stack or a heap can grow. and if it
is there then who decides the limit?

plz clarify.

Jul 8 '07 #1
11 2902

Nehil wrote:
I would like to know which is dynamic in nature.
if i refer the C memory model (Richard Steven), it is shown that both
stack and heap grow towards each other.
Now, can one go into other's area and hence effecting the size of
other memory area.

Does any limit exist upto which a stack or a heap can grow. and if it
is there then who decides the limit?

plz clarify.
The C Standard does not even define the terms "stack" and "heap",
though they're common in most computing architectures. This group only
discusses C as defined by it's Standard and hence your questions are
not topical.

Having said that, the nature of the stack and the heap, if one or both
do exist, usually have more to do with the system's hardware
architecture and it's operating system than a normal C program. So
possible answers to your questions are dependent upon the details of
your system, it's system software and it's C implementation. Post to
an appropriate group, (for example a Windows or Unix programming
group).

Jul 8 '07 #2
In article <11************ *********@q75g2 000hsh.googlegr oups.com>,
Nehil <ne***********@ gmail.comwrote:
>I would like to know which is dynamic in nature.
if i refer the C memory model (Richard Steven), it is shown that both
stack and heap grow towards each other.
Now, can one go into other's area and hence effecting the size of
other memory area.

Does any limit exist upto which a stack or a heap can grow. and if it
is there then who decides the limit?

plz clarify.
http://groups.google.com/groups?q=st...up:comp.lang.c
dave

--
Dave Vandervies dj******@csclub .uwaterloo.ca
A truly intelligent animal would try to act as if it were not intelligent,
at least when around humans.
--Richard Heathfield in comp.programmin g
Jul 8 '07 #3
To all :
Thanks a lot for ur answers.

=============== =============== =============== =============== =============== =============== =========

Well, i understand that standards say nothing about these two terms
and they are implementation specific.

may i know who implements the C standard : is it compiler, linker,
loader, OS or some combination of these with some collaboration.

plz look at the following program :

int i=0;
int main(void)
{
i++;
printf("%d\n",i );
main();
return 0;
}

each time the the program is executed, with same binary/exe or
different (i.e. by compiling again) the value of i is different.
on what factors the value of i last printed, depends upon.
plea

please clarify.

=============== =============== =============== =============== =============== =============== =========

Jul 8 '07 #4
santosh wrote:
Nehil wrote:
<snip>
plz look at the following program :

int i=0;
int main(void)
{
i++;
printf("%d\n",i );
main();
return 0;
}

each time the the program is executed, with same binary/exe or
different (i.e. by compiling again) the value of i is different.

That shouldn't be so.

The above program will recursively call the main function forever,
since there's no termination condition. In practise it's likely to run
out of memory and get terminated.
Of course, the program as presented by Nehil will invoke undefined
behaviour. To make it defined stdio.h has to be included.

Jul 8 '07 #5
On Sun, 08 Jul 2007 13:44:22 -0000, in comp.lang.c , Nehil
<ne***********@ gmail.comwrote:
>may i know who implements the C standard : is it compiler, linker,
loader, OS or some combination of these with some collaboration.
All of them, probably.
>plz look at the following program :

int i=0;
int main(void)
{
i++;
printf("%d\n", i);
main();
return 0;
}

each time the the program is executed, with same binary/exe or
different (i.e. by compiling again) the value of i is different.
Because you have forgotten to #include stdio.h, and so the behaviour
is undefined. include the header and try again.

Note that the programme will continue indefinitely, or until you run
out of memory.

--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Jul 8 '07 #6

"Nehil" <ne***********@ gmail.comwrote in message
news:11******** *************@q 75g2000hsh.goog legroups.com...
>I would like to know which is dynamic in nature.
if i refer the C memory model (Richard Steven), it is shown that both
stack and heap grow towards each other.
Now, can one go into other's area and hence effecting the size of
other memory area.

Does any limit exist upto which a stack or a heap can grow. and if it
is there then who decides the limit?
Assume a typical structured C program consisting of subroutines that call
each other and pass each other data. As the program grows in size and
complexity, its stack usage will tend to grow as the logarithm of the number
of functions it contains. It's heap usage, however, will grow linearly with
the amount of data it processes. That is beacuse stack variables are thrown
away when the function exits, heap allocations typically are not.

Because of the law of logarithms, it follows that you only need a relatively
small stack, even for a huge program, as long as we impose the rule that,
say, no one stack item may be more then 1K in size. On the other hand you
can easily gobble many megabytes of heap space. Another factor is that, if
the data item is huge, such as a 24-bit rgba image, typically it doesn't
make much sense to hardcode the dimensions at compile time. If it is small,
like a chess board, often hardocidng size will make sense. C99 allows
variable-sized objects on the stack, but generally this is considered to be
an undesireable feature, becasue it makes it too easy to run yourself out of
stack space as user enters a maliciously long word.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jul 8 '07 #7
On Jul 8, 6:44 am, Nehil <nehilparas...@ gmail.comwrote:
>
Well, i understand that standards say nothing about these two terms
and they are implementation specific.

may i know who implements the C standard : is it compiler, linker,
loader, OS or some combination of these with some collaboration.
Usually a combination of all (in some sense) with some collaboration.
For example, an implementation will often rely on the linker and the
loader to ensure that variables which need to be initialized to all-
bits-zero are initialized correctly when the program starts. The
loader will often use a facility from the OS to get a chunk of memory
filled with zero bits.

Jul 8 '07 #8
Mark McIntyre <ma**********@s pamcop.netwrite s:
On Sun, 08 Jul 2007 13:44:22 -0000, in comp.lang.c , Nehil
<ne***********@ gmail.comwrote:
>>may i know who implements the C standard : is it compiler, linker,
loader, OS or some combination of these with some collaboration.

All of them, probably.
>>plz look at the following program :

int i=0;
int main(void)
{
i++;
printf("%d\n" ,i);
main();
return 0;
}

each time the the program is executed, with same binary/exe or
different (i.e. by compiling again) the value of i is different.

Because you have forgotten to #include stdio.h, and so the behaviour
is undefined. include the header and try again.
It's true that calling printf without a '#include <stdio.h>' (or a
declaration for printf) invokes undefined behavior, but in practice
it's highly unlikely that that explains the behavior he's seeing.
Note that the programme will continue indefinitely, or until you run
out of memory.
Assuming the '#include <stdio.h>' is added, there are three
possibilities I can think of:

1. The bottomless recursion will cause the program to run out of
memory after some number of calls. It will print the value of i
before each call; the last value printed will *probably* give you an
idea of how many calls occurred. However, running out of memory
invokes undefined behavior (terminating the program is common, but not
guaranteed), so there's no way to be certain. If you see different
numbers on successive invocations, it can be for any number of
reasons. The program may have different amounts of memory available
for whatever reason, or different amounts of output may have been
buffered but not yet displayed when the program crashed. (Unless you
call fflush(stdout) after each printf, you may not see all the
output.)

2. 'i' could reach INT_MAX before the program runs out of memory. At
this point, 'i++' invokes undefined behavior. Wrapping around to
INT_MIN is a common result, but it's not guaranteed. If it does
simply wrap around, the program will continue running until it runs
out of memory; see above.

2. The compiler may be smart enough to recognize the tail recursion,
transforming the recursive call into an infinite loop that doesn't
consume extra memory on each call. When 'i' reaches INT_MAX, the next
'i++' will invoke undefined behavior; see above. If the program
continues after this, it will probably keep running until you kill it.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 9 '07 #9
"Malcolm McLean" <re*******@btin ternet.comwrite s:
"Nehil" <ne***********@ gmail.comwrote in message
news:11******** *************@q 75g2000hsh.goog legroups.com...
>>I would like to know which is dynamic in nature.
if i refer the C memory model (Richard Steven), it is shown that both
stack and heap grow towards each other.
Now, can one go into other's area and hence effecting the size of
other memory area.

Does any limit exist upto which a stack or a heap can grow. and if it
is there then who decides the limit?
Assume a typical structured C program consisting of subroutines that
call each other and pass each other data. As the program grows in size
and complexity, its stack usage will tend to grow as the logarithm of
the number of functions it contains. It's heap usage, however, will
grow linearly with the amount of data it processes. That is beacuse
stack variables are thrown away when the function exits, heap
allocations typically are not.
[...]

You're assuming no recursion. Recursion is common in programs that
process recursively-defined data; the more complex the input data, the
deeper the nested function calls. A compiler with a recursive descent
parser is a good example of this; a program that processes XML is
probably another.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 9 '07 #10

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

Similar topics

14
30075
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! ...
17
5012
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; };
1
3759
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 ?
2
2847
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is allocated on the stack since it's a value type variable - variable d is allocated on the stack since it's a value type variable Where does variable "c"...
3
1703
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
3363
by: shine | last post by:
what is the difference between a heap and a stack?
24
2846
by: arcticool | last post by:
I had an interview today and I got destroyed :( The question was why have a stack and a heap? I could answer all the practical stuff like value types live on the stack, enums are on the stack, as are structs, where classes are on the heap... when value types go out of scope the memory is re- allocated, object remain in memory waiting to be...
16
4431
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...
53
26344
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
9
3149
by: Roman Mashak | last post by:
Hello, I'm confused about heap and stack memories management in C language. Most books explain that local stack variables for each function are automatically allocated when function starts and deallocated when it exits. In contrast, malloc() always takes memory in the heap. Now, let's consider the code as follows: int get_buffer() {
0
7584
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...
0
8109
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...
1
7645
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...
0
6263
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...
1
5485
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2085
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1202
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.