vikram said:
i have series of questions
1.How a c program is loaded in memory
i mean the whats is the structure that the code segment?? data segment??
Some implementations break apart programs into different segments.
For example, a code segment is where the executable instructions are
placed. A data segment may contain the variables. Some implementations
go further and have Zero-Initialized, Read-Only and overlay segments.
Simple implementations just place everything in one segment. Read
your compiler documentation.
The actual loading of programs "into memory" is a property of the
operating system, if there is one. Not all platforms have operating
systems (OSes). Also, not all programs are "loaded into memory".
Many programs, including the one I'm working on now, reside in
"Read Only Memory {ROM}" and live there for the life of the product.
Research about operating systems and the one(s) you use.
2.When you say
const int *p;
where is p stored in the memory?? what happens internal so that its a read only.
The variable is stored in memory. Only the compiler or operating
system know where it resides.
You have declared a pointer to constant data. The data may reside
anywhere (including writable memory), but it cannot be altered
through the 'p' pointer.
Read your C language reference manual and the FAQ (in my
signature) about "const correctness".
3. when declared
volatile int *p
where exactly in the memory it is stored.
i know it reads explicitly from memory, & wont allow compiler to
optimise.
Again, the _pointer_ can reside anywhere. You have declared
a pointer to volatile _data_. The pointer is not volatile,
just the location it points to.
The compiler may optimize the instructions using the pointer.
However, the executable code must refresh, reload, the data
before itis used. The compiler cannot treat the variable as
unchanging (which is different than constant).
These types of pointers are often used to point to locations
that are changed by the hardware without notifying the software,
such as I/O devices (UARTs, USB, etc.)
4. what are different stack memory... heap memory... organised.
& how are variables stored in memory..
is it int * p
p=malloc(4); allocates 4 bytes in heap memory??
There is no requirement that an implementation must have a
stack or heap. Those are popular data structures that many
implementations use, but they are not required.
The method for determining storage is implementation dependent.
A compiler may place all the variables in registers or in
a segment of memory.
The malloc() function allocates from _dynamic_ memory.
Dynamic memory is memory that is available to a program during
runtime. Variables allocated in this area have a life-time
extending until they are deleted by the program or the
program's execution ends. This differs from variables declared
in function or block scope which disappear after execution
leaves the function or block, respectively.
Also, the malloc() function is required to return _at_least_
as many units of memory as requested. It may allocate more.
For example, if a platform is most efficient accessing 32-bit
(4-byte) quantities, a malloc(3) may actually allocate 4
bytes so as to maintain efficiency of the processor.
5.
char *str1="hi"
char *str2="ya";
main()
{
whats wrong in strcpy(str1,str2)????????
}
The problem is that you are copying one constant string
literal, "ya", on top of another constant string literal,
"hi". This often leads to undefined behavior. The
compiler may store the literals in the code segment or
in ROM.
Get into the habit of using pointers to const data for
pointers to string literals:
const char * str1 = "hi"; /* pointer to const data */
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq:
http://www.parashift.com/c++-faq-lite
C Faq:
http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library