473,387 Members | 1,619 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

memory oraganisation??

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??
2.When you say
const int *p;
where is p stored in the memory?? what happens internal so that its a read only.

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.

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??

5.
char *str1="hi"
char *str2="ya";
main()
{
whats wrong in strcpy(str1,str2)????????
}
6.#define int *INT1
typedef int * INT2
INT1 a,b
INT2 c,d

whats the data type of a,b,c,d??
Nov 14 '05 #1
8 2895
vikram <vi******@yahoo.co.uk> scribbled the following:
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??

Depends entirely on the implementation. There might not even be "code
segments" and "data segments" at all. Your code might be stored on
clay tablets for all C cares.
2.When you say
const int *p;
where is p stored in the memory?? what happens internal so that its a read only.
See question 1.
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.
See question 1.
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??
See question 1.
5.
char *str1="hi"
char *str2="ya";
main()
{
whats wrong in strcpy(str1,str2)????????
}
String literals can't be modified in standard C. Declare str1 as
char str1[]="hi" and you'll be fine.
6.#define int *INT1
typedef int * INT2
INT1 a,b
INT2 c,d whats the data type of a,b,c,d??


a, c and d are int *. b is int.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The trouble with the French is they don't have a word for entrepreneur."
- George Bush
Nov 14 '05 #2
2.When you say
const int *p;
where is p stored in the memory??
Depends on where you put it. If it is a global variable, it will probably be
put in the Data section (provided there is one, see other newsgroup post).
If it is in a function, it is most likely in the stack.
what happens internal so that its a read only.
"*p" is read-only. "p" itself is not. "*p" itself will probably be stored
in the .rodata (Read Only Data Segment) section (or .data if there is no such
..rodata). This is all compiler dependent. However, imagine this:

int xx = 55;
const int *p = &xx;

This will work, you probably need a cast, but then, "*p" (or xx for that
matter) is /not/ stored in .RODATA (but very well be stored in .DATA, which is
read-write.)
3. when declared
volatile int *p
where exactly in the memory it is stored.
Same as above. "volatile" does not change the location.
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??
http://webster.cs.ucr.edu/AoA/Linux/...rg.html#999687
5.
char *str1="hi"
char *str2="ya";
main()
{
whats wrong in strcpy(str1,str2)????????
}
Strings of this kind are constant (= read-only). Also see
char str1[] = "not constant";
const char str[] = "constant";
char *str1[] = "constant"; *****
const char *str1[] = "constant";

For the **** case: GNU GCC has an option to make these strings non-constant,
see its -fwritable-strings option.
6.#define int *INT1
typedef int * INT2
INT1 a,b
INT2 c,d

whats the data type of a,b,c,d??


A and B are of type INT1, whatever that may be. Note that INT1 is not
substitued by 'int *', but "int" (and only int) is replaced by "*INT1". (Which
leads to another interesting side-effect: your typedef will get broken.)

C and D are of type "int *".

The correct approach would have been:
#define INT1 int *

Jan Engelhardt
--
Nov 14 '05 #3
[..]
6.#define int *INT1
#define INT1 int *
typedef int * INT2
INT1 a,b
INT2 c,d

whats the data type of a,b,c,d??

Nov 14 '05 #4
vikram wrote:

i have series of questions
1.How a c program is loaded in memory
.... snip ...
whats the data type of a,b,c,d??


When is the homework due? You neglected to post your instructors
name and e-mail so that we could send the answers directly.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
vikram wrote:
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

Nov 14 '05 #6
>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??
memory is not guaranteed to even HAVE smegments. Nor is there any
guarantee that there is a unique *THE* code segment any more than
your hand has *THE* finger.
2.When you say
const int *p;
where is p stored in the memory??
*IN MEMORY*. What answer would you like: thirty-third gate from
the northeast corner of J47? The variable may move in physical
memory numerous times during the execution of the program (due to,
say, paging or swapping).
what happens internal so that its a
read only.
You declared it const, so it's not supposed to be written on.
Implementations vary as to whether you actually CAN write on it.
Some systems have no memory protection whatever (Z80 and 8086
systems, for example). Some use the memory management hardware in
the CPU or in auxiliary circuitry (80[3456]86 processors, for
example) to enforce it being read-only. Even if the CPU has such
capabilities, C isn't guaranteed to use them (For example, a
80[3456]86 processor running MS-DOS probably doesn't do memory
protection of const variables. The same processor running a recent
Windows or UNIX OS probably would if the compiler cooperates. And
then there's the -fwritable-strings option on GCC, which says to
put quoted string literals in read-write memory anyway)).
3. when declared
volatile int *p
where exactly in the memory it is stored.
thirty-third gate from the northeast corner of J47?
GPS coordinates 32.78538838172645 N 96.79765501837465 W ?
i know it reads explicitly from memory, & wont allow compiler to
optimise.
Optimization may be in the eye of the beholder. For example,
volatile does not prohibit the operation of CPU or motherboard
cache memory (and some hardware may not be able to turn it off).
Also, nothing prohibits a volatile variable from being paged out
to disk.
4. what are different stack memory... heap memory... organised.
Memory is not organized by the AFL-CIO or other labor unions (which
are distinct from the definition of "union" in the C standard).
There is no guarantee that there even IS a hardware stack.
ANSI C does not define anything like "stack memory", "heap memory",
"code smegment", or "data smegment".
& how are variables stored in memory..
is it int * p
p=malloc(4); allocates 4 bytes in heap memory??
This is trivially true by definition *IF* you use a common definition
of heap memory: "that place from which malloc() allocates its
memory", which still doesn't rule out having what you call "stack",
"heap", "code smegment", and "data smegment" all intermingled.

Do not assume that the 4 bytes allocated have 8 bits each.
They have *AT LEAST* 8 bits, but there are some implementations
where a byte, as defined in the C standard, is 32 bits. and
nothing rules out bytes of 11, 29, or 73 bits.
5.
char *str1="hi"
char *str2="ya";
main()
{
whats wrong in strcpy(str1,str2)????????
You may not write to string literals in C. This invokes the wrath
of undefined behavior. (You might get a segmentation fault, or you
might succeed in scribbling on the memory any, changing *ALL* of
the "hi" strings in other portions of the program to "ya" because
the compiler consolidated them into one string. Or it might even
appear to work.)
}
6.#define int *INT1
typedef int * INT2
INT1 a,b
INT2 c,d

whats the data type of a,b,c,d??


This shouldn't even compile. That typedef comes out as:

typedef *INT1 *INT2

plus you are missing some semicolons. Also, doing a #define of int
is likely to wreak havoc on other parts of the program.
Gordon L. Burditt
Nov 14 '05 #7
"Joona I Palaste" <pa*****@cc.helsinki.fi> wrote in message
news:bv**********@oravannahka.helsinki.fi...
vikram <vi******@yahoo.co.uk> scribbled the following:
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??

Depends entirely on the implementation. There might not even be "code
segments" and "data segments" at all. Your code might be stored on
clay tablets for all C cares.
2.When you say
const int *p;
where is p stored in the memory?? what happens internal so that its a
read only.
See question 1.


These are actually two distinct questions.
Q2a: Where is p stored in memory?
A: See Q1.
Q2b: What happens internal[ly,] so that it[']s read-only?
A: It is not defined. 'const' means read-only _to a compiler_. If you let
your compiler do its job properly, it does not let you change anything
declared 'const', so you don't need to care whether it actually is read-only
or not. If you meddle with the compiler's job (e.g. by casting), you get
what you deserve. See Q1 ;-)
6.#define int *INT1
typedef int * INT2
INT1 a,b
INT2 c,d

whats the data type of a,b,c,d??


a, c and d are int *. b is int.


Are you sure? ;-)

To the OP: you have *two* syntax errors there (though your compiler will
probably display more). First, missing semicolons on lines 2,3 and 4.
Second, there is no symbol INT1. There is, however, a symbol int defined as
*INT1, making your typedef and everything else fail. ITYM:

#define INT1 int *
typedef int * INT2;
INT1 a,b; /* a is int*, b is int */
INT2 c,d; /* c and d are int* */
Nov 14 '05 #8
"Jan Engelhardt" <je*****@linux01.gwdg.de> wrote in message
news:Pi******************************@yvahk01.tjqt .qr...

int xx = 55;
const int *p = &xx;

This will work, you probably need a cast, [...]
Why?
Strings of this kind are constant (= read-only). Also see
char str1[] = "not constant";
const char str[] = "constant";
char *str1[] = "constant"; *****
const char *str1[] = "constant";

For the **** case: GNU GCC has an option to make these strings non-constant, see its -fwritable-strings option.
For the **** case: I would be surprised to see it compile. 'char *str1[];'
declares an array (of unknown size) of pointers to char. This needs to be
initialized as one. Add braces around your string and you should be OK.
6.#define int *INT1
typedef int * INT2
INT1 a,b
INT2 c,d

whats the data type of a,b,c,d??


A and B are of type INT1, whatever that may be. Note that INT1 is not
substitued by 'int *', but "int" (and only int) is replaced by "*INT1".

(Which leads to another interesting side-effect: your typedef will get broken.)

C and D are of type "int *".

The correct approach would have been:
#define INT1 int *


For a rather strange definition of 'correct', yes ;-)

Peter
Nov 14 '05 #9

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

Similar topics

0
by: Andreas Suurkuusk | last post by:
Hi, I just noticed your post in the "C# memory problem: no end for our problem?" thread. In the post you implied that I do not how the garbage collector works and that I mislead people. Since...
4
by: Frank Esser | last post by:
I am using SQL 8 Personal edition with sp2 applied. I set the max server memory to 32MB and leave the min server memory at 0. When my application starts hitting the database hard the memory usage...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
9
by: Mike P | last post by:
I know everything about reference counting and making sure you don't have large objects lying around. I have also profiled my app with multiple tools. I know about the fact GC collects memory but...
12
by: Jeremy | last post by:
Hi all, I'm getting very confused about how DB2 uses shared memory and I wonder if someone could clarify matters for me, please ? We are running 32bit DB2 V7.2 FP9 under AIX 4.3.3 on a machine...
22
by: xixi | last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer pool size to accommadate better performance, say size 200000, if i have multiple connection to the same database from...
5
by: kumarmdb2 | last post by:
Hi guys, For last few days we are getting out of private memory error. We have a development environment. We tried to figure out the problem but we believe that it might be related to the OS...
1
by: Jean-Paul Calderone | last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote: The test doesn't demonstrate any leaks. It does demonstrate that memory usage can remain at or near peak memory usage even after...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.