Thread: use of extern

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    use of extern

    does extern only cover variables that are in another source file or is there away of doing it so it can find a variable that is in another function.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The extern keyword declares that an identifier has external linkage. If you have declared a variable in function scope without extern, then it has no linkage, and no amount of extern elsewhere will allow you to refer to it from outside the function.

    However, your question betrays a problem: you're still thinking at implementation-level for the whole program. As I mentioned previously, this cannot be sustained because at some point, it will be beyond your ability to think at implementation detail level for the entirety of a sufficiently large and complex program. You need to practice writing functions for which you specify the interface, and then remain secure in the knowledge that as long as you have written the function according to the "contract" specified by its interface, you don't need to worry about its implementation when writing other functions that call it, i.e., you don't have to "find a variable that is in another function", and in fact it is better that you "forget" that such a local variable existed!
    Last edited by laserlight; 05-17-2019 at 03:33 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    so how do i get round the issue of having to pass 20 arguments into a function so that a second function called by the first can use them (or a third or fourth? my argument to all this is if i have a variable that is so critical to so many functions that its one of the arguments passed to 99% of the functions how is that secure. It means as i have just discovered i have to go through 500 lines of code testing to see what changed it
    coop

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    so how do i get round the issue of having to pass 20 arguments into a function so that a second function called by the first can use them (or a third or fourth?
    Restructure your code so that these functions are called by another function, instead of one function calling another in a chain. It might also be a sign that certain arguments should have been grouped into a struct.

    Quote Originally Posted by cooper1200
    my argument to all this is if i have a variable that is so critical to so many functions that its one of the arguments passed to 99% of the functions how is that secure.
    That might be the case if you really have global state, and sometimes you do, e.g., think of standard I/O and stdout, stdin, stderr. However, I suspect that you're just passing the variable to 99% of your functions because you haven't considered how to write functions to work on only parts of that variable (presumably it is a struct, an array, or some other container).

    Quote Originally Posted by cooper1200
    It means as i have just discovered i have to go through 500 lines of code testing to see what changed it
    You can automate unit testing so that you can more easily discover whether your functions are indeed doing their jobs independently of others, and then automate integration testing to see if the functions composed of other function calls are indeed doing their jobs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    lets take this function as an example
    Code:
    void get_bets(int num_players, int *p_pooled_bets, Card *p_deck, Hand *p_player)
    {
        int i, highest_bet = 0, temp_bet, loop = 1;
        while (loop)
        {
            for (i = 0; i < num_players; i++)
            {
                show_hand(i, p_player);
                temp_bet = get_bet(p_player[i].bank, p_player[i].bets, highest_bet, p_player[i].player_name);
    
                if (temp_bet)
                {
                    p_player[i].bets += temp_bet;
                    p_player[i].bank -= temp_bet;
                    highest_bet = p_player[i].bets;
                }
    //*
                else
                {
                    num_players = player_folded(i, num_players, p_pooled_bets, p_deck, p_player);
                    i -= 1;
                }
    //*/
                if (check_bets_equal(num_players, highest_bet, p_player))
                {
                    loop = 0;
                    break;
                }
            }
        }
    }
    the first part calls show hand. that needs the player whos turn it is to bet so i passed in his number (i) and the player array,
    second one gets the bet from the user and either returns a valid bet or 0 if the player folds and takes in specific parts of the player array.
    third bit is checking if a bet was returned or the player folded. if a bet was returned update highest bet in case of a raise. if the player folded it calls player folded and that takes the arguments player number whos turn it is ie the one that folded the total number of players, pooled bets so we can record the players bets so far, the deck array and finally the player array so each player can be copied down one element.
    player folded function checks to see if the player that folded was the last one in the array (so i dont go out of bounds) if it isnt copies the player after that into his slot and so on. then it calls a function to put the players cards back in the deck. that function finaly uses the deck argumement that was passed in 4 functions previously.

    i can see 2 solutions to this.... either have some complex code that the functions return back down the line (i can only return one thing so that means more pointers passed into the first function) and legthy bits of code with switch statements to determine what has changed.

    the second solution is to have a global variable where at least the only thing that can change it is what i program is if i want to change the card at index 3 i would have to write deck[3] there wouldnt be any issue of pointers not being passed correctly.

    i dont think i am explaining what im confused about very well so i hope you can understand the issue
    coop

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't have a simple remedy for what you're talking about because it requires understanding more than what you've shown, but consider:
    Code:
    show_hand(i, p_player);
    For starters, p_player is misleading: it sounds like a pointer to a particular player, but in the way you're using it, it's a pointer to the first element of an array of players, i.e., in colloquial usage we would call it a pointer to an array of players. Why do you need an entire array of players to show the hand of a player? You could have written:
    Code:
    show_hand(p_player + i);
    Looking at get_bet, it looks like you only need p_player + i and highest_bet.

    Looking at the bigger picture of get_bets: does it actually get_bets? It looks more like it is doing much more than that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    get bets doesn't actually get the physical for want of a better word bet. get bet does.

    general idea is its a round of betting before the players change cards in their hands there are four sub consepts to that
    1)show the players hand (so he/she knows what they are betting on)
    2)get the players bet
    3)the player might fold
    4 check the bets are equall ie every player has "called"

    get bets is the interface to them.

    coop

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i take your point on the confustion of p_player it should be plural then its clear i mean the whole array not just an element (especially as i have other functions that work on just 1 element at a time)

    your other point answers a question i asked on a different thread about which is better. pass the whole struct or pass separate members of the struct

    coop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 05-07-2017, 12:29 PM
  2. extern
    By cable in forum C Programming
    Replies: 2
    Last Post: 07-06-2011, 06:07 PM
  3. Extern C
    By chico1st in forum C Programming
    Replies: 9
    Last Post: 06-13-2008, 02:34 AM
  4. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  5. Using extern C and mfc?
    By seizmic in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2005, 01:31 PM

Tags for this Thread