Hi All,
Please let me know,
For a C program, where the statements and variables are stored in memory (heap, stack) and which segment of memory?
any link for memory allocation for C programs.......
Thanks in advance.
Bhupesh
Hi All,
Please let me know,
For a C program, where the statements and variables are stored in memory (heap, stack) and which segment of memory?
any link for memory allocation for C programs.......
Thanks in advance.
Bhupesh
Your question sounds like part of an interview or school question.
If you can pose a more precise question, I'd be happy to answer you question, but as it stands, your question is a bit vague.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I too have no clue what the question is, but here's a guess at the answer:
Use malloc() to allocate memory on the heap:
Normal (non-pointer) variables are allocated on the stack:Code:char* str = malloc( 80 * sizeof( char ) );
Code:char str[80];
Hi
Thanx for the reply, actually what i m asking is,
I am asking about the memory segment, as i read earlier there are four memory segments (Code segment, Data segment, Stack segment, Extra segment).
I know data with the melloc, calloc, alloc, takes memory in heap segment.
while the data arguments used in function call are stored in stack segment.
Suppose we write a C program,
like,
Code:main() { int a = 9, b = 10; int *c; c = &a; //already defined some computational function compute(a, b) }
Where this all statement and data will be stored? its an interview question only...
As i know statements will go to the code segment.
if we allocate memory to a data, will go to the heap(extra).
function been called will go to the stack segment (in stack segment, here the arguments and other variables will b stored here.)
Please clear me these things........
Thanx
----------------------------
bhupesh
The variables a, b and c would be stored on stack.Code:int a = 9, b = 10; int *c; c = &a;
And the values 9 and 10 would be saved along with the code ( in text segment ).
Note that ALL of this is completely and utterly meaningless if you are using a modern 32-bit compiler, such as gcc or MS Visual Studio. Using a "DOS" compiler these days is pretty bad.
The "segments" you are talking of are the processor segments in x86 real-mode. In this case, code (that is your "statements of C") end up in the Code-segment, the Data segment contains your global variables, and the heap, Stack is used for the call-stack and local variables.
Note however that the segments are not entirely static. There may for example be several sections of code, in different segments. The processor uses the code-segment to point to the current section of code, but it can change from one section to another by what's called a "Far call" or "far return" - "far" here means that not only the instruction pointer, but also the code-segment is changed.
Likewise, the data-segment can "move around", such that the data-segment register doesn't always point to the same place. If you use the heap, then DS will (for a time) point to some heap location.
ES is uses, as the name implies, as an "extra" segment, when for example copying from one data segment to another [typically from the heap to a global data variable, or the other way around]. Functions like memcpy() are often implemented as "rep movs", which implicitly takes DS as the source segment and ES as the destination segment. Modern x86 processors [286 and later] have two more "extra" segments, called FS and GS.
Have a look here for a further explanation of C and Memory Models in real-mode
http://en.wikipedia.org/wiki/C_memory_model
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
> I am asking about the memory segment, as i read earlier there are four memory segments
> (Code segment, Data segment, Stack segment, Extra segment).
That's a very implementation specific view of what C uses.
C doesn't even assume a stack, only that there is a mechanism for managing dynamic scope, of which a stack is a common implementation choice.
Also, I don't think there is any distinction in the standard between what would be code and data memory.
Historically, the original C compilers used sections called .text, .data and .bss (program, initialised data, uninitialised data respectively). With C89, .rodata (for constants) became common as well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.