global-variables

Doing away with Globals?

I have a set of tree objects with a depth somewhere in the 20s. Each of the nodes in this tree needs access to its tree's root. A couple of solutions: Each node can store a reference to the root directly (wastes memory) I can compute the root at runtime by "going up" (wastes cycles) I can use static fields (but this amounts to global...

Worse sin: side effects or passing massive objects?

I have a function inside a loop inside a function. The inner function acquires and stores a large vector of data in memory (as a global variable... I'm using "R" which is like "S-Plus"). The loop loops through a long list of data to be acquired. The outer function starts the process and passes in the list of datasets to be acquired. I p...

Refactoring global to local. Should they be static or not?

I'm refactoring "spaghetti code" C module to work in multitasking (RTOS) environment. Now, there are very long functions and many unnecessary global variables. When I try to replace global variables that exists only in one function with locals, I get into dilemma. Every global variable is behave like local "static" - e.g. keep its valu...

PHP session side-effect warning with global variables as a source of data

I'm trying to host a PHP web site that was given to me. I see this warning: Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can...

When is it ok to use a global variable in C?

Apparently there's a lot of variety in opinions out there, ranging from, "Never! Always encapsulate (even if it's with a mere macro!)" to "It's no big deal - use them when it's more convenient than not." So. Specific, concrete reasons (preferably with an example) Why global variables are dangerous When global variables should be use...

Where do you put program scope variables in UI driven application?

Ok, so I know that global variables are considered bad, and the singleton pattern is overused. And I have read in many places that a class should do only one task and contain only those variables that allow it to accomplish that one task. However, while working on my latest project, I actually thought about these rules before writing an...

PHP Automatically "GET" Variables

I am desiging a new website for my company and I am trying to implement switch navigation which is what I have used on all my sites in the past. <?php switch($x) { default: include("inc/main.php"); break; case "products": include("inc/products.php"); break; } ?> For some reason when I go to index.php?x=products nothing happens, it ...

Do I need a semaphore when reading from a global structure?

A fairly basic question, but I don't see it asked anywhere. Let's say we have a global struct (in C) like so: struct foo { int written_frequently1; int read_only; int written_frequently2; }; It seems clear to me that if we have lots of threads reading and writing, we need a semaphore (or other lock) on the written_frequently me...

Storing static user data in a C# windows application.

I have an application that needs to hit the ActiveDirectory to get user permission/roles on startup of the app, and persist throughout. I don't want to hit AD on every form to recheck the user's permissions, so I'd like the user's role and possibly other data on the logged-in user to be globally available on any form within the applica...

PHP Templating

I'm writing a simple templating layer in PHP but I've got myself a little stuck. Here's how it works at the moment: Firstly I use fetch_template to load the template contents from the database - this works (and I collect all the templates at startup if you're interested). I use PHP variables in my template code and in the logic - e.g.:...

Global Variables in Cocoa/Objective-C?

According to Cocoa Programming for Mac OS X, 3rd Edition, on page 202 (chapter 13): You will be registering, reading, and setting defaults in several classes in your application. To make sure that you always use the same name, you should declare those strings in a single file and then simply #import that file into any fil...

When are global variables acceptable?

Everyone here seems to hate global variables, but I see at least one very reasonable use for them: They are great for holding program parameters that are determined at program initialization and not modified afterwords. Do you agree that this is an exception to the "globals are evil" rule? Is there any other exception that you can t...

ok , global variable is condemned, singleton is despised, what's the alternative?

For desktop application that is. This is just a general question that maybe only need general answers. ...

Accessing 'Global' Variables In An ExecuteSQL Task

I have an SSIS package that does the following: Selects the connection strings from a table of servers. The connection string is either the name of the server along with the domain (i.e. Dalin.myhouse.com) or is the direct IP to a server. The package iterates through each connection string and populates a defined 'global' variable. Th...

Accessing variables from other functions without using global variables

I've heard from a variety of places that global variables are inherently nasty and evil, but when doing some non-object oriented Javascript, I can't see how to avoid them. Say I have a function which generates a number using a complex algorithm using random numbers and stuff, but I need to keep using that particular number in some other ...

Set Global Variable for Duration of a Request

Can I set some kind of global variable for the length of a single Request, so that all the controls of the page can respond to it without having to pass it through to each of them? For instance, if someone hit a Save button on my MasterPage, could I set something so that each UserControl on my Page can have a Page_Load like: protected ...

Global variables in Python

A global variable created in one function cannot be used in another function directly. Instead I need to store the global variable in a local variable of the function which needs its access. Am I correct? Why is it so? ...

How to implement dynamic arrays in Delphi

I originally had an array[1..1000] that was defined as a global variable. But now I need that to be n, not 1000 and I don't find out n until later. I know what n is before I fill the array up but I need it to be global therefore need a way to define the size of a global array at run time. Context is filling an array with a linear transf...

Global variables in delphi

Hey guys, I have a console application written in delphi. I saw that I can have global variables by assigning then to units scopes. but in console application I don't use units. (From what I've understood it's forms-only). ...

Getting a Global setting in C #

Taking this article on classes and structs as an example: http://msdn.microsoft.com/en-us/library/ms173109.aspx namespace ProgrammingGuide { // Class definition. public class MyCustomClass { // Class members: // Property. public int Number { get; set; } // Method. public int Multiply...