#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0

    some doubts in c


    1)can i use nested function in c. it is working with
    gnu compiler

    f1()
    {

    f2()
    {
    printf("from f2");
    }

    f2()
    }
    how many nesting possible

    2)Why nonstatic function can't access ststic varibale in c++ ?

    3) where will static varibale will stored in memmory
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    all i can add is that static variables are stored in the heap.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0

    not very sure


    static varibale r stored in heap
    i aslo think so But i went for an interview
    and the interviwer told me it is not correct and it stored in data
    area can you get exact picture
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    2) How do you explain the results of this program?

    Code:
    #include <iostream>
    using namespace std;
    
    class Person
    {
    public:
    	static int objectCount;
    	char* name;
    	
    	Person(char* n)
    	{
    		name = n;
    		objectCount++;
    	}
    	void func()
    	{
    		cout<<objectCount<<endl;
    	}
    };
    
    int Person::objectCount = 0;
    
    int main()
    {
    	Person my_object("Tim");
    	cout<<my_object.name<<endl;
    	cout<<my_object.objectCount<<endl;
    
    	my_object.func();
    
    	return 0;
    }
    Last edited by 7stud; August 1st, 2003 at 01:00 AM.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0
    i am telling about c
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    2)Why nonstatic function can't access ststic varibale in c++ ?
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0
    hello i want to know c implementation
    not c++
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0
    non static function in the case of c++
    only static varibale can be access in static function

    and i doubt why this implementation
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0
    hello 7stud


    can u tell answers to my other questions
  18. #10
  19. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    hmm yes apparently we were both wrong, i guess they arent stored on the heap, i think it is dynamically allocated memory that is stored on the heap.
    AUTOMATIC AND STATIC VARIABLES
    C programs have a number of segments (or areas) where data is located. These segments are typically,

    _DATA Static data

    _BSS Uninitialized static data, zeroed out before call to main()

    _STACK Automatic data, resides on stack frame, thus local to functions

    _CONST Constant data, using the ANSI C keyword const
    http://www.cs.ntu.edu.au/sit/resourc...gram/c_046.htm
  20. #11
  21. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    india
    Posts
    28
    Rep Power
    0
    I herad it is in some data area. Ok can you explain in c++ why

    nostatic function can't access static variable??

    And what about function nesting ???

    can you explain it
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    138
    Rep Power
    12
    Hi Sujji,
    I think that is because,"The static methods cannot refer to the non-static data members of the class because values for those only exist for instances of the class. "
  24. #13
  25. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    1)can i use nested function in c. it is working with
    gnu compiler
    c doesn't allow nested functions at all, in any way at all so far as i know. if you're using nested functions then you're not using c i don't think.

    the gnu compiler can compile c and c++ so i'm going to guess it's compiling your code that you're referring to as c as c++.

    not sure, but code that uses nested functions can't correctly be described as c.



    nostatic function can't access static variable??
    ? :/ there's 2 types of static - internal and external. internal is only applicable to variables, whereas external is applicable to both functions and variables.

    internal static (or internal non static - doesn't make any difference) variables can only be accessed from within the function where it resides. the scope of internal variables is limited to the function they're in. static, or not stitic, in conjunction with internal variables doesn't make any difference to the scope (scope as in, what can access them (or what can see them), and what can't.)
    (making an internal variable static effects it's lifetime (not scope), as in it lasts from when the function it's in is first called to when the whole code ends, where as internal non-static variables only last so long as that particular single function call lasts - normal / usual 'automatic' variables basically)

    external variables, and functions can both have static applied to them. functions are always external - there can't be an internal function (so far as c goes). if an external variable or a function is static then this limits their scope to the file they're in.

    obviously functions usually have a very global nature to them - they can be accessed pretty much from anywhere in the code, but making them static restricts that globalness - restricts their scope. and the same goes for global variables - static global (external) variables are no longer global - it removes their global nature; their scope is restricted to the file they're defined in.

    i think that's how it is but i'm not *totally* 100% sure but i think it is. so far as where on the heap or stack they're stored i'm not sure. also this is all from a c point-of-view. some of it may also be applicable to c++, but there again it may not be.
    Last edited by balance; August 1st, 2003 at 10:10 AM.

IMN logo majestic logo threadwatch logo seochat tools logo