byref: return byval a_pointer vs. return *a_pointer

General FreeBASIC programming questions.
Post Reply
noop
Posts: 130
Joined: Sep 18, 2006 10:29

byref: return byval a_pointer vs. return *a_pointer

Post by noop »

Hello,

where is the difference in returning a pointer by-value and returning the dereferenced pointer in a byref-function-result?

Code: Select all

dim shared as integer x

function test_b() byref as integer
    dim as integer ptr px = @x
    return byval px
end function

function test_d() byref as integer
    dim as integer ptr px = @x
    return *px
end function

x = 4

print test_b()
print test_d()
sleep
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: byref: return byval a_pointer vs. return *a_pointer

Post by Tourist Trap »

I was affraid that you had found a difference. In my opinion the way it works in your example is normal, there shouldn't be any difference or I don't see why at all.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: byref: return byval a_pointer vs. return *a_pointer

Post by MichaelW »

It looks to me like test_b should return a pointer, and test_d should trigger a warning.

Code: Select all

#include <stdio.h>
#include <conio.h>

int x = 4;

int *test_b()
{
    int *px = &x;
    return px;    
}

int *test_d()
{
    int *px = &x;
    return *px;   // line 15
}

int main(void)
{
    printf( "%d\n%d\n", test_b(), test_d() );
    _getch();
}

Code: Select all

C:\Users\User\FreeBASIC My\byref>gcc.exe -std=c99 -pedantic -Os -m64 -masm=intel
 testc.c -o testc.exe
testc.c: In function 'test_d':
testc.c:15:5: warning: return makes pointer from integer without a cast
     return *px;
     ^

Code: Select all

4206608
4
fxm
Moderator
Posts: 12112
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: byref: return byval a_pointer vs. return *a_pointer

Post by fxm »

No, not warning because this syntax with the BYVAL keyword is allowed by the fb compiler and also documented in manual.
See my previous post:
http://www.freebasic.net/forum/viewtopi ... 15#p210315
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Re: byref: return byval a_pointer vs. return *a_pointer

Post by MichaelW »

fxm wrote:No, not warning because this syntax with the BYVAL keyword is allowed by the fb compiler and also documented in manual...
What is the reason for allowing it?

Do you agree that test_b should return a pointer? Per the "byref as integer" it's supposed to return a pointer, and per the "return byval px" it's supposed to return a pointer.
fxm
Moderator
Posts: 12112
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: byref: return byval a_pointer vs. return *a_pointer

Post by fxm »

test_b() as test_d() returns a reference.
(in fact in internal, always a pointer is passed by value and an internal code dereferences this pointer for the user).
By specifying Return Byval, an address (usually stored in a pointer) can be passed directly as-is, forcing the Byref function result to reference the same memory location which the address pointed to.
noop
Posts: 130
Joined: Sep 18, 2006 10:29

Re: byref: return byval a_pointer vs. return *a_pointer

Post by noop »

fxm, I may have misinterpreted your post/posts. So please bear with me here.
fxm wrote:test_b() as test_d() returns a reference.
By specifying Return Byval, an address (usually stored in a pointer) can be passed directly as-is, forcing the Byref function result to reference the same memory location which the address pointed to.
I don't understand why the function could point to sth. else. If I return *px then the byref-return would make a @(*px) out of it and then dereference it "upon arrival". Using byval px (I feel like) does exactly the same thing. In what scenario could the returned pointer (internally) point to a different address?


I've also had a look at the linked thread, from which I draw the same conclusion.

Code: Select all

sub FAKEPRINT1(byref E as integer)
    Print E
end sub 

dim as integer e = 1

FAKEPRINT1 (e)
FAKEPRINT1 (*(@e))
FAKEPRINT1 (byval @e)

sleep
Output:

Code: Select all

1
1
1
The only difference I saw was the way the parser handled it in a function call:

Code: Select all

function f(byref i as integer) byref as integer
    print "inside=";@i
    f = i
end function

dim as integer i=123
print "i from outside=";@i
Print i
f(i) = 100  'parsed as equality operator, not assignement
print i
(f(i)) = 200
print i
f(byval @i) = 300    
print i

sleep
Output:

Code: Select all

i from outside=1638080
 123
inside=1638076
 123
inside=1638080
 200
inside=1638080
 300
It's interesting to see a different address in the first function call. It seems like a temporary copy is made from the variable i.
fxm
Moderator
Posts: 12112
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: byref: return byval a_pointer vs. return *a_pointer

Post by fxm »

I never wrote that the function could point to something else.
Post Reply