bss(uninitialized data) segment allocation


 
Thread Tools Search this Thread
Top Forums Programming bss(uninitialized data) segment allocation
# 1  
Old 04-24-2005
bss(uninitialized data) segment allocation

Hi
1) Please go through the following code :

char string2[12];
char string1[6];
main()
{
memcpy(string2,"SENDER ",12);
strcpy(string1,"******");
printf("%s\n%s\n",string1,string2);
}

2) and the output of size command for the executable is :

1904 + 415 + 18 = 2337

3) and the output is as expected ( gdb output ):

Breakpoint 1, main () at test.c:5
5 memcpy(string2,"SENDER ",12);
2: string2 = '\000' <repeats 11 times>
1: string1 = "\000\000\000\000\000"
(gdb) s
6 strcpy(string1,"******");
2: string2 = "SENDER "
1: string1 = "\000\000\000\000\000"
(gdb) s
7 printf("%s\n%s\n",string1,string2);
2: string2 = "\000ENDER "
1: string1 = "******"
My question is in what order the global variables are stored in the memory.

For the above example, from the output of size command 18 is the total size of the global variables and when the strcpy statement is executed, it is overwriting the first character of string2 with NULL in order to terminate string1 with NULL.

So, how the variables got stored in memory?
1) Is string1 followed by string2?
2) Is string2 followed by string1?

Could anybody help in this regard
Thanks
# 2  
Old 04-25-2005
just insert the following statement in your code:

Code:
fprintf(stdout,"string1: %d, string2: %d", string1,string2);

This should print the starting address of the two strings. Run the program several times and see if you can get any pattern (of memory allocation) from that.

Cheers!
# 3  
Old 04-25-2005
Just tried this on a FreeBSD system. The addresses allocated are the same every time, as no other memory is allocated in the interval between program runs.
Also, the allocation is in the order that is specified in the program. i.e. string2 is allocated a starting address that is (12 bytes) before that of string1.
Why exactly are you interested in knowing the order of allocation of the variables?
# 4  
Old 04-25-2005
If string2 were
Code:
char string2[13];

then the starting address of string1 may change, depending on your system's architecture. You cannot depend on the starting position of objects in memory.

This is because the compiler may adjust the starting byte of string1 to match the addressing scheme your system uses. Or to optimize access to the object.
# 5  
Old 04-25-2005
Why it is eating the first character of string2 only, if string2 is reserved first 12 bytes or whatever and string1 next 6 bytes

The order of declaration is:
char string2[12];
char string1[6];

The statement order is :
memcpy(string2,"SENDER ",12);
strcpy(string1,******);

It is interesting!!!
# 6  
Old 04-26-2005
I got the answer by analyzing all the replies from you.

I used the fprintf statement to know the starting address of the strings.
On my m/c, I found that it is storing string1 first and then string 2.

So, after executing strcpy(string1,"******") statement it is rewriting the first character of string2 with NULL in order to terminate string1 as strcpy does.

Is the order of storing is OS dependent or compiler dependent ?

Thanks a lot.
# 7  
Old 04-26-2005
It would depend on the OS, the compiler and the architecture of your system.. I think.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Telnet uninitialized

HI I need help regarding telnet as I try to run the service it gives error #svcs -xv svc:/network/telnet:default svc:/network/telnet:default (Telnet server) State: uninitialized since Wed May 15 16:46:41 2013 Reason: Restarter svc:/network/inetd:default is not running. See:... (14 Replies)
Discussion started by: smazshah
14 Replies

2. Solaris

Block-based allocation and Extent-based allocation in Solaris

Hi guys! Could you tell me what's this figure about? (See the attached figure below.) This is a representation of block allocation filesystem and extent allocation filesystem in Solaris. Does this mean that in a block-based allocation, data are placed in individual blocks while in... (0 Replies)
Discussion started by: arah
0 Replies

3. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

4. Shell Programming and Scripting

use of uninitialized value in subtraction

Hallo all i am trying to execute this script ............... But this is throwing the error...... use of uninitialized value in subtraction in at icd_convert.pl line 156 use of uninitialized value in subtraction in at icd_convert.pl line 157 use of uninitialized value in subtraction in at... (1 Reply)
Discussion started by: suvenduperl
1 Replies

5. AIX

data segment on AIX

Hi guys, Are all users authorised to modify the data segment and stack segment to unlimited on AIX? Is a reboot required after giving ulimit -d unlimited? Thanks vandi (2 Replies)
Discussion started by: vandi
2 Replies

6. Programming

dynamic allocation vs static allocation in c

i wrote a tiny version of tail command using a large buffer statically allocated but, in a second time, i found another version in which i use a bidimensional array dynamically allocated. here is the first version /*my tiny tail, it prints the last 5 line of a file */ #include<stdio.h>... (4 Replies)
Discussion started by: lucasclaus
4 Replies

7. Solaris

How to clear STATE - uninitialized in solaris 10

Helloo Frd's, How to clear uninitialized state in solaris 10 Services, the fallowing inetadm shows on my server. I tried svcadm clear, enable... not worked. And also i can't change to enable some of service on that. sara4@root# inetadm ENABLED STATE FMRI disabled ... (6 Replies)
Discussion started by: madhu548
6 Replies

8. UNIX for Advanced & Expert Users

Memory allocation for float data type

Dear All, How internally memory allocated when we declare the float data type. how many bytes allocated for decimal and how many bytes for fraction. kindly help me in this regards. (2 Replies)
Discussion started by: rajamohan
2 Replies

9. UNIX for Advanced & Expert Users

set Ulimit data segment to Unlimited

Hi, as per my Unix admin all parameters in Ulimit are set to Unlimited in Hard limits but some how few profiles setting data segment part to limited number value. So i wanted to over write in my profile to set unlimited as hard limits are set to unlimited. What is the command to set ulimit for... (1 Reply)
Discussion started by: terala_s
1 Replies

10. Shell Programming and Scripting

Extract data segment using awk??

How do I filter a long report, with the "STARTWORD" and "STOPWORD" as the variables to use in my awk command, to print the whole data segment that only contains the matched start/stop word? awk '/start/, /stop/' file <- this prints the line, though I need to print the whole segment. Newline... (1 Reply)
Discussion started by: apalex
1 Replies
Login or Register to Ask a Question