avoiding global variables while using GLUT - c++

GLUT is a great API and it's very easy to use but I am having some difficulty with how it handles scope. When defining callbacks there is no option to pass parameters so it seems to me as though the programmer is forced to rely on global variables, which I find difficult to accept. Right now I have all the GLUT code in it's own module, running on it's own thread, and define a static pointer which I assign at the entry point to the module, like so:
Main module
int main( int argc, char** argv ) {
int foo;
boost::thread graphicsThread(glutMain, argc, argv, &foo);
//...
graphicsThread.join();
return 0;
}
GLUT module
static int* FOO_REF;
int glutMain( int argc, char** argv, int* foo ) {
FOO_REF = foo;
glutInit(&argc, argv);
//etc...
Is there a better solution than this?

If you're using freeglut or a derivative and willing to confine yourself to freeglut derivatives only it has a non-standard extension to solve exactly the problem. You can associate a void* with every window. If you make that a struct that contains all the per-window data you want you can avoid the globals entirely.
Synopsis:
#include <GL/glut.h>
#include <GL/freeglut_ext.h>
void * glutGetWindowData();
glutSetWindowData(void *data);

What I did was declare a global.h for all my globals. And initialize them in main. For my "basic/general" variables (ie camera, position, iterationNumber,...) they were all declared seperately. In main:
include "global.h"
Vector position_g = ...
Vector angles_g = ...
int time_g = 0;
int main () {
...
}
But for the variables that were "section specific" ie only in one game mode/level, I made a union and an enum.
enum mainGame{skipLevel, ...};
enum mainMenu {viewingSettings, ...};
typedef union generic_union {
int i;
char c;
bool b;
char s[100]; // or char * s;
float f;
} generic;
And declared a globalData variable.
extern generic * globalData; // in global.h
generic * globalData = NULL; // in main
Which can now be used:
int main () {
...
globalData = malloc (sizeof (generic)*numGlobals);
globalData[skipLevel].b = false;
...
}
Now when in your key press handling function, you can assign a key to toggle globalData[skipLevel]. And in any other file all you have to do is include global.h.
include "global.h"
void onKeyPress (... ) {
If (inMainGame) {
If (key == SPACE) {
globalData [skipLevel] = true;
}
}
And finally the use:
include "global.h"
void levelManager () {
...
if (globalData[skipLevel]) level++;
...
}
Pros
Only have to lug around 1 variable and one include.
You can free variables you no longer want or are using in that instance. (very useful for reducing "pollution"). If one game mode only needs 1 variable, thats all you have to store, if it needs 48, just as easy!
Can easily handle any variable type, by adding it to the union.
Totally Portable
Cons
Have to remember variable type to dereference the generic union (not that hard)
And watchout for enums being used (you can use a style for enums like mainMenu_e to solve this)
Adds complexity, but as the variable number grow, a system like this because well worth it.
Personally I find this very neat despite the few extra moving parts.
If this is unclear let me know, and Ill try to fix it :)

Related

Access command line arguments in C++

Is it possible to get the command line arguments without receiving them via int main(int, char**)? I don't want to pass the arguments to multiple methods, so a global function would be perfect. Also, I do not want to store the arguments by myself via global variables. I'm running Windows and Linux.
edit:
Example:
int main()
{
int argc = GetArgumentCount();
char ** argv = GetArguments();
return 0;
}
edit:
It's possible to use LPTSTR WINAPI GetCommandLine(void); in win32.
https://msdn.microsoft.com/en-us/library/ms683156(v=vs.85).aspx
I'm looking for equivalent functions in Linux.
Is it possible to get the command line arguments without receiving
them via int main(int, char**)?
Yes, with platform-specific functions. But that's not necessary (see below).
I don't want to pass the arguments to multiple methods,
That's understandable. It's an anti-pattern also known as "tramp data".
Also, I do not want to store the arguments by myself via global variables.
Yes, global variables are rarely a good idea.
Here's an alternative approach: store them as a static local container object in some globally available non-member function which returns the container by reference.
Example:
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string>& Arguments()
{
static std::vector<std::string> arguments;
return arguments;
}
void f()
{
// use arguments anywhere else:
std::cout << Arguments()[0];
}
int main(int argc, char* argv[])
{
for (int i = 0; i < argc; ++i)
{
Arguments().push_back(argv[i]);
}
f();
}
Of course, this can be made more sophisticated. For example, you might want to prevent anyone else but main from changing the vector by wrapping the vector in a class and declaring main as a friend, something like this:
#include <iostream>
#include <string>
#include <vector>
class Arguments final
{
public:
static int Count()
{
return arguments.size();
}
static std::string Get(int index)
{
return arguments[index];
};
private:
Arguments() = delete;
friend int main(int argc, char* argv[]);
static std::vector<std::string> arguments;
};
std::vector<std::string> Arguments::arguments;
void f()
{
// use Arguments anywhere else:
std::cout << Arguments::Get(0);
}
int main(int argc, char* argv[])
{
for (int i = 0; i < argc; ++i)
{
Arguments::arguments.push_back(argv[i]);
}
f();
}
Note that special care is needed to avoid bugs at program shutdown, when static objects are destroyed. You must make sure that no destructor of a static object accesses Arguments, or else you risk undefined behaviour.
Is it possible to get the command line arguments without receiving them via int main(int, char**) ?
No (at least, not in portable manner), however you could put the usual argc, argv into some global variable (or other global data, often after parsing). And that could also be into some static data with other functions in the same translation unit retrieving it. Hence a reasonable (readable and portable) approach would be:
static int myargc;
static char **myargv;
int GetArgumentCount(void) {
return myargc;
}
char**GetArguments(void) {
return myargv;
}
int main(int argc, char**argv) {
myargc= argc;
myargv= argv;
/// etc....
Notice that on some systems or some implementations you might access to the command line arguments in some other ways.
dirty Linux specific tricks
For example, on Linux, using proc(5), you might parse /proc/self/cmdline but it is unreasonable to do that (on your Linux system, try to run od -cx /proc/self/cmdline in your terminal to guess what I mean), so I still recommend using int main(int argc, char**argv) and storing, in some global or static data, the argc and argv, or more probably, do some parsing of program arguments.
So on Linux, you might code your GetArgumentCount and GetArguments functions (by parsing /proc/self/cmdline, see also this) but it would be foolish to do so without using argc and argv from main (even if it is technically doable). Coding such a crazy GetArgumentCount and GetArguments which parses /proc/self/cmdline is left as an exercise to the masochistic reader.
Perhaps you need that because some static data's constructor -running before main and called before it from crt0- uses them; but in that case, your program's design is IMHO very wrong. I have no idea if similar dirty tricks are doable in Windows.
If you really think that is a good idea, you can easily make cor command line arguments global:
int argc_ = 0;
char** argv_ = NULL;
int main(int argc, char* argv[]) {
argc_ = argc;
argv_ = argv;
// ...
}

error: ‘length’ was not declared in this scope c++

I am beginner c++ programmer, It's my first program even (For those who are very keen to give negatives). I had written the same code in c but now trying to do in c++.
Where I get the following error.
error: ‘length’ was not declared in this scope
My code is as below.
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
class Huffman
{
public:
int data_size, length; //THis length variable is not accessible in main function below in main function.
Huffman(char *filename);
~Huffman();
struct Huffman1
{
int value;
unsigned char sym; /* symbol */
struct Huffman1 *left,*right; /* left and right subtrees */
}; typedef struct Huffman1 Node;
};
Huffman::Huffman(char * file_name)
{
//I will do something here soon
}
Huffman::~Huffman()
{
}
int main(int argc, char * * argv)
{
length=10; //Not accessible here.
if (argc < 2)
{
cout<<"Ohh.. Sorry , you forgot to provide the Input File please" <<endl;
return(0);
}
Huffman Object1(argv[1]);
return(0);
}
I am not sure that it's c++ programming error because it may be because i am compiling it g++ Filename.c -o filename. Could someone please correct if it's a programming error or it's due to the way i compile ?
thanks.
length is a member of the class, so it does not exist outside the class.
You can access lenth after creating an object of class Huffman as follows
Huffman Object(argv[1]);
Object.length = 10;
length belongs to Huffman class. So you should use it for Object1 after it's definition:
Huffman Object1(argv[1]);
Object1.length = 10;
You know, public: doesn't mean that anything put inside under that branch in the class tree, will be accessible everywhere it just means that you access the instance variables of the class through "dot notation" like so Object.length.
However if you truly wanted length to be accessible everywhere, you should declare it as a global variable:
short int length;
class Huffman{
...
};
...
It's a compile error and your code is responsible. You defined length inside your Huffman class. It's a member of that class, not a global variable.
Imagine your class as a C Struct. You'd need to create a struct first in order to access the variable. Same thing applies to C++ classes.
Try Object1.length = 10; after you create the instance of your class.
EDIT
For your purposes, use C++ classes as you would use C structs. That will do the trick.
I would actually put the Node struct declaration outside of the Huffman class. I think it's easier to understand. Also, using a typedef to a struct is not really that useful in C++ for these cases, the name of the struct is usable by just declaring the struct.
The pointers do not allocate memory for the struct themselves. Only after you allocate memory they will be usable, and even then they're members of Object1, so you need that too.
#include <iostream>
#include <fstream>
#include <assert.h>
using namespace std;
struct Node
{
int value;
unsigned char sym; /* symbol */
};
class Huffman
{
public:
int data_size, length; //THis length variable is not accessible in main function below in main function.
Huffman(char *filename);
~Huffman();
Node *left,*right; /* left and right subtrees */
};
Huffman::Huffman(char * file_name)
{
//I will do something here soon
}
Huffman::~Huffman()
{
}
int main(int argc, char * * argv)
{
length=10; //Not accessible here.
if (argc < 2)
{
cout<<"Ohh.. Sorry , you forgot to provide the Input File please" <<endl;
return(0);
}
Huffman Object1(argv[1]);
Object1.left = new Node;
Object1.right = new Node;
//Do your stuff here...
Object1.left->sym;
return(0);
}
This should get you started, it is by no means a perfect implementation. It's not even very C++ oriented, but I already went ahead of myself with the answer. This is a topic for a very different question, which you're welcome to ask in SO, but try not to make questions inside questions.
Good luck!
length is part of your class, not main, thus the compiler is right.
Members belong to an object and are accessed liek this:
Huffman huffmannObj(...);
std::cout << huffmannObj.length << std::endl;
length is a publicly accessible member of your class, but you'll need an instance of that class first before you can do anything with the member
Huffman h(whatever_constructor_params);
h.length = 10;
...is ok

Changing C++ program entry point. STL crashes

So I can successfully change the C++ entry point to be in a class, which helps me isolate my graphics system initialization from the main program code. But when I call some library code from the new main the whole program crashes. For example:
#include <iostream>
using namespace std;
int ENTRY(){
cout << "hello from here" << endl;
system("pause");
}
I compile it with these linker options: -e__Z5ENTRYv -nostartfiles
Without the /cout/ line it works fine, else it crashes with /Access Violation/ at
Is there something that I'm missing?
What exactly do you think -nostartfiles does?
It suppresses the CRT initialization, which among other things is responsible for calling global constructors. Without global constructors, cout isn't initialized. Without initialization, your program goes boom.
Why mess with this anyway? Wouldn't it be easier to just link in a small boilerplate main? It could look like this:
// app.hpp
class App {
protected:
App();
virtual ~App();
private:
virtual int run(/*args if you want them*/) = 0;
int startup(/*args if you want them*/);
friend int app_run(/*args if you want them*/);
};
// app.cpp: just add this to your project
namespace { App* the_app; }
App::App() { the_app = this; }
App::~App() {}
int App::startup() {
// Add whatever code you want here, e.g. to create a window.
return run();
}
int app_run() { return the_app->startup(); }
int main() { return app_run(); }
int wmain() { return app_run(); }
int WinMain(HINSTANCE, HINSTANCE, char*, int) { return app_run(); }
int wWinMain(HINSTANCE, HINSTANCE, WCHAR*, int) { return app_run(); }
Now just derive your main class from App and add a global object of that type.
In C++, main is a magic function; the compiler can generate
extra code in it to, for example, initialize static variables.
Or, depending on the implementation, this may be done by the
startup code. In either case, setting some other global symbol
as entry point will probably mean that static variables will not
be initialized. Don't do it: changing the entry point like this
is really only valid if you use no static variables, anywhere,
and even then, it's shaky.

Partially initialize variable defined in other module

I'm considering a certain solution where I would like to initialize a cell of an array that is defined in other module (there will be many modules initializing one table). The array won't be read before running main (so there is not problem with static initialization order).
My approach:
/* secondary module */
extern int i[10]; // the array
const struct Initialize {
Initialize() { i[0] = 12345; }
} init;
/* main module */
#include <stdio.h>
int i[10];
int main()
{
printf("%d\n", i[0]); // check if the value is initialized
}
Compiler won't strip out init constant because constructor has side effects. Am I right? Is the mechanism OK? On GCC (-O3) everything is fine.
//EDIT
In a real world there will be many modules. I want to avoid an extra module, a central place that will gathered all minor initialization routines (for better scalability). So this is important that each module triggers its own initialization.
This works with MSVC compilers but with GNU C++ does not (at least for me). GNU linker will strip all the symbol not used outside your compilation unit. I know only one way to guarantee such initialization - "init once" idiom. For examle:
init_once.h:
template <typename T>
class InitOnce
{
T *instance;
static unsigned refs;
public:
InitOnce() {
if (!refs++) {
instance = new T();
}
}
~InitOnce() {
if (!--refs) {
delete instance;
}
}
};
template <typename T> unsigned InitOnce<T>::refs(0);
unit.h:
#include "init_once.h"
class Init : public InitOnce<Init>
{
public:
Init();
~Init();
};
static Init module_init_;
secondary.cpp:
#include "unit.h"
extern int i[10]; // the array
Init::Init()
{
i[0] = 12345;
}
...
I don't think you want the extern int i[10]; in your main module, though, adf88.
EDIT
/*secondary module (secondary.cpp) */
int i[10];
void func()
{
i[0]=1;
}
.
/*main module (main.cpp)*/
#include<iostream>
extern int i[];
void func();
int main()
{
func();
std::cout<<i[0]; //prints 1
}
Compile, link and create and executable using g++ secondary.cpp main.cpp -o myfile
In general constructors are used(and should be used) for initializing members of a class only.
This might work, but it's dangerous. Globals/statics construction order within a single module is undefined, and so is module loading order (unless you're managing it explicitly). For example, you assume that during secondary.c Initialize() ctor run, i is already present. You'd have to be very careful not to have two modules initialize the same common data, or have two modules carry out initializations with overlapping side effects.
I think a cleaner design to tackle such a need is to have the owner of the common data (your main module) expose it as a global singleton, with an interface to carry out whichever data initializations needed. You'd have a central place to control init-order, and maybe even control concurrent access (using critical sections or other concurrency primitives). Along the lines of your simplified example, that might be -
/main module (main.c)/
#include
class CommonDat
{
int i;
public:
const int GetI() { return i;}
void SetI(int newI) { i = newI; }
void incI()
{
AcquireSomeLock();
i++;
ReleaseTheLock();
}
}
CommonDat g_CommonDat;
CommonDat* getCommonDat() { return &g_CommonDat; }
int main(void)
{
printf("%d",getCommonDat()->GetI());
}
It's also preferable to have the secondary modules call these interfaces at controlled times in runtime (and not during the global c'tors pass).
(NOTE: you named the files as C files, but tagged the question as c++. The suggested code is c++, of course).
May I ask why you use an array (running the risk of getting out of bounds) when you could use a std::vector ?
std::vector<int>& globalArray()
{
static std::vector<int> V;
return V;
}
bool const push_back(std::vector<int>& vec, int v)
{
vec.push_back(v);
return true; // dummy return for static init
}
This array is lazily initialized on the first call to the function.
You can use it like such:
// module1.cpp
static bool const dummy = push_back(globalArray(), 1);
// module2.cpp
static bool const dummy = push_back(globalArray(), 2);
It seems much easier and less error-prone. It's not multithread compliant until C++0x though.

C++: Define simple constant for use?

In C++ I wanted to define a constant that I can use in another function, A short answer on how to do this will be fine..
Lets say at the beginning of my code I want to define this constant:
//After #includes
bool OS = 1; //1 = linux
if (OS) {
const ??? = "clear";
} else {
const ??? = "cls";
}
I don't know what type to use to define the "clear" string... I'm so confused.
Later on I want to use it within a function:
int foo() {
system(::cls); //:: for global
return 0;
}
How would I define the string up top, and use the string down below? I heard char only had one character and things... I'm not sure how to use , since it says it's converting string into const char or something.
char* isn't quite a char. char* is basically a string (it's what strings were before C++ came along).
For illustration:
int array[N]; // An array of N ints.
char str[N]; // An array of N chars, which is also (loosely) called a string.
char[] degrades to char*, so you'll often see functions take a char*.
To convert std::string to const char*, you can simply call:
std::string s;
s.c_str()
In this case, it's common to use the preprocessor to define your OS. This way you can use the compiler to do the platform specific stuff:
#ifdef OS_LINUX
const char cls[] = "clear";
#elif OS_WIN
const char cls[] = "cls";
#endif
One thing you may want to consider is making it a function. This avoids nasty dependencies of global construction order.
string GetClearCommand() {
if (OS == "LINUX") {
return "clear";
} else if (OS == "WIN") {
return "cls";
}
FAIL("No OS specified?");
return "";
}
What it looks like you're trying to do is this:
#include <iostream>
using namespace std;
#ifdef LINUX
const char cls[] = "LINUX_CLEAR";
#elif WIN
const char cls[] = "WIN_CLEAR";
#else
const char cls[] = "OTHER_CLEAR";
#endif
void fake_system(const char* arg) {
std::cout << "fake_system: " << arg << std::endl;
}
int main(int argc, char** argv) {
fake_system(cls);
return 0;
}
// Then build the program passing your OS parameter.
$ g++ -DLINUX clear.cc -o clear
$ ./clear
fake_system: LINUX_CLEAR
Here's the problem, you're suffering from going out of scope with the variables. If I declare something within brackets, it only exists within the brackets.
if( foo ){
const char* blah = "blah";
}
Once we leave the if statement, the variable blah disappears. You'll need to instantiate it non-locally to whatever brackets you write. Hence:
void Bar(){
const char* blah = "blah";
if( foo ){
//blah exists within here
}
}
However, blah will not exist outside of Bar. Get it?
Yet another option is to create a class with a bunch of static methods. Create a new method for each command. Something like:
// in sys-commands.h
class SystemCommands {
public:
static char const* clear();
static char const* remove();
};
This gives you a few nice options for the implementation. The nicest one is to have a separate implementation file for each platform that you select during compile time.
// in sys-commands-win32.cpp
#include "sys-commands.h"
char const* SystemCommands::clear() { return "cls"; }
char const* SystemCommands::remove() { return "erase /f/q"; }
// in sys-commands-macosx.cpp
#include "sys-commands.h"
char const* SystemCommands::clear() { return "/usr/bin/clear"; }
char const* SystemCommands::remove() { return "/bin/rm -fr"; }
Which file gets compiled will determine which command set will be used. Your application code will look like:
#include <cstdlib>
#include "sys-commands.h"
int main() {
std::system(SystemCommands::clear());
return 0;
}
Edit: I forgot to mention that I prefer static functions to global constants for a bunch of reasons. If nothing else, you can make them non-constant without changing their types - in other words, if you ever have to select the command set based on runtime settings, the user code does not have to change or even be aware that such a change occurred.
You can use a common header file and link to different modules depending on the systen:
// systemconstants.hpp
#ifndef SYSTEM_CONSTANTS_HPP_INCLUDED
#define SYSTEM_CONSTANTS_HPP_INCLUDED
namespace constants {
extern const char cls[]; // declaration of cls with incomplete type
}
#endif
In case of Linux, just compile and link to this one:
// linux/systemconstants.cpp
#include "systemconstants.hpp"
namespace constants {
extern const char cls[] = "clear";
}
In case of Windows, just compile and link to this one:
// windows/systemconstants.cpp
#include "systemconstants.hpp"
namespace constants {
extern const char cls[] = "cls";
}
System-specific translation units could be placed in specific subdirectories (linux/, windows/, etc) of which one could be automatically selected during the build process. This extends to many other things, not just string constants.