Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Memories

Reply
Thread Tools

Memories

 
 
Russell Shaw
Guest
Posts: n/a
 
      06-10-2005
Keith Thompson wrote:
> http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
>
>>Dear All.
>>
>>As far as i know that Then memeory is divided into the three segements
>>
>>1. Heap;
>>2. Stack;
>>3. Data segmnet;

>
>
> That may be true in some implementations, but none of these are C
> terms. In some implementations, malloc() and friends allocate memory
> from the "heap", local variables are on the "stack", and static and
> global variables are in the "data segment" -- but others may do things
> differently.
>
> [...]

....

> There's no good way in C to determine how much memory is available
> before doing a function call. Just avoid infinite recursion and hope
> there's enough space.


size_t freespace = alloca(0) - sbrk(0) + 1;
 
Reply With Quote
 
 
 
 
Class that cannot be inherited ... C++
Guest
Posts: n/a
 
      06-10-2005


Keith Thompson wrote:
> (E-Mail Removed) writes:
> > Dear All.
> >
> > As far as i know that Then memeory is divided into the three segements
> >
> > 1. Heap;
> > 2. Stack;
> > 3. Data segmnet;

>
> That may be true in some implementations, but none of these are C
> terms. In some implementations, malloc() and friends allocate memory
> from the "heap", local variables are on the "stack", and static and
> global variables are in the "data segment" -- but others may do things
> differently.
>

hi as u r writing that static and global will be in data segment , data
segment is the part which is copied if an application is all ready in
memory .
eg. if internate exploreer is allready open and we have clicked it once
more a new code copy will not be there only the data segment part will
be cpoied in memory and same codesegment will be used .
is it correct? it is .

now
i have program a application in which i have use a global static
variable.
if the data segment is holding the static and global variables so that
segment should be copied to other segment (as a new instance of that
process play) but what is happening that global static variable is
incrementing for the all three instance of that process are running.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      06-10-2005
(E-Mail Removed) writes:
> Keith Thompson wrote:

[...]
>> #include <stdio.h> /* necessary for printf */
>> int main(void)
>> {
>> printf("Hi\n");
>> return main();
>> }
>>
>> If the compiler performs tail-recursion optimization, transforming the
>> recursive call to a loop, this will just print "Hi" forever with no
>> stack overflow.

>
> I just checked your above code... but still it says the stack over
> flow,


Read what I wrote above again. Apparently your compiler doesn't
perform the optimization.

--
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
 
Keith Thompson
Guest
Posts: n/a
 
      06-10-2005
Russell Shaw <rjshawN_o@s_pam.netspace.net.au> writes:
> Keith Thompson wrote:

[...]
>> There's no good way in C to determine how much memory is available
>> before doing a function call. Just avoid infinite recursion and hope
>> there's enough space.

>
> size_t freespace = alloca(0) - sbrk(0) + 1;


The word "good" was intended to exclude non-portable code.

--
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
 
CBFalconer
Guest
Posts: n/a
 
      06-10-2005
Russell Shaw wrote:
> Keith Thompson wrote:
>

.... snip ...
>
>> There's no good way in C to determine how much memory is available
>> before doing a function call. Just avoid infinite recursion and
>> hope there's enough space.

>
> size_t freespace = alloca(0) - sbrk(0) + 1;


None of those calls are portable, and even if they exist for their
usual (non-standardized) purposes there is no guarantee they
cooperate in that manner. Please don't post system specific code
without plainly identifying it as such. Some innocent might take
you seriously.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      06-10-2005
CBFalconer wrote:

> Please don't post system specific code without plainly identifying it
> as such. Some innocent might take you seriously.


Think of the children!


 
Reply With Quote
 
Gordon Burditt
Guest
Posts: n/a
 
      06-10-2005
>As far as i know that Then memeory is divided into the three segements
>
>1. Heap;
>2. Stack;
>3. Data segmnet;


Memory is divided into 4 segments, not usually all found
in the same machine at the same time:
1. Leased
2. Mortgaged
3. Fully Paid For
4. Stolen

Gordon L. Burditt
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-10-2005
"Class that cannot be inherited ... C++" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> (E-Mail Removed) writes:
>> > Dear All.
>> >
>> > As far as i know that Then memeory is divided into the three segements
>> >
>> > 1. Heap;
>> > 2. Stack;
>> > 3. Data segmnet;

>>
>> That may be true in some implementations, but none of these are C
>> terms. In some implementations, malloc() and friends allocate memory
>> from the "heap", local variables are on the "stack", and static and
>> global variables are in the "data segment" -- but others may do things
>> differently.
>>

> hi as u r writing that static and global will be in data segment , data
> segment is the part which is copied if an application is all ready in
> memory .
> eg. if internate exploreer is allready open and we have clicked it once
> more a new code copy will not be there only the data segment part will
> be cpoied in memory and same codesegment will be used .
> is it correct? it is .


Please don't use abbreviations like "u" and "r"; take the time to
spell out the words. Thank you.

> now
> i have program a application in which i have use a global static
> variable.
> if the data segment is holding the static and global variables so that
> segment should be copied to other segment (as a new instance of that
> process play) but what is happening that global static variable is
> incrementing for the all three instance of that process are running.


The behavior you're describing is system-specific. The C standard
doesn't even address the possibility of more than one program running
simultaneously.

If multiple copies of the same program are running simultaneously,
presumably they're not going to interfere with each other's variables
(if they did, they wouldn't be behaving in accordance with the
language standard). How this is done is up to the implementation.

--
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
 
Malcolm
Guest
Posts: n/a
 
      06-10-2005

<(E-Mail Removed)> wrote
>
> I will really do the spell check on my statements, So that it may
> not be the concern for others in near future, I will really
> follow your advice, Presently, I just want to know about the memories
> may be off topic to you, but really i want to know how my code
> behaves. (To Try To Catch The Flow)
>

You need to think of C as a portable assembler that runs on a virtual
machine. However you don't know exactly how this virtual machine is
designed. You know it accesses memory through addresses, but you don't know
how many bits it takes to hold an address. You know it has at least a few
kilobytes of memory installed, but you don't know exactly how much, and it
might be several terabytes. You know that all the memory is identical to
you, the C programmer, though physically it may be held on different chips
with different access speeds.

So you can indeed think of this conceptual machine as having a "stack", a
"heap" and a "data segment". You might also want to divide the "data
segment" into read-only and read/write memory.

Local variables go on the stack. When a function returns, the local
variables are removed from the stack. When another function is called, that
space is reused for the new function to work in.
malloc() allocates memory from the heap. Once memory is taken from the heap,
it is marked as "used" in some way, and is yours to do what you want with.
When you call free(0 the meory is returned to the heap and might be used by
the next call to malloc(), or maybe by a different program running on the
same machine.
The data segment is a fixed-size chunk of memory that hold all the globals
and static local variables. It persists throughout function calls, and
unlike memory from the heap its size is fixed for the life of the program.

However it is important to realise that this virtual machine is only a rough
representation of reality. Some posters here might say that it is such a
rough representation that in fact it is not even an aid to understanding.


 
Reply With Quote
 
Paul Mesken
Guest
Posts: n/a
 
      06-10-2005
On 9 Jun 2005 22:32:44 -0700, (E-Mail Removed) wrote:

>CBFalconer wrote:


>> You need both a spelling checker and to read the C standard. When
>> you do, please tell us where you find heap, stack, or data segment
>> mentioned.


<snip>

>I am really sorry my native is not english but nevertheless I am
>trying my level best to present and put my querries at the best
>in English


Don't worry about your English Ranjeet. Some might make an issue out
of it but it's good enough for the (tolerant) majority of us. English
is not my native language either and there are many more like that
here.

Besides : I don't even believe that English is required to post to
this newsgroup (although it will maximize the response, of course

 
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
How FB-DIMM Memories Work Silverstrand Front Page News 0 12-26-2005 04:10 PM
modelsim - looking at memories fpgawizz VHDL 2 04-04-2005 12:07 AM
passed 70-229, done MCSD.NET, thanks for the memories General Protection Fault MCSD 5 12-19-2003 06:26 PM
Looking for help to recover memories... Patrick Computer Support 1 08-20-2003 04:30 PM
Re: Need help recovering lost memories from dead Hard Drive :( Cuanto Computer Support 0 08-19-2003 05:59 PM



Advertisments