Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Question about sfTcpListener_accept  (Read 5640 times)

0 Members and 1 Guest are viewing this topic.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Question about sfTcpListener_accept
« on: June 30, 2013, 08:54:36 am »
I noticed this a while ago, but thought there must be a reason for it so I never asked, but I have a question about sfTcpListener_accept since I was doing some binding related work when and reminded myself about it.

Anyways, let's take a quick look at this guy.
sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket** connected)
{
    CSFML_CHECK_RETURN(listener, sfSocketError);
    CSFML_CHECK_RETURN(connected, sfSocketError);

    *connected = new sfTcpSocket;
    return static_cast<sfSocketStatus>(listener->This.accept((*connected)->This));
}

Is there any reason we give this function the address of the pointer instead of just the pointer itself?
And wouldn't this do the exact same thing?

sfSocketStatus sfTcpListener_accept(sfTcpListener* listener, sfTcpSocket* connected)
{
    CSFML_CHECK_RETURN(listener, sfSocketError);
    CSFML_CHECK_RETURN(connected, sfSocketError);

    connected = new sfTcpSocket;
    return static_cast<sfSocketStatus>(listener->This.accept(connected->This));
}

I'm just curious about what the difference is.
DSFML - SFML for the D Programming Language.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about sfTcpListener_accept
« Reply #1 on: June 30, 2013, 09:07:00 am »
"connected" is local to the function (it's a copy of the variable passed by the caller). So any modification to it won't be visible to the caller. You must pass a pointer to the variable you want to modify -- and since in this case the variable to modify is a pointer, you must pass a pointer to the pointer.

By the way, thanks for showing me this code: it's wrong. The socket should be destroyed if accept fails.
Laurent Gomila - SFML developer

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Question about sfTcpListener_accept
« Reply #2 on: June 30, 2013, 09:54:14 am »
Maybe I just don't understand pointers as much as I thought, but when you dereference connected, aren't you just accessing the pointer to the object? I don't see how this is a copy. Or maybe I am just using it wrong?

Sorry for making you play teacher :P

By the way, thanks for showing me this code: it's wrong. The socket should be destroyed if accept fails.

Glad I could help!
DSFML - SFML for the D Programming Language.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about sfTcpListener_accept
« Reply #3 on: June 30, 2013, 10:50:28 am »
The function doesn't want to change the sfTcpSocket object, but the sfTcpSocket* pointer. So it must have a sfTcpSocket** argument (a pointer to what it modifies).

Look:

void blop(T x)
{
    x = 0;
}

void blop(T* x)
{
    *x = 0;
}

The first one has no effect outside the function, the second has. Right?

Now replace T with the type that you want to modify. Let's say we want to modify an int:

void blop(int* x)
{
    *x = 0;
}

Easy. Now we want to modify a sfTcpSocket*:

void blop(sfTcpSocket** x)
{
    *x = 0;
}
Laurent Gomila - SFML developer

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Question about sfTcpListener_accept
« Reply #4 on: June 30, 2013, 11:59:37 am »
That all makes sense and all, but what I am saying is that this:
void blop(T* x)
{
    x = new T;
}

...

T* tDog;

blop(tDog);

 

Seems to do the exact same thing as:
void blip(T** x)
{
    *x = new T;
}

...

T* tDog;

blip(&tDog);
 

Which is how sfTcpListener_accept seems to be working.
DSFML - SFML for the D Programming Language.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Question about sfTcpListener_accept
« Reply #5 on: June 30, 2013, 01:27:29 pm »
No it doesn't. Since you pass a copy of tDog and not its address, the function cannot modify it. I don't know why you don't understand that, and I don't know how to explain it better, so maybe you should just try to execute this code and see by yourself.
Laurent Gomila - SFML developer

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Question about sfTcpListener_accept
« Reply #6 on: June 30, 2013, 04:27:06 pm »
Ah, I got ya now! I think it was the passing the pointer by value that was throwing me off, but I totally understand now.

Thank you for taking time to explain it to me. :D
DSFML - SFML for the D Programming Language.