Gaming – A dream

Games are the most important part of our life. Who doesn’t like to play games? I think all of them.. Whether you are a kid or grown up.

What would life be like if there is no source of social interaction like gaming?

Its one of the best form of interaction which forces people to think or visualize something different, beyond their imagination. Playing games directly affect your brain’s development. It makes you think out of the box and allows you to enter the virtual world.

It can change your brain. It increases your motor skills, memory, visual abilities, hand-eye coordination and keeps you active.

Sometimes playing more games can become addiction. And it has the power to affect you in the real world. Some researchers have found an increase of about 4% in gamers levels of aggression after playing violent games.
When I was a kid and got my first computer, the first thing I installed in my P3 machine was NFS2 and Mortal Kombat. And I used to spend hours and hours playing games daily, and sometimes skip my morning school.

After 2 years, I realised why shouldn’t I make my game, which everyone can play. Simple 2d game was my first target.

Then I did some research on game development. Have read various blogposts regarding game development.

But game development is one of the most difficult and complicated thing I have ever found. As it involves graphics, basic maths and physics.

As a beginner, I too had many questions around my mind such as,
– which platform to target?
– what programming language should I use?
– should I use game engine, or create my own?
– what library/tool should I use?

My target was to create simple 2d games first without using game engines.

Difference between #define and const?

Const is a declaration of constant, compiler will do appropriate checks during the compile time. #define is a macro compiler will see a value. Speaking in terms of memory const declaration could be more efficient:

const short x = 1;
// this will allocate 16 bits for x (actually it depends on the machine,
I assume unix platform with 32 bits integers)

#define x (short)1
// this will probably allocate 32 bits to hold the constant 1.

  • Use const instead of #define mainly because of compiler checks. And inline functions instead of macros as well. Preprocessor is the obsolete remnant from early C days, there’s no use of it in the oop environment.
  • No diffence realy in memory, both are compiled as numbers, but with const’s you can define it per namespace or class so that you can use it like mylinklist::maxmemebers, it looks better and more readable.
  • #define macro does not take any space on stack, but it gets replaced in the code by its definition thereby increasing the size of exe.

Constant variable occupies space on stack and does not increase the size of the exe.

#define can be used for conditional compilation elsewhere:

#if SOME_DEFINE == 1
// Conditional code
#endif

This is something you can’t do with a const.

 

One should prefer const int sum = 1; over #define sum 1 for a number of reasons:

Scope Based Mechanism:

#defines don’t respect scopes so there is no way to create a class scoped namespace. While const variables can be scoped in classes.

Avoiding Weird magical numbers during compilation errors:

If you are using #define those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.

Ease of Debugging:

Also for same reasons, while debugging #define would provide no help really.
To avoid both above situations const will be a better choice.

String constants, BTW, are one example of such an exception. With #defined string constants one can use compile-time concatenation feature of C/C++ compilers, as in

#define OUT_NAME "output"
#define LOG_EXT ".log"
#define TEXT_EXT ".txt"

const char *const log_file_name = OUT_NAME LOG_EXT;
const char *const text_file_name = OUT_NAME TEXT_EXT;

P.S. Again, just in case, when someone mentions static const as an alternative to #define, it usually means that they are talking about C, not about C++.

Why Use Asprintf?

If you use sprintf() or vsprintf(), you need to allocate a buffer first, and you need to be sure that the buffer is large enough to contain what sprintf writes. Otherwise sprintf will happily overwrite whatever memory lies beyond the end of the buffer.

char* x = (char*) malloc(5 * sizeof(char));
sprintf(x,"%s%s%s", "12", "34", "56"); // writes "123456" but overruns the buffer

… writes the ‘6’ beyond the end of the space allocated to x, either corrupting some other variable, or causing a segmentation fault.

A non-malicious user who provides an overlong input, could cause the program to behave in unexpected ways. A malicious user could exploit this as a way to get their own executable code into the system.

One guard against this is to use snprintf(), which truncates the string to the maximum length you supply.

char *x = (char *) malloc(5 * sizeof(char));
int size = snprintf(x, 5, "%s%s%s", "12", "34", "56"); // writes "12345"

In this case, if the return value size is greater than 5, then you know that truncation occurred – and if you didn’t want truncation, you could allocate a new string and try snprintf again.

char *x = (char *) malloc(BUF_LEN * sizeof(char));
int size = snprintf(x, 5, "%s%s%s", "12", "34", "56");
if(size > BUF_LEN) {
    realloc(&x,size * sizeof(char));
    snprintf(x, 5, "%s%s%s", "12", "34", "56");
}

(that’s a pretty naive algorithm, but it illustrates the point)

asprintf() does this in one step for you – calculates the length of the string, allocates that amount of memory, and writes the string into it.

char *x;
int size = asprintf(&x, "%s%s%s", "12", "34", "56");

In all cases, once you’ve finished with x you need to release it, or you leak memory:

free(x);

asprintf() is an implicit malloc(), so you have to check it worked, just as you would with malloc() or any other system call.

if(size == -1 ) {
   /* deal with error in some way */
}

More Info:
http://stackoverflow.com/questions/12746885/why-use-asprintf