Re: [algogeeks] C concept on memory layout

2012-02-07 Thread Manni mbd
i didn't got .. !! please explain some more..

On 2/7/12, sumit mahamuni sumit143smail...@gmail.com wrote:
 Hello,

 Here you are right about variable p in add function that it retains it's
 value even though function loses its scope. And for main function error you
 are seeing has nothing to do with how that variable is stored?
 It is about the scope of that variable C compiler sees the scope of static
 variable p  limited to add function only, so other function can't use it.
 And remember error is at compile time not run time so it is related to
 compiler not memory. I hope it explains everything.

 On Tue, Feb 7, 2012 at 9:43 AM, atul anand atul.87fri...@gmail.com wrote:

 http://www.geeksforgeeks.org/archives/14268


 On Tue, Feb 7, 2012 at 1:06 AM, gmagog...@gmail.com
 gmagog...@gmail.comwrote:

 I think you are right about p being in BSS segment and it does last even
 the function finishes, however, you may need a pointer to get the data
 out
 of p. Then you can read the data.

 Correct me if i am wrong


 On Mon, Feb 6, 2012 at 1:04 PM, Ravi Ranjan
 ravi.cool2...@gmail.comwrote:

 i have a confusion in it

 #include stdio.h
 #include stdlib.h


 void add(int,int);

 int main(int argc, char *argv[])
 {

 add(6,3);
 printf(%d,p);

   system(PAUSE);
   return 0;
 }


 void add(int a, int b)
 {
   static int p;
 p = a+ b;
 }


 here the memory layout says variable p is in BSS segment ... so its an
 independent region from stack frame. when the function looses its
 scope
 from function defination(add) then still it should be alive... and can
 be
 recognized/used by other function(main) but it gves an error of
 unknown
 variable p.  need the correct logic... if i m wrong...

 thanx
 ravi

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




 --
 Thanks and Regards,
 Sumit Mahamuni.

 -- Slow code that scales better can be faster than fast code that doesn't
 scale!
 -- Tough times never lasts, but tough people do.
 -- I love deadlines. I like the whooshing sound they make as they fly by. -
 D. Adams

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] C concept on memory layout

2012-02-07 Thread rahul
i think  guys are confuse between scope of variable and lifetime of
variable.
p scope is add function and lifetime of p is till the program run.
so u can't access variable outside the scope of variable whatever is the
lifetime of variable.

u can look at peter ven den linden Deep C secrets.

best regards,

On Tue, Feb 7, 2012 at 10:43 AM, Manni mbd mbd2...@gmail.com wrote:

 i didn't got .. !! please explain some more..

 On 2/7/12, sumit mahamuni sumit143smail...@gmail.com wrote:
  Hello,
 
  Here you are right about variable p in add function that it retains it's
  value even though function loses its scope. And for main function error
 you
  are seeing has nothing to do with how that variable is stored?
  It is about the scope of that variable C compiler sees the scope of
 static
  variable p  limited to add function only, so other function can't use it.
  And remember error is at compile time not run time so it is related to
  compiler not memory. I hope it explains everything.
 
  On Tue, Feb 7, 2012 at 9:43 AM, atul anand atul.87fri...@gmail.com
 wrote:
 
  http://www.geeksforgeeks.org/archives/14268
 
 
  On Tue, Feb 7, 2012 at 1:06 AM, gmagog...@gmail.com
  gmagog...@gmail.comwrote:
 
  I think you are right about p being in BSS segment and it does last
 even
  the function finishes, however, you may need a pointer to get the data
  out
  of p. Then you can read the data.
 
  Correct me if i am wrong
 
 
  On Mon, Feb 6, 2012 at 1:04 PM, Ravi Ranjan
  ravi.cool2...@gmail.comwrote:
 
  i have a confusion in it
 
  #include stdio.h
  #include stdlib.h
 
 
  void add(int,int);
 
  int main(int argc, char *argv[])
  {
 
  add(6,3);
  printf(%d,p);
 
system(PAUSE);
return 0;
  }
 
 
  void add(int a, int b)
  {
static int p;
  p = a+ b;
  }
 
 
  here the memory layout says variable p is in BSS segment ... so its
 an
  independent region from stack frame. when the function looses its
  scope
  from function defination(add) then still it should be alive... and can
  be
  recognized/used by other function(main) but it gves an error of
  unknown
  variable p.  need the correct logic... if i m wrong...
 
  thanx
  ravi
 
  --
  You received this message because you are subscribed to the Google
  Groups Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
 
   --
  You received this message because you are subscribed to the Google
 Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
 
   --
  You received this message because you are subscribed to the Google
 Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
 
 
 
  --
  Thanks and Regards,
  Sumit Mahamuni.
 
  -- Slow code that scales better can be faster than fast code that doesn't
  scale!
  -- Tough times never lasts, but tough people do.
  -- I love deadlines. I like the whooshing sound they make as they fly
 by. -
  D. Adams
 
  --
  You received this message because you are subscribed to the Google Groups
  Algorithm Geeks group.
  To post to this group, send email to algogeeks@googlegroups.com.
  To unsubscribe from this group, send email to
  algogeeks+unsubscr...@googlegroups.com.
  For more options, visit this group at
  http://groups.google.com/group/algogeeks?hl=en.
 
 

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.



-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] C concept on memory layout

2012-02-07 Thread Atul Singh
I think rahul has given a clear solution i.e why the static variable is not
accessible in main() function because of its scope.
I would like to add one more point in this...that  Static variables may be
initialized in their declarations;

however, the initializers must be constant expressions, and initialization
is done only once at compile time when memory is allocated for the static
variable

-- 
ATul Singh | Final Year  | Computer Science  Engineering | NIT
Jalandhar  | 9530739855
|

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] C concept on memory layout

2012-02-07 Thread Ravi Ranjan
@all

thanx for the explanation..

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] C concept on memory layout

2012-02-06 Thread Ravi Ranjan
i have a confusion in it

#include stdio.h
#include stdlib.h


void add(int,int);

int main(int argc, char *argv[])
{

add(6,3);
printf(%d,p);

  system(PAUSE);
  return 0;
}


void add(int a, int b)
{
  static int p;
p = a+ b;
}


here the memory layout says variable p is in BSS segment ... so its an
independent region from stack frame. when the function looses its scope
from function defination(add) then still it should be alive... and can be
recognized/used by other function(main) but it gves an error of unknown
variable p.  need the correct logic... if i m wrong...

thanx
ravi

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] C concept on memory layout

2012-02-06 Thread gmagog...@gmail.com
I think you are right about p being in BSS segment and it does last even
the function finishes, however, you may need a pointer to get the data out
of p. Then you can read the data.

Correct me if i am wrong


On Mon, Feb 6, 2012 at 1:04 PM, Ravi Ranjan ravi.cool2...@gmail.com wrote:

 i have a confusion in it

 #include stdio.h
 #include stdlib.h


 void add(int,int);

 int main(int argc, char *argv[])
 {

 add(6,3);
 printf(%d,p);

   system(PAUSE);
   return 0;
 }


 void add(int a, int b)
 {
   static int p;
 p = a+ b;
 }


 here the memory layout says variable p is in BSS segment ... so its an
 independent region from stack frame. when the function looses its scope
 from function defination(add) then still it should be alive... and can be
 recognized/used by other function(main) but it gves an error of unknown
 variable p.  need the correct logic... if i m wrong...

 thanx
 ravi

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] C concept on memory layout

2012-02-06 Thread atul anand
http://www.geeksforgeeks.org/archives/14268

On Tue, Feb 7, 2012 at 1:06 AM, gmagog...@gmail.com gmagog...@gmail.comwrote:

 I think you are right about p being in BSS segment and it does last even
 the function finishes, however, you may need a pointer to get the data out
 of p. Then you can read the data.

 Correct me if i am wrong


 On Mon, Feb 6, 2012 at 1:04 PM, Ravi Ranjan ravi.cool2...@gmail.comwrote:

 i have a confusion in it

 #include stdio.h
 #include stdlib.h


 void add(int,int);

 int main(int argc, char *argv[])
 {

 add(6,3);
 printf(%d,p);

   system(PAUSE);
   return 0;
 }


 void add(int a, int b)
 {
   static int p;
 p = a+ b;
 }


 here the memory layout says variable p is in BSS segment ... so its an
 independent region from stack frame. when the function looses its scope
 from function defination(add) then still it should be alive... and can be
 recognized/used by other function(main) but it gves an error of unknown
 variable p.  need the correct logic... if i m wrong...

 thanx
 ravi

 --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] C concept on memory layout

2012-02-06 Thread sumit mahamuni
Hello,

Here you are right about variable p in add function that it retains it's
value even though function loses its scope. And for main function error you
are seeing has nothing to do with how that variable is stored?
It is about the scope of that variable C compiler sees the scope of static
variable p  limited to add function only, so other function can't use it.
And remember error is at compile time not run time so it is related to
compiler not memory. I hope it explains everything.

On Tue, Feb 7, 2012 at 9:43 AM, atul anand atul.87fri...@gmail.com wrote:

 http://www.geeksforgeeks.org/archives/14268


 On Tue, Feb 7, 2012 at 1:06 AM, gmagog...@gmail.com 
 gmagog...@gmail.comwrote:

 I think you are right about p being in BSS segment and it does last even
 the function finishes, however, you may need a pointer to get the data out
 of p. Then you can read the data.

 Correct me if i am wrong


 On Mon, Feb 6, 2012 at 1:04 PM, Ravi Ranjan ravi.cool2...@gmail.comwrote:

 i have a confusion in it

 #include stdio.h
 #include stdlib.h


 void add(int,int);

 int main(int argc, char *argv[])
 {

 add(6,3);
 printf(%d,p);

   system(PAUSE);
   return 0;
 }


 void add(int a, int b)
 {
   static int p;
 p = a+ b;
 }


 here the memory layout says variable p is in BSS segment ... so its an
 independent region from stack frame. when the function looses its scope
 from function defination(add) then still it should be alive... and can be
 recognized/used by other function(main) but it gves an error of unknown
 variable p.  need the correct logic... if i m wrong...

 thanx
 ravi

 --
 You received this message because you are subscribed to the Google
 Groups Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.


  --
 You received this message because you are subscribed to the Google Groups
 Algorithm Geeks group.
 To post to this group, send email to algogeeks@googlegroups.com.
 To unsubscribe from this group, send email to
 algogeeks+unsubscr...@googlegroups.com.
 For more options, visit this group at
 http://groups.google.com/group/algogeeks?hl=en.




-- 
Thanks and Regards,
Sumit Mahamuni.

-- Slow code that scales better can be faster than fast code that doesn't
scale!
-- Tough times never lasts, but tough people do.
-- I love deadlines. I like the whooshing sound they make as they fly by. -
D. Adams

-- 
You received this message because you are subscribed to the Google Groups 
Algorithm Geeks group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.