Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > variable allocated from stack/bss ??

Reply
Thread Tools

variable allocated from stack/bss ??

 
 
onkar
Guest
Posts: n/a
 
      11-29-2006
Given the following code & variable i .

int main(int argc,char **argv){
int i;
printf("%d\n",i);
return 0;
}

here i is allocated from bss or stack ?

I think it is stack,because it prints garbage value; If it were
allocated from bss the default value would be 0
please tell me if I am right or wrong ??

 
Reply With Quote
 
 
 
 
MQ
Guest
Posts: n/a
 
      11-29-2006

onkar wrote:
> Given the following code & variable i .
>
> int main(int argc,char **argv){
> int i;
> printf("%d\n",i);
> return 0;
> }
>
> here i is allocated from bss or stack ?
>
> I think it is stack,because it prints garbage value; If it were
> allocated from bss the default value would be 0
> please tell me if I am right or wrong ??


Local variables are stored on the stack. BSS is used for uninitialized
global or static variables.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      11-29-2006
onkar wrote:
> Given the following code & variable i .
>
> int main(int argc,char **argv){
> int i;
> printf("%d\n",i);
> return 0;
> }
>
> here i is allocated from bss or stack ?
>
> I think it is stack,because it prints garbage value; If it were
> allocated from bss the default value would be 0
> please tell me if I am right or wrong ??


The C standard doesn't specify the exact places where variables of
different types are stored. It just specifies their scope,
implementations are free to do whatever they want behind the scenes, as
long as all the necessary semantics are maintained.

Coming to your off-topic question, the short answer is yes, on most
implementations.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-29-2006
MQ said:

<snip>

> Local variables are stored on the stack.


C&V please.

> BSS is used for uninitialized
> global or static variables.


C&V please.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-29-2006
onkar said:

> Given the following code & variable i .
>
> int main(int argc,char **argv){
> int i;
> printf("%d\n",i);
> return 0;
> }
>
> here i is allocated from bss or stack ?


The C Standard doesn't require your implementation to have a bss or a stack.
It does require, however, that you provide a proper prototype for printf,
and it also requires that you don't evaluate indeterminately-valued
objects.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-29-2006
onkar wrote:
> Given the following code & variable i .
>
> int main(int argc,char **argv){
> int i;
> printf("%d\n",i);
> return 0;
> }
>
> here i is allocated from bss or stack ?
>
> I think it is stack,because it prints garbage value; If it were
> allocated from bss the default value would be 0
> please tell me if I am right or wrong ??


Neither 'bss' nor 'stack' have any meaning in C. All we can say is that
'i' is an uninitialized automatic variable, and so has an indeterminate
initial value. If your implementation has some segment of memory
initialized to zero -- and that is by no means a given -- then it may,
or may not, use parts of that for some automatic variables. The
mechanism is completely up to the implementation.

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-29-2006
MQ wrote:
> onkar wrote:
>> Given the following code & variable i .
>>
>> int main(int argc,char **argv){
>> int i;
>> printf("%d\n",i);
>> return 0;
>> }
>>
>> here i is allocated from bss or stack ?
>>
>> I think it is stack,because it prints garbage value; If it were
>> allocated from bss the default value would be 0
>> please tell me if I am right or wrong ??

>
> Local variables are stored on the stack. BSS is used for uninitialized
> global or static variables.


There is no such requirement for an implementation of the C programming
language. There is, in fact, no requirement for things called 'stack'
or 'BSS' to even exist. Please don't give such foolish and incorrect
'answers'. They only show your limited experience and lack of
qualification to give meaningful answers.
 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      11-29-2006
Martin Ambuhl wrote:
> onkar wrote:
>
>> Given the following code & variable i .
>>
>> int main(int argc,char **argv){
>> int i;
>> printf("%d\n",i);
>> return 0;
>> }
>>
>> here i is allocated from bss or stack ?
>>
>> I think it is stack,because it prints garbage value; If it were
>> allocated from bss the default value would be 0
>> please tell me if I am right or wrong ??

>
>
> Neither 'bss' nor 'stack' have any meaning in C. All we can say is that
> 'i' is an uninitialized automatic variable, and so has an indeterminate
> initial value. If your implementation has some segment of memory
> initialized to zero -- and that is by no means a given -- then it may,
> or may not, use parts of that for some automatic variables. The
> mechanism is completely up to the implementation.
>


Translation:

In some weird implementation that could exist somewhere there is no
stack and no bss. Since I want to be pedantic I will generalize then
to say that "The C language dfoesn't require a stack" even if I know
that 99% of all the implementations in work stations today use
exactly those.

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      11-29-2006
onkar wrote:
> Given the following code & variable i .
>
> int main(int argc,char **argv){
> int i;
> printf("%d\n",i);
> return 0;
> }
>
> here i is allocated from bss or stack ?
>
> I think it is stack,because it prints garbage value; If it were
> allocated from bss the default value would be 0
> please tell me if I am right or wrong ??
>


Yes, you are right
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      11-29-2006
jacob navia said:

> onkar wrote:
>> Given the following code & variable i .
>>
>> int main(int argc,char **argv){
>> int i;
>> printf("%d\n",i);
>> return 0;
>> }
>>
>> here i is allocated from bss or stack ?
>>
>> I think it is stack,because it prints garbage value; If it were
>> allocated from bss the default value would be 0
>> please tell me if I am right or wrong ??
>>

>
> Yes, you are right


Chapter and verse, please.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
How to find the size of dynamically allocated struct-type variable? nmtoan@gmail.com C Programming 5 10-21-2005 06:26 PM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Allocated variable returning by a fuction is unallocated automatically ? orion30 C Programming 1 07-15-2003 05:09 AM
An allocated variable returning by a fuction is unallocated automatically ? orion30 C++ 2 07-14-2003 10:39 AM



Advertisments