Question About Globals

This is a discussion on Question About Globals within the C Programming forums, part of the General Programming Boards category; I have seen a few programs for unix that seem to group a lot of variables into a structure. They ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    1

    Question About Globals

    I have seen a few programs for unix that seem to group a lot of variables into a structure. They will init the variables somewhere near the beginning of the program and then pass a pointer to this structure in nearly every function call.

    Why do they do this, instead of, ie.
    declare "global_t foo" in main.c, malloc and init in main(),
    and then declare "extern global_t foo" in every other file and skip passing the pointer completely?

    BTW. I tested this both ways with LCC, and it made absolutely no difference in the size of the .exe. Did the compiler write exactly the same code both ways?

  2. #2
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    I think it is because when you are reading code and a bunch of variables are all part of a structure it is a bit easier to know what they are doing(in a large program) eg: cats.berman = 3 and cats.persion = 5 is easier to understand than berman = 3 and than persion = 5 appearing out of nowhere.

    Dont hold me to that but thats how it was described to me.
    "Assumptions are the mother of all **** ups!"

  3. #3
    zsaniK Kinasz's Avatar
    Join Date
    Jan 2003
    Posts
    222
    I should add that when they are in a structure you can pass them in to functions as read only, so that function cant modify the values if youve made a mistake in your code. Its also easy to see in the function prototype whethor or not the function will modify any values or just read them.
    "Assumptions are the mother of all **** ups!"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21