As Brian stated, using separate statements is easier to read. Also, keep
in mind that the following statements can be ambiguous:
int a, b, c; // Creates three integers
int *a, b, c; // Three integer pointers, or one integer pointer and two
integers?
I know GCC prefers
int *a, *b, *c; // Three pointers
int *a, b, *c; // Two pointers and a regular integer
I have heard rumors that some non-standard c/c++ compilers may treat the
following as all pointers:
int* a, b, c; // Three pointers
So, for both readability and potential portability, it is best to use
separate statements:
SDL_Surface *video;
SDL_Surface *some_buffer;
// …
As far as optimizations go, Brian also pointed out that there should be no
optimization between declaring those variables in a list or separately.
I hope that helps weed out some of the issue.
-OzOn Mon, Jan 30, 2012 at 4:18 AM, Brian Barrett <brian.ripoff at gmail.com>wrote:
My original post was all about efficiency, but re-reading the original
question I see that that was but one point to the question.
what are cons and pros and whyc one should be used?
Most programmers find separate declarations are easier to read (I am
extrapolating “most” from the source code I’ve read on the Internet).
However, in programming languages that don’t force all declarations to
the top of the current block/scope (C++ and modern C), the preferred
solution is to actually to wait until the last possible moment to
declare a variable, and then to initialise it with a useful value.
Your example might then look like:
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, SDL_SWSURFACE);
if(!screen)
{
// error handling
}
SDL_Surface *background = IMG_Load(“background.png”);
if(!background)
{
// error handling
}
SDL_Surface *image1 = IMG_Load(“image_1.png”);
if(!image1)
{
// error handling
}
The idea here is that you avoid having variables in scope that aren’t
usable yet. It also makes it easier to lift a chunk of code into a
separate helper function, if necessary.
– Brian
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org