Having some trouble with variadic functions and SDL

I was kind of annoyed that in SDL I have to free one surface at a time. I want to be able to free all of my surfaces at once. So I tried to write a variadic function (I was told about them by Bazzy a day or two ago when I asked about something to do with the Linux kernel source) to do it. I'm not sure whether it will work, I think not. This is what I have at the moment:
1
2
3
4
5
6
7
8
9
10
11
#include "sdl_.h"

void FreeIMG(SDL_Surface* surf, ...) {
    va_list ap; // Deal with our variadic args
    va_start(ap, surf);
aligned?
    
    // Here I want to free each surface that the function was given

    va_end(ap);
}

I'm not so sure that the function itself will even work. I want to free the surface pointed to by surf, but then surf should be a pointer to a pointer (?) because the surfaces given will be pointers themselves.

How can I do this?

The point of the function is to use SDL_FreeSurface(mySurface) to free an arbitrary amount of surfaces given as arguments to the function. I'm just not sure how to do it. Thanks.
You definition is wrong. You need to pass the amount of surfaces you're passing:
1
2
3
4
5
6
7
8
9
10
11
12
13
void FreeNsrf(unsigned n,...){
	va_list l;
	va_start(l,n);
	SDL_Surface *s;
	for (unsigned a=0;a<n;a++){
		s=va_arg(l,SDL_Surface *);
		SDL_FreeSurface(s);
	}
	va_end(l);
}

//example call:
FreeNsrf(3,srfA,srfB,srfC);

This is much worse and error-prone than freeing one surface at a time. Screw up one parameter and there's no telling what can happen.
Last edited on
Oh =[
Is there another way you can suggest of freeing multiple surfaces, or should I just live with it?
I would (and do) live with it. If you're allocating more than three surfaces, it's time to put them in an array or vector, and then you can just free them in a loop.
Oh-Kay... :(

I wanted to be clever...
Keep them all in a std::deque or std::list. You can then free the ones you want using the std::for_each() [or one of the other] algorithm[s].
Ok. I'll try that, thanks.

Screw up one parameter and there's no telling what can happen.

That sounds like fun...

OMG 512 posts. Should I try to get to 1337? Maybe I'll get to 1024, and then I can say I have one "kilopost"... decisions, decisions...
Last edited on
Be clever in your algorithms, not in your syntax.
Last edited on
Man... to slow...

If you wanted to use the variadic function anyway, you don't necessarily have to indicate the number. You could just specify that the argument list is a list of non-NULL pointers, and the list terminates with a NULL pointer.

FreeIMG( foo, bar, baz, NULL );

[edit] BTW, I agree 100% with helios -- please don't use variadic functions...
Last edited on
Of course!
What is it with you guys and "foo-bar"?
Oh, it is to do with the WW2 thing, "FUBAR"...

I just read this, http://www.catb.org/~esr/jargon/html/C/Commonwealth-Hackish.html
That sounds like how I pronounce things. Definitely char with a hard "ch" sound. However I do call ! bang, although most probably because about 80% of the people on the internet are American and about 80% of what can be read on the internet is also American.
Last edited on
FUBAR is common military jargon, not specifically from WWII.
Oh, ok.
Topic archived. No new replies allowed.