Reg. Local vs Global declarations


 
Thread Tools Search this Thread
Top Forums Programming Reg. Local vs Global declarations
# 1  
Old 04-30-2005
Reg. Local vs Global declarations

Please go through the following two versions of code :-

Version 1 --- string1 and string2 declared as Global variables
The output is :-- (as expected sprintf is overwriting the first byte of string2 with NULL)
string1=send
string2=

#include <stdio.h>
char string1[4];
char string2[5];
main()
{

strcpy(string2,"test");
sprintf(string1,"%s","send");
printf("string1=%s\n",string1);
printf("string2=%s\n",string2);
}

Version 2 --- string1 and string2 declared as Local variables
The output is :--(Is sprintf is terminating string1 with NULL or not?)
string1=send
string2=test

#include <stdio.h>
main()
{
char string1[4];
char string2[5];

strcpy(string2,"test");
sprintf(string1,"%s","send");
printf("string1=%s\n",string1);
printf("string2=%s\n",string2);
}

Is there any difference b/w memory allocation in Local and Global declarations.

Thanks for ur suggestion in advance.
# 2  
Old 05-02-2005
This depends on a lot of things like architecture, compiler, options, etc.

What architecture are you on ?

Version 1 --- string1 and string2 declared as Global variables
The output is :-- (as expected sprintf is overwriting the first byte of string2 with NULL)
string1=send
string2=


This need not happen becaus string2 comes before string1 (lower)
in memory (on PC linux and many architecutres), so the output is
string1=send
string2=test.

If the space allocated for string2 is too short, you will see
something like:
string1=send
string2=testsend

The space allocated for string1 does not change anything for this test.

Besides, use
sprint("%x", string1)


to see where the string space are allocated; try it with various array length, you will see that sometimes
the compiler leaves much more space than required between the too string,
may be for optimization related questions, be it on the heap or on the stack, so that it may well work even if the allocation is buggy.

HTH, else precise architecture, compiler etc.

samuel
# 3  
Old 05-02-2005
Thanks.

As u mentioned, it depends on arch., comp,... B'se I am also getting diff. o/p on Linux m/c.

Actually, my environment is : Sun OS 5.8 on Sun SPARC arch.

My observation is as follows( on solaris):

1) For the global case : allocation is string1 first and then string2
2) For the local case :
PUSH string1
decrement SP
PUSH string2
and hence string2 first and then string1

Once again, thanks for ur time.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple interpreter declarations

Hi, I am writing a shell script that connects to a remote server and performs some tasks on the server and exits. Since i am using a ssh connection, i am using a "expect" utility to supply the password automatically (which is present within the script). In order to use this utility, i need to... (3 Replies)
Discussion started by: sunrexstar
3 Replies

2. Solaris

Covert Global zone to local zone

Dears, I would like to convert solaris 10 x86 and solaris 10 sparc (Global Zones) physical servers into Local zones. i found a document which seems to be helpful but i'm stuck @ the 1st step. to test this i want to do it 1st on x86 system running under vmware ESXi and if it succeeds i will... (1 Reply)
Discussion started by: mduweik
1 Replies

3. UNIX for Dummies Questions & Answers

Global - Local script??

Hi, I have a script which can be called from any path on the machine. It kind of acts like a global script. How do I achive this? :confused: The path from which I call it is different from the path where it exists. (where <script name> ) Thanks and Regards, Preetham R. (1 Reply)
Discussion started by: preethgideon
1 Replies

4. HP-UX

Seeing different output from who -u in global INIT and local SRP

I am seeing different output from "who -u" when we execute "who -u" in global INIT and local SRP after calling telnet, rlogin and remsh. We are not seeing correct output when we login to local SRP using telnet, rlogin and remsh. srp_init just creates an UTMP entry in the UTMP database. Based my... (1 Reply)
Discussion started by: madhur.tripathi
1 Replies

5. Solaris

sharing a directory between local and global zone

is this the step? add fs set dir=/export set special=/export set type=lofs add options rw end i notice i can't post immediately, moderator needs to moderate. i have 1 more post still haven't appear in the forum..hmm.... (1 Reply)
Discussion started by: binary0011
1 Replies

6. Solaris

Global zone name from local zone

How to check the global zone name from local zone. (6 Replies)
Discussion started by: fugitive
6 Replies

7. Solaris

Not able to ping global zone from local zone

Hi Gurus I am not able to ping the local zone from global zone when i am trying to ping i am getting below ICMP Host Unreachable from gateway zone ( 192.268.35.210) for icmp from zone ( 192.168.35.210) to sun1 ( 192.168.35.210) However i can ping local zone from global please... (12 Replies)
Discussion started by: kumarmani
12 Replies

8. Shell Programming and Scripting

Problem with global and local variables

Guys, how can I define global variables in sorlaris...cause I lose the values outside the scope. Rite now wat I do is,I redirect variable value to a file n then get it back outside the function...:o....theres obviously a better way of doing this...I now this is a basic question....but please... (2 Replies)
Discussion started by: qzv2jm
2 Replies

9. Shell Programming and Scripting

Global variable becomes local

I have encountered a very weird behavior of a global variable in Korn Shell in AIX: A function f1 in my script pipes the output of the function f2 to a program. A variable defined as global using typeset gets its value in f2. That value is not seen in f1. If I remove the pipe ksh recognizes the... (2 Replies)
Discussion started by: odashe318
2 Replies
Login or Register to Ask a Question