Why use this mode?

why use this ???

void SDL_WM_GetCaption (char **title, char **icon)
{
SDL_VideoDevice *video = current_video;

if ( video ) {
if ( title ) {
*title = video->wm_title;
}
if ( icon ) {
*icon = video->wm_icon;
}
}
}

instead of this ???

void SDL_WM_GetCaption (char *title, char *icon)
{
if ( current_video) {
title = current_video->wm_title;
icon = current_video->wm_icon;
}
}

its only a wested time the first mode ? wm_title and wm_icon are char* and
you only return the pointer to te global var area where are saved …

or my fault ??

Seeya

I don know the details behind SDL, but in general programming, the former is faster than the later because you access a variable with local scope (on the stack), instead of a variable with global scope, with longer adressing that would take longewr to read from memory. Of course, you can’t avoid accessing it at least once ^_-. Also, many compilers optimize local variables as processor registers, which reinforces why to use the first approach.

Also, these varibles are not char*, but char**. They would be char* on a function to ASSIGN the window caption, but when you need to retrieve this value by passing a parameter, you must pass a POINTER to a char*, hence, a char**. The first code is perfectly correct in this sense, where the second would miserably fail, as it would assing to a value on the stack a copy of the pointer to the caption, not the user variable passed as the argument.

Paulo>

From: “Goul_duKat” <goul_dukat at despammed.com>
Date: Tue, 16 Sep 2003 16:58:58 +0200
To: sdl at libsdl.org
Subject: [SDL] Why use this mode ???

why use this ???

void SDL_WM_GetCaption (char **title, char **icon)
{
SDL_VideoDevice *video = current_video;

if ( video ) {
if ( title ) {
*title = video->wm_title;
}
if ( icon ) {
*icon = video->wm_icon;
}
}
}

instead of this ???

void SDL_WM_GetCaption (char *title, char *icon)
{
if ( current_video) {
title = current_video->wm_title;
icon = current_video->wm_icon;
}
}

its only a wested time the first mode ? wm_title and wm_icon are char* and
you only return the pointer to te global var area where are saved …

or my fault ??

Seeya


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1On Tuesday 16 September 2003 16:58, Goul_duKat wrote:

void SDL_WM_GetCaption (char **title, char **icon)
{
SDL_VideoDevice *video = current_video;

if ( video ) {
if ( title ) {
*title = video->wm_title;
}
if ( icon ) {
*icon = video->wm_icon;
}
}
}

instead of this ???

void SDL_WM_GetCaption (char *title, char *icon)
{
if ( current_video) {
title = current_video->wm_title;
icon = current_video->wm_icon;
}
}

The video vs. current_video shouldn’t make a difference.

However, you apparently don’t understand C pointers, strings and arguments
enough.

When the parameters of a function are modified within a function call, the
caller of the function will not know. So if you’ve got something like this:

void foo(int a)
{
a++;
printf("%i\n", a);
}

int main(int argc, char** argv)
{
int a = 4;
foo(a);
printf("%i\n", a);
return 0;
}

the output will be:

5
4

This is why your suggested change will not work.

Note that in the version that SDL uses now, the parameters title and icon
themselves are not changed, but the memory that they point to is changed.
If you changed my example above in a way that foo() takes a pointer to int
instead of an int, the output would be 5\n5\n.

cu,
Nicolai
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/Zy3nsxPozBga0lwRAv6TAJ9BgLK9C+aUipOQmjn0sNpa+4MjowCeMjUx
ReZJt8YoLuqH8FWXm8Bx7PE=
=LJFh
-----END PGP SIGNATURE-----

why use this ???

void SDL_WM_GetCaption (char **title, char **icon)
{
SDL_VideoDevice *video = current_video;

if ( video ) {
if ( title ) {
*title = video->wm_title;
}
if ( icon ) {
*icon = video->wm_icon;
}
}
}

instead of this ???

void SDL_WM_GetCaption (char *title, char *icon)
{
if ( current_video) {
title = current_video->wm_title;
icon = current_video->wm_icon;
}
}

its only a wested time the first mode ? wm_title and wm_icon are char* and
you only return the pointer to te global var area where are saved …

or my fault ??

The first case is safer because it goes to rather extreme lengths to
ensure that the programmer provided a valid pointer. But, mostly it lets
the programmer ask for what he wants by sending in a pointer or NULL. In
the second case he must always provide two valid pointers, other wise
you get an pointer exception.

All in all, the first is safer and more flexible. It shows that a
considerate, helpful, and careful coder did the work.

As for the “waste” involved. The difference in execution time between
the two is measured in fractions of a nanosecond. This is a function
that is used a few times during a program. So the total waste is
measured in fractions of a nanosecond. It is a waste of your limited
life span to worry about such a tiny little thing. You have cost your
self and many other people several minutes of their life over a
potential waste of a couple of CPU cycles.

Don’t sweat the small stuff. :slight_smile: Seriously, don’t worry about tiny
things like this unless they are in the inner loop and are executed
thousands, or maybe even millions, of times per second.

	Bob PendletonOn Tue, 2003-09-16 at 09:58, Goul_duKat wrote:

Seeya


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±----------------------------------+

why use this ???

void SDL_WM_GetCaption (char **title, char **icon)
{
SDL_VideoDevice *video = current_video;

if ( video ) {
if ( title ) {
*title = video->wm_title;
}
if ( icon ) {
*icon = video->wm_icon;
}
}
}

instead of this ???

void SDL_WM_GetCaption (char *title, char *icon)
{
if ( current_video) {
title = current_video->wm_title;
icon = current_video->wm_icon;
}
}

its only a wested time the first mode ? wm_title and wm_icon are char* and
you only return the pointer to te global var area where are saved …

or my fault ??

The first case is safer because it goes to rather extreme lengths to
ensure that the programmer provided a valid pointer. But, mostly it lets
the programmer ask for what he wants by sending in a pointer or NULL. In
the second case he must always provide two valid pointers, other wise
you get an pointer exception.

All in all, the first is safer and more flexible. It shows that a
considerate, helpful, and careful coder did the work.

I hate replying to my own posts, but I didn’t notice the first time that
you didn’t use pointers to pointers. My bad. As others have pointed out,
the second one just plain doesn’t work.On Tue, 2003-09-16 at 10:19, Bob Pendleton wrote:

On Tue, 2003-09-16 at 09:58, Goul_duKat wrote:

As for the “waste” involved. The difference in execution time between
the two is measured in fractions of a nanosecond. This is a function
that is used a few times during a program. So the total waste is
measured in fractions of a nanosecond. It is a waste of your limited
life span to worry about such a tiny little thing. You have cost your
self and many other people several minutes of their life over a
potential waste of a couple of CPU cycles.

Don’t sweat the small stuff. :slight_smile: Seriously, don’t worry about tiny
things like this unless they are in the inner loop and are executed
thousands, or maybe even millions, of times per second.

  Bob Pendleton

Seeya


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±----------------------------------+

“Nicolai Haehnle” <prefect_ at gmx.net> ha scritto nel messaggio
news:200309161736.07574.prefect_ at gmx.net…------
However, you apparently don’t understand C pointers, strings and arguments
enough.

a crap … all of you dont read the source code and the struct of
current_video :-\

to understand the change pointer in name function read the structure !!!

the assign:
*title = video->wm_title;

is
title = wm_title (the wm_title is char) and didn’t copy the c_string
but copy only the pointer to the string … in wm_title is storing a
c_string not an array of string …

it’s very crasy for me district the sdl source or i didn’t understand this
function … help me …
make a little source whit this function plz to understand …

sorry for time all of you spent