memory

P

pnreddy1976

Hi friends...
I have very very basic question

my programe is like this.
#include <stdio.h>

int i=0,j=27,k;
void fun();
void main()
{
int a,b;
fun();
}

void fun()
{
int static x,y;
}


here i want to know that... how the memory is assigned to this program.
ie out of four memory segment(code,data,stack,extra) how the momory is
distributed for the programmes & variables
plz hellp me.
Thanking you
Reddy
 
R

Richard Heathfield

(e-mail address removed) said:
Hi friends...
I have very very basic question

my programe is like this.
#include <stdio.h>

int i=0,j=27,k;
void fun();
void main()

In C, main returns int.

When you've straightened that out in your head, maybe you'll be ready for
the next startling piece of news, which is...

here i want to know that... how the memory is assigned to this program.
ie out of four memory segment(code,data,stack,extra) how the momory is
distributed for the programmes & variables

....that C doesn't require your implementation to segment memory into code,
data, stack, and extra. In your program, i, j, and k have file scope,
external linkage, and static storage duration. The a and b objects have
block scope within the main function, and will be destroyed when control
flow passes through the end of the block in which they are defined. The x
and y objects have static storage duration, but are visible by name only
within the fun() function.
 
T

tomstdenis

Richard said:
...that C doesn't require your implementation to segment memory into code,
data, stack, and extra. In your program, i, j, and k have file scope,
external linkage, and static storage duration. The a and b objects have
block scope within the main function, and will be destroyed when control
flow passes through the end of the block in which they are defined. The x
and y objects have static storage duration, but are visible by name only
within the fun() function.

PSST. Homework....

Tom
 
C

Chris Dollin

Hi friends...
I have very very basic question

my programe is like this.
#include <stdio.h>

int i=0,j=27,k;
void fun();
void main()
{
int a,b;
fun();
}

void fun()
{
int static x,y;
}


here i want to know that... how the memory is assigned to this program.

Why do you care how memory is allocated in this completely pointless
code?

No point, no motivation, no reason to care: no understanding.
 
D

deepak

---------------------------------------------
| stack for main() |
| int a, b |
---------------------------------------------
| Stack for fun() |
| Empty |
----------------------------------------------
|Heap |
| Empty |
---------------------------------------------
|Initialized Data |
|i = 0, j = 27 |
---------------------------------------------
|Non-Initialized Data |
|Int x,y,k |
---------------------------------------------
|Text section |
---------------------------------------------

Readonly section is also there in data section.

One impotant thing to note is Stack will grow downwards and heap
upwards.
 
T

Tom St Denis

deepak said:
---------------------------------------------
| stack for main() |
| int a, b |
---------------------------------------------
| Stack for fun() |
| Empty |
----------------------------------------------
|Heap |
| Empty |

First off, use a fixed-pitch font for ASCII art, this diagram looks
really awful.

Second, that's not what the standard says and it ISN'T TRUE on many
platforms.

For example, for GBA development [e.g. systems where your program is
executed out of a ROM] all of the globals are in the BSS segment and
the initialized variables are copied from ROM at startup.

Not all platforms have to have a distinct heap and stack, etc, etc,
etc.

So before you go out and try to answer obvious homework questions why
not at least get your facts straight?

Tom
 
C

Chris Dollin

deepak said:
One impotant thing to note is Stack will grow downwards and heap
upwards.

Says /who/?

Really, if you don't know what you're talking about, don't talk about it.
 
C

CBFalconer

my programe is like this.
#include <stdio.h>

int i=0,j=27,k;
void fun();
void main()
{
int a,b;
fun();
}

void fun()
{
int static x,y;
}

here i want to know that... how the memory is assigned to this
program. ie out of four memory segment(code,data,stack,extra)
how the momory is distributed for the programmes & variables

In the first place your program is illegal. main returns int. In
the second your indentation is seriously flawed. Third, why do you
include stdio.h, when you don't use anything in it. Fourth, there
are no such things as code, data, stack, extra in C. Objects can
have static, automatic, or dynamic duration.

Your objects i, j, k, x, y are all static. a and b are automatic.
You have no objects with dynamic duration. Anything further is
between you and your implementor, and off-topic here.
 
D

deepak

Chris Dollin,

Richard Stevens tells it in his book 'Advanced Unix Programming'.
I just written it in a generic form.
I don't know how the alignemnt of diagram gone like this.
When i typed i aligned it properly.

Please go through the book and start argueing.
 
C

Chris Dollin

deepak wrote:

Oy. Please do me the kindness of not top-posting.

You had said:

I said:

You say:
Richard Stevens tells it in his book 'Advanced Unix Programming'.

Well, I suppose that is an answer to the question I asked. Let
me try again:

Does the Standard require that the stack grow down and the heap
grow up (whatever that means), so that a C programmer may rely on
it in their portable programs?

[Note that the book title offers an hint as to the implementation
independence of the C code within. I don't know if POSIX puts
requirements on stack/heap direction; I've never written code
that would want to know.]

Answer: not only does the Standard not require a particular growth
direction, it doesn't require that there be "a stack" and "a heap"
distinct from it. An implementation that had a stack and a heap
could grow them in whatever directions it found convenient; it
could also allocate call frames in the heap if it chose, in no
particular order.
 
R

Richard Heathfield

(e-mail address removed) said:
PSST. Homework....

Possibly. But if any teacher is clueless enough to set such a homework
question in the context of a C programming course, the student deserves at
least a chance at learning the truth.

If it /is/ homework, and if the OP is paying for the course, I recommend to
him that he seeks reimbursement and a competent teacher.
 
K

Keith Thompson

CBFalconer said:
In the first place your program is illegal. main returns int. In
the second your indentation is seriously flawed. Third, why do you
include stdio.h, when you don't use anything in it. Fourth, there
are no such things as code, data, stack, extra in C. Objects can
have static, automatic, or dynamic duration.

The standard uses the terms static, automatic, and allocated.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top