Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Global Variable Placement (http://www.chiefdelphi.com/forums/showthread.php?t=62318)

robbrad 22-01-2008 22:34

Global Variable Placement
 
I've tried to insert global variables in various places in Kevin's code only to get stack overflow, code errors (RC), sytax errors, compiler crashes and general system crashes. Where do you all put your global variables. I'm interested in global only. I'd like to minimise passing information between functions. Thanks

Kevin Watson 23-01-2008 00:50

Re: Global Variable Placement
 
Quote:

Originally Posted by robbrad (Post 683992)
I've tried to insert global variables in various places in Kevin's code only to get stack overflow, code errors (RC), sytax errors, compiler crashes and general system crashes. Where do you all put your global variables. I'm interested in global only. I'd like to minimise passing information between functions. Thanks

A good practice is to declare a variable outside of any function and then declare the variable as "extern" within the associated header file. So for example, at the top of teleop.c I declare a variable:

// this allocates memory
unsigned int foo;

Then in teleop.h I declare the variable again with the keyword extern:

// no memory is allocated, but lets the compiler know the name (foo)
// and type (unsigned int), and that it is located in another source file
extern unsigned int foo;

Then include teleop.h at the top of each source file that needs to use foo. The line will look like this:

#include "teleop.h"

-Kevin


All times are GMT -5. The time now is 23:10.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi